|
| 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 | +} |
0 commit comments