Skip to content

Commit

Permalink
- Added support for markdown
Browse files Browse the repository at this point in the history
- Added quick actions
    - Start New chat
    - Copy
  • Loading branch information
vineyrawat committed Apr 22, 2024
1 parent fa99a0b commit 23f7e29
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/providers/gemini_chat_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class GeminiChatProvider extends StateNotifier<GeminiChatState> {
state = state.copyWith(isTyping: isTyping);
}

reset() {
state = state.copyWith(isLoading: false, messages: [], isTyping: false);
}

init() async {
// create a timeout
await Future.delayed(const Duration(seconds: 2));
Expand Down
65 changes: 65 additions & 0 deletions lib/screens/chat.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'dart:developer';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import "package:flutter/material.dart";
import 'package:flutter/services.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:image_picker/image_picker.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:flutter_chat_ui/flutter_chat_ui.dart';
Expand All @@ -11,6 +14,7 @@ import 'package:simple_gradient_text/simple_gradient_text.dart';
import 'package:shimmer/shimmer.dart';
import 'package:speech_to_text/speech_recognition_result.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;
import 'package:toastification/toastification.dart';

class ChatPage extends ConsumerStatefulWidget {
const ChatPage({super.key});
Expand All @@ -37,6 +41,64 @@ class _ChatPageState extends ConsumerState<ChatPage> {
customStatusBuilder: (message, {required context}) {
return const SizedBox();
},
textMessageBuilder: (p0, {required messageWidth, required showName}) {
if (p0.author.id == '1') {
return Padding(
padding: const EdgeInsets.all(15.0),
child: Text(p0.text, style: const TextStyle(color: Colors.white)),
);
}
return Markdown(
data: p0.text,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
);
},
listBottomWidget: AnimatedContainer(
height: geminiChat.isTyping ? 0 : 80,
duration: const Duration(milliseconds: 300),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0)
.copyWith(bottom: 20),
child: Row(
children: [
TextButton(
onPressed: () {
ref.read(geminiChatProvider.notifier).reset();
},
child: Row(
children: [
Icon(CupertinoIcons.bolt_circle,
color: Theme.of(context).colorScheme.primary),
const SizedBox(width: 5),
const Text("Start New Chat"),
],
)),
IconButton(
onPressed: () {
var text =
(geminiChat.messages.first.toJson()["type"] == 'text')
? geminiChat.messages.first.toJson()["text"]
: '';
log(text);
Clipboard.setData(ClipboardData(text: text));
toastification.show(
context: context,
type: ToastificationType.success,
style: ToastificationStyle.flat,
title: const Text('Copied'),
description: const Text('Copied to clipboard'),
alignment: Alignment.bottomCenter,
autoCloseDuration: const Duration(seconds: 4),
boxShadow: lowModeShadow,
);
},
icon: Icon(Icons.copy_rounded,
color: Theme.of(context).colorScheme.primary))
],
),
),
),
emptyState: EmptyStateWidget(onSendPressed: (p0) async {
FocusManager.instance.primaryFocus?.unfocus();
await ref
Expand Down Expand Up @@ -336,7 +398,10 @@ class _CustomBottomInputBarState extends State<CustomBottomInputBar> {
widget.onSendPressed(inputController.text);
inputController.clear();
},
minLines: 1,
maxLines: 5,
controller: inputController,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintStyle: const TextStyle(
fontSize: 18,
Expand Down
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_markdown:
dependency: "direct main"
description:
name: flutter_markdown
sha256: "9921f9deda326f8a885e202b1e35237eadfc1345239a0f6f0f1ff287e047547f"
url: "https://pub.dev"
source: hosted
version: "0.7.1"
flutter_parsed_text:
dependency: transitive
description:
Expand Down Expand Up @@ -733,6 +741,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.7.0"
markdown:
dependency: transitive
description:
name: markdown
sha256: ef2a1298144e3f985cc736b22e0ccdaf188b5b3970648f2d9dc13efd1d9df051
url: "https://pub.dev"
source: hosted
version: "7.2.2"
matcher:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.1.0
version: 1.2.0

environment:
sdk: ">=3.2.3 <4.0.0"
Expand Down Expand Up @@ -54,6 +54,7 @@ dependencies:
image_picker: ^1.1.0
google_generative_ai: ^0.3.1
speech_to_text: ^6.6.1
flutter_markdown: ^0.7.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 23f7e29

Please sign in to comment.