|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:flutter/rendering.dart'; |
| 4 | +import 'package:flutter_ai/services/gemini_services.dart'; |
| 5 | +import 'package:http/http.dart' as http; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | + |
| 8 | +class GeminiMsgScreen extends StatefulWidget { |
| 9 | + const GeminiMsgScreen({super.key}); |
| 10 | + |
| 11 | + @override |
| 12 | + State<GeminiMsgScreen> createState() => _GeminiMsgScreenState(); |
| 13 | +} |
| 14 | + |
| 15 | +class _GeminiMsgScreenState extends State<GeminiMsgScreen> { |
| 16 | + final TextEditingController chatController = TextEditingController(); |
| 17 | + final ScrollController scrollController = ScrollController(); |
| 18 | + List<Map<String, dynamic>> chatHistory = []; |
| 19 | + |
| 20 | + sendMessage() { |
| 21 | + FocusManager.instance.primaryFocus?.unfocus(); |
| 22 | + if (chatController.text.isNotEmpty) { |
| 23 | + chatHistory.add({ |
| 24 | + "time": DateTime.now(), |
| 25 | + "message": chatController.text, |
| 26 | + "isSender": true, |
| 27 | + }); |
| 28 | + chatController.clear(); |
| 29 | + } |
| 30 | + if (mounted) setState(() {}); |
| 31 | + if (scrollController.hasClients) { |
| 32 | + scrollController.jumpTo(scrollController.position.maxScrollExtent); |
| 33 | + } |
| 34 | + if (mounted) setState(() {}); |
| 35 | + getResponses(); |
| 36 | + } |
| 37 | + |
| 38 | + void getResponses() async { |
| 39 | + final url = |
| 40 | + "https://generativelanguage.googleapis.com/v1beta2/models/chat-bison-001:generateMessage?key=${GeminiServices.apikey}"; |
| 41 | + final uri = Uri.parse(url); |
| 42 | + List<Map<String, String>> msg = []; |
| 43 | + for (var i = 0; i < chatHistory.length; i++) { |
| 44 | + msg.add({"content": chatHistory[i]["message"]}); |
| 45 | + } |
| 46 | + Map<String, dynamic> request = { |
| 47 | + "prompt": { |
| 48 | + "messages": [msg] |
| 49 | + }, |
| 50 | + "temperature": 0.25, |
| 51 | + "candidateCount": 1, |
| 52 | + "topP": 1, |
| 53 | + "topK": 1 |
| 54 | + }; |
| 55 | + chatHistory.add({ |
| 56 | + "time": DateTime.now(), |
| 57 | + "message": 'typing', |
| 58 | + "isSender": false, |
| 59 | + }); |
| 60 | + if (scrollController.hasClients) { |
| 61 | + scrollController.jumpTo(scrollController.position.maxScrollExtent); |
| 62 | + } |
| 63 | + final response = await http.post(uri, body: jsonEncode(request)); |
| 64 | + chatHistory.removeLast(); |
| 65 | + chatHistory.add({ |
| 66 | + "time": DateTime.now(), |
| 67 | + "message": json.decode(response.body)["candidates"][0]["content"], |
| 68 | + "isSender": false, |
| 69 | + }); |
| 70 | + if (mounted) setState(() {}); |
| 71 | + if (scrollController.hasClients) { |
| 72 | + scrollController.animateTo( |
| 73 | + scrollController.position.maxScrollExtent, |
| 74 | + duration: const Duration(microseconds: 1), |
| 75 | + curve: Curves.linear, |
| 76 | + ); |
| 77 | + } |
| 78 | + if (mounted) setState(() {}); |
| 79 | + } |
| 80 | + |
| 81 | + double previousScrollPosition = 0.0; |
| 82 | + ScrollDirection scrollDirection = ScrollDirection.idle; |
| 83 | + void onScroll() { |
| 84 | + double currentScrollPosition = scrollController.position.pixels; |
| 85 | + if (currentScrollPosition > previousScrollPosition) { |
| 86 | + scrollDirection = ScrollDirection.forward; |
| 87 | + if (mounted) setState(() {}); |
| 88 | + } else if (currentScrollPosition < previousScrollPosition) { |
| 89 | + scrollDirection = ScrollDirection.reverse; |
| 90 | + if (mounted) setState(() {}); |
| 91 | + } |
| 92 | + |
| 93 | + previousScrollPosition = currentScrollPosition; |
| 94 | + } |
| 95 | + |
| 96 | + @override |
| 97 | + void initState() { |
| 98 | + scrollController.addListener(onScroll); |
| 99 | + if (mounted) setState(() {}); |
| 100 | + super.initState(); |
| 101 | + } |
| 102 | + |
| 103 | + @override |
| 104 | + Widget build(BuildContext context) { |
| 105 | + return Scaffold( |
| 106 | + appBar: AppBar(title: const Text('Chat with Gemini AI')), |
| 107 | + bottomNavigationBar: Padding( |
| 108 | + padding: EdgeInsets.only( |
| 109 | + left: 15, |
| 110 | + right: 15, |
| 111 | + bottom: MediaQuery.of(context).viewInsets.bottom, |
| 112 | + ), |
| 113 | + child: Padding( |
| 114 | + padding: const EdgeInsets.only(bottom: 20), |
| 115 | + child: Row( |
| 116 | + mainAxisSize: MainAxisSize.min, |
| 117 | + children: [ |
| 118 | + Expanded( |
| 119 | + child: TextFormField( |
| 120 | + maxLines: null, |
| 121 | + controller: chatController, |
| 122 | + keyboardType: TextInputType.multiline, |
| 123 | + onChanged: (value) => setState(() {}), |
| 124 | + textInputAction: TextInputAction.send, |
| 125 | + textCapitalization: TextCapitalization.sentences, |
| 126 | + decoration: InputDecoration( |
| 127 | + isDense: true, |
| 128 | + hintText: "Type a message", |
| 129 | + contentPadding: const EdgeInsets.all(12.0), |
| 130 | + enabledBorder: OutlineInputBorder( |
| 131 | + borderRadius: BorderRadius.circular(30), |
| 132 | + borderSide: const BorderSide(color: Colors.grey), |
| 133 | + ), |
| 134 | + focusedBorder: OutlineInputBorder( |
| 135 | + borderRadius: BorderRadius.circular(30), |
| 136 | + borderSide: const BorderSide(color: Colors.grey), |
| 137 | + ), |
| 138 | + ), |
| 139 | + ), |
| 140 | + ), |
| 141 | + if (chatController.text.isNotEmpty) |
| 142 | + Padding( |
| 143 | + padding: const EdgeInsets.only(left: 10), |
| 144 | + child: InkWell( |
| 145 | + onTap: () => sendMessage(), |
| 146 | + child: const CircleAvatar(child: Icon(Icons.send)), |
| 147 | + ), |
| 148 | + ), |
| 149 | + ], |
| 150 | + ), |
| 151 | + ), |
| 152 | + ), |
| 153 | + body: Padding( |
| 154 | + padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 20), |
| 155 | + child: chatHistory.isEmpty |
| 156 | + ? const Center( |
| 157 | + child: Text('Start conservating with Gemini...'), |
| 158 | + ) |
| 159 | + : ListView.builder( |
| 160 | + itemCount: chatHistory.length, |
| 161 | + controller: scrollController, |
| 162 | + padding: const EdgeInsets.only(top: 10, bottom: 10), |
| 163 | + physics: const BouncingScrollPhysics(), |
| 164 | + itemBuilder: (context, index) { |
| 165 | + return Container( |
| 166 | + padding: const EdgeInsets.only( |
| 167 | + left: 14, |
| 168 | + right: 14, |
| 169 | + top: 10, |
| 170 | + bottom: 10, |
| 171 | + ), |
| 172 | + child: Align( |
| 173 | + alignment: (chatHistory[index]["isSender"] |
| 174 | + ? Alignment.topRight |
| 175 | + : Alignment.topLeft), |
| 176 | + child: Container( |
| 177 | + padding: const EdgeInsets.all(16), |
| 178 | + decoration: BoxDecoration( |
| 179 | + borderRadius: BorderRadius.circular(20), |
| 180 | + boxShadow: [ |
| 181 | + BoxShadow( |
| 182 | + color: Colors.grey.withOpacity(0.5), |
| 183 | + spreadRadius: 2, |
| 184 | + blurRadius: 5, |
| 185 | + offset: const Offset(0, 3), |
| 186 | + ), |
| 187 | + ], |
| 188 | + color: chatHistory[index]["isSender"] |
| 189 | + ? Colors.pink |
| 190 | + : Colors.white, |
| 191 | + ), |
| 192 | + child: Text( |
| 193 | + chatHistory[index]["message"], |
| 194 | + style: TextStyle( |
| 195 | + fontSize: 15, |
| 196 | + color: chatHistory[index]["isSender"] |
| 197 | + ? Colors.white |
| 198 | + : Colors.black, |
| 199 | + ), |
| 200 | + ), |
| 201 | + ), |
| 202 | + ), |
| 203 | + ); |
| 204 | + }, |
| 205 | + ), |
| 206 | + ), |
| 207 | + ); |
| 208 | + } |
| 209 | +} |
0 commit comments