Skip to content

Commit 790af61

Browse files
author
Babak
committed
change CHANGELOG.md and format codes
1 parent aca283a commit 790af61

File tree

7 files changed

+119
-11
lines changed

7 files changed

+119
-11
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 2.0.1
2+
* ## new feature
3+
* Decode utf-8
4+
* streamChat
5+
6+
## 2.0.0
7+
* work for lower Dart SDK version
8+
19
## 2.0.0-dev-1
210

311
* ## Add new crucial features

example/lib/main.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:example/sections/chat.dart';
2+
import 'package:example/sections/chat_stream.dart';
23
import 'package:example/sections/embed_batch_contents.dart';
34
import 'package:example/sections/embed_content.dart';
45
import 'package:example/sections/response_widget_stream.dart';
@@ -54,11 +55,12 @@ class _MyHomePageState extends State<MyHomePage> {
5455
SectionItem(0, 'Stream text', const SectionTextStreamInput()),
5556
SectionItem(1, 'textAndImage', const SectionTextAndImageInput()),
5657
SectionItem(2, 'chat', const SectionChat()),
57-
SectionItem(3, 'text', const SectionTextInput()),
58-
SectionItem(4, 'embedContent', const SectionEmbedContent()),
59-
SectionItem(5, 'batchEmbedContents', const SectionBatchEmbedContents()),
58+
SectionItem(3, 'Stream chat', const SectionStreamChat()),
59+
SectionItem(4, 'text', const SectionTextInput()),
60+
SectionItem(5, 'embedContent', const SectionEmbedContent()),
61+
SectionItem(6, 'batchEmbedContents', const SectionBatchEmbedContents()),
6062
SectionItem(
61-
6, 'response without setState()', const ResponseWidgetSection()),
63+
7, 'response without setState()', const ResponseWidgetSection()),
6264
];
6365

6466
@override
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import 'package:example/widgets/chat_input_box.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_gemini/flutter_gemini.dart';
4+
import 'package:flutter_markdown/flutter_markdown.dart';
5+
6+
class SectionStreamChat extends StatefulWidget {
7+
const SectionStreamChat({super.key});
8+
9+
@override
10+
State<SectionStreamChat> createState() => _SectionStreamChatState();
11+
}
12+
13+
class _SectionStreamChatState extends State<SectionStreamChat> {
14+
final controller = TextEditingController();
15+
final gemini = Gemini.instance;
16+
bool _loading = false;
17+
18+
bool get loading => _loading;
19+
20+
set loading(bool set) => setState(() => _loading = set);
21+
final List<Content> chats = [];
22+
23+
@override
24+
Widget build(BuildContext context) {
25+
return Column(
26+
children: [
27+
Expanded(
28+
child: chats.isNotEmpty
29+
? Align(
30+
alignment: Alignment.bottomCenter,
31+
child: SingleChildScrollView(
32+
reverse: true,
33+
child: ListView.builder(
34+
itemBuilder: chatItem,
35+
shrinkWrap: true,
36+
physics: const NeverScrollableScrollPhysics(),
37+
itemCount: chats.length,
38+
reverse: false,
39+
),
40+
),
41+
)
42+
: const Center(child: Text('Search something!'))),
43+
if (loading) const CircularProgressIndicator(),
44+
ChatInputBox(
45+
controller: controller,
46+
onSend: () {
47+
if (controller.text.isNotEmpty) {
48+
final searchedText = controller.text;
49+
chats.add(
50+
Content(role: 'user', parts: [Parts(text: searchedText)]));
51+
controller.clear();
52+
loading = true;
53+
54+
gemini.streamChat(chats).listen((value) {
55+
print("-------------------------------");
56+
print(value.output);
57+
loading = false;
58+
setState(() {
59+
if (chats.isNotEmpty &&
60+
chats.last.role == value.content?.role) {
61+
chats.last.parts!.last.text =
62+
'${chats.last.parts!.last.text}${value.output}';
63+
} else {
64+
chats.add(Content(
65+
role: 'model', parts: [Parts(text: value.output)]));
66+
}
67+
});
68+
});
69+
}
70+
},
71+
),
72+
],
73+
);
74+
}
75+
76+
Widget chatItem(BuildContext context, int index) {
77+
final Content content = chats[index];
78+
79+
return Card(
80+
elevation: 0,
81+
color:
82+
content.role == 'model' ? Colors.blue.shade800 : Colors.transparent,
83+
child: Padding(
84+
padding: const EdgeInsets.all(8.0),
85+
child: Column(
86+
crossAxisAlignment: CrossAxisAlignment.start,
87+
children: [
88+
Text(content.role ?? 'role'),
89+
Markdown(
90+
shrinkWrap: true,
91+
physics: const NeverScrollableScrollPhysics(),
92+
data:
93+
content.parts?.lastOrNull?.text ?? 'cannot generate data!'),
94+
],
95+
),
96+
),
97+
);
98+
}
99+
}

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ packages:
148148
path: ".."
149149
relative: true
150150
source: path
151-
version: "2.0.0"
151+
version: "2.0.1"
152152
flutter_lints:
153153
dependency: "direct dev"
154154
description:

lib/src/implement/gemini_implement.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class GeminiImpl implements GeminiInterface {
5757
'contents': [
5858
{
5959
"parts": [
60-
{"text": text},
60+
{"text": text}
6161
]
6262
}
6363
]
@@ -121,7 +121,6 @@ class GeminiImpl implements GeminiInterface {
121121
try {
122122
res = utf8.decode(list);
123123
} catch (e) {
124-
print("error: $e");
125124
cacheUnits = list;
126125
continue;
127126
}

lib/src/init.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class Gemini implements GeminiInterface {
4949
: _impl = GeminiImpl(
5050
api: GeminiService(
5151
Dio(BaseOptions(
52-
baseUrl: '${baseURL ?? Constants.baseUrl}${version ?? Constants.defaultVersion}/',
52+
baseUrl:
53+
'${baseURL ?? Constants.baseUrl}${version ?? Constants.defaultVersion}/',
5354
contentType: 'application/json',
5455
headers: headers,
5556
)),

pubspec.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: flutter_gemini
22
description: Flutter Google Gemini SDK. Google Gemini is a set of cutting-edge large language models
33
(LLMs) designed to be the driving force behind Google's future AI initiatives.
4-
version: 2.0.0-dev-1
4+
version: 2.0.1
55
homepage: https://github.com/babakcode/flutter_gemini
66
topics:
77
- gemini
@@ -23,8 +23,7 @@ platforms:
2323
macos:
2424

2525
environment:
26-
sdk: '>=3.1.3 <4.0.0'
27-
flutter: ">=1.17.0"
26+
sdk: '>=3.0.0 <4.0.0'
2827

2928
dependencies:
3029
dio: ^5.4.0

0 commit comments

Comments
 (0)