11import 'package:adaptive_theme/adaptive_theme.dart' ;
22import 'package:flutter/material.dart' ;
33import 'package:google_generative_ai/google_generative_ai.dart' ;
4+ import 'package:flutter_dotenv/flutter_dotenv.dart' ;
45
5- // Replace with your actual Gemini API key
6- const apiKey = 'api-key' ;
6+ final String apiKey = dotenv.env['API_KEY' ] ?? '' ;
77
88class ChatBotPage extends StatefulWidget {
99 const ChatBotPage ({Key ? key}) : super (key: key);
@@ -23,28 +23,46 @@ class _ChatBotPageState extends State<ChatBotPage> {
2323 _initializeModel ();
2424 }
2525
26- void _initializeModel () async {
26+ void _initializeModel () {
27+
2728 _model = GenerativeModel (
28- model: 'gemini-1 .5-flash-latest ' ,
29+ model: 'gemini-2 .5-flash' ,
2930 apiKey: apiKey,
3031 );
3132 }
3233
3334 void _sendMessage () async {
34- if (_controller.text.isEmpty) return ;
35+ if (_controller.text.trim ().isEmpty) return ;
36+
37+ final userMessage = _controller.text.trim ();
3538
3639 setState (() {
37- _messages.add ({"sender" : "user" , "text" : _controller.text});
40+ _messages.add ({
41+ "sender" : "user" ,
42+ "text" : userMessage,
43+ });
3844 });
3945
40- final prompt = _controller.text + " don't give answer in markdown format" ;
4146 _controller.clear ();
47+ try {
48+ if (_model == null ) return ;
49+
50+ final response = await _model! .generateContent ([
51+ Content .text (userMessage),
52+ ]);
4253
43- if (_model != null ) {
44- final content = [Content .text (prompt)];
45- final response = await _model! .generateContent (content);
4654 setState (() {
47- _messages.add ({"sender" : "bot" , "text" : response.text! });
55+ _messages.add ({
56+ "sender" : "bot" ,
57+ "text" : response.text ?? "No response" ,
58+ });
59+ });
60+ } catch (e) {
61+ setState (() {
62+ _messages.add ({
63+ "sender" : "bot" ,
64+ "text" : "Error: $e " ,
65+ });
4866 });
4967 }
5068 }
@@ -53,9 +71,8 @@ class _ChatBotPageState extends State<ChatBotPage> {
5371 Widget build (BuildContext context) {
5472 return Scaffold (
5573 appBar: AppBar (
56- leading: IconButton (
74+ leading: IconButton (
5775 icon: const Icon (Icons .arrow_back_ios),
58-
5976 onPressed: () => Navigator .of (context).pop (),
6077 ),
6178 centerTitle: true ,
@@ -70,23 +87,30 @@ class _ChatBotPageState extends State<ChatBotPage> {
7087 final message = _messages[index];
7188 final isUser = message["sender" ] == "user" ;
7289 return Align (
73- alignment: isUser ? Alignment .centerRight : Alignment .centerLeft,
90+ alignment:
91+ isUser ? Alignment .centerRight : Alignment .centerLeft,
7492 child: Container (
7593 constraints: BoxConstraints (
7694 maxWidth: MediaQuery .of (context).size.width * 0.75 ,
7795 ),
78- margin: const EdgeInsets .symmetric (vertical: 5.0 , horizontal: 10.0 ),
96+ margin: const EdgeInsets .symmetric (
97+ vertical: 5.0 , horizontal: 10.0 ),
7998 padding: const EdgeInsets .all (10.0 ),
8099 decoration: BoxDecoration (
81100 color: isUser
82- ? (AdaptiveTheme .of (context).mode.isDark ? Color .fromARGB (255 , 31 , 49 , 70 ) : Colors .blue[100 ])
83- : (AdaptiveTheme .of (context).mode.isDark ? Colors .grey[700 ] : Colors .grey[300 ]),
101+ ? (AdaptiveTheme .of (context).mode.isDark
102+ ? Color .fromARGB (255 , 31 , 49 , 70 )
103+ : Colors .blue[100 ])
104+ : (AdaptiveTheme .of (context).mode.isDark
105+ ? Colors .grey[700 ]
106+ : Colors .grey[300 ]),
84107 borderRadius: BorderRadius .circular (10.0 ),
85108 ),
86109 child: Row (
87110 crossAxisAlignment: CrossAxisAlignment .start,
88111 children: [
89- if (! isUser) const CircleAvatar (child: Icon (Icons .android)),
112+ if (! isUser)
113+ const CircleAvatar (child: Icon (Icons .android)),
90114 if (! isUser) const SizedBox (width: 5.0 ),
91115 Expanded (
92116 child: Column (
@@ -97,7 +121,8 @@ class _ChatBotPageState extends State<ChatBotPage> {
97121 ),
98122 ),
99123 if (isUser) const SizedBox (width: 5.0 ),
100- if (isUser) const CircleAvatar (child: Icon (Icons .person)),
124+ if (isUser)
125+ const CircleAvatar (child: Icon (Icons .person)),
101126 ],
102127 ),
103128 ),
@@ -130,17 +155,15 @@ class _ChatBotPageState extends State<ChatBotPage> {
130155 );
131156 }
132157
133-
134158 @override
135159 void dispose () {
136160 _controller.dispose ();
137161 super .dispose ();
138162 }
139163}
140164
141-
142165void main () {
143166 runApp (const MaterialApp (
144167 home: ChatBotPage (),
145168 ));
146- }
169+ }
0 commit comments