1+ import 'package:chat_bubbles/bubbles/bubble_normal.dart' ;
2+ import 'package:chat_bubbles/message_bars/message_bar.dart' ;
3+ import 'package:flutter/material.dart' ;
4+
5+ class ChatBotPage extends StatefulWidget {
6+ @override
7+ State <StatefulWidget > createState () => _ChatBotPageState ();
8+
9+ }
10+
11+ class _ChatBotPageState extends State <ChatBotPage > {
12+
13+ var chatList = < Widget > [];
14+
15+ final ScrollController _scrollController = ScrollController ();
16+
17+ @override
18+ void dispose () {
19+ _scrollController.dispose ();
20+ super .dispose ();
21+ }
22+
23+ void handleChatSend (String msg) {
24+ final sentChat = BubbleNormal (
25+ text: msg,
26+ isSender: true ,
27+ color: Colors .purple,
28+ textStyle: TextStyle (
29+ fontSize: 18 ,
30+ color: Colors .white
31+ ),
32+ );
33+
34+ final responseChat = BubbleNormal (
35+ text: "nodakke yenu illa backendalli" , // TODO get response from backend
36+ color: Colors .grey.shade300,
37+ isSender: false ,
38+ textStyle: TextStyle (
39+ fontSize: 18
40+ ),
41+ );
42+
43+ chatList.add (sentChat);
44+ chatList.add (SizedBox (height: 8 ,));
45+ chatList.add (responseChat);
46+ chatList.add (SizedBox (height: 8 ,));
47+ }
48+
49+ @override
50+ Widget build (BuildContext context) {
51+ return Scaffold (
52+ appBar: AppBar (
53+ centerTitle: true ,
54+ title: Text ("Library chatbot" )
55+ ),
56+ body: Column (
57+ children: [
58+ Expanded (
59+ child: SingleChildScrollView (
60+ controller: _scrollController,
61+ child: Column (
62+ children: chatList,
63+ ),
64+ ),
65+ ),
66+ CustomMessageBar (
67+ onSend: (msg) {
68+ setState (() {
69+ handleChatSend (msg);
70+ });
71+ WidgetsBinding .instance.addPostFrameCallback ((_) {
72+ _scrollController.animateTo (
73+ _scrollController.position.maxScrollExtent,
74+ duration: Duration (milliseconds: 200 ),
75+ curve: Curves .easeOut,
76+ );
77+ });
78+ },
79+ )
80+ ],
81+ )
82+ );
83+ }
84+
85+ }
86+
87+ class CustomMessageBar extends MessageBar {
88+ final TextEditingController _textController = TextEditingController ();
89+
90+ CustomMessageBar ({super .onSend});
91+
92+ @override
93+ Widget build (BuildContext context) {
94+ return Align (
95+ alignment: Alignment .bottomCenter,
96+ child: Container (
97+ child: Row (
98+ children: [
99+ Expanded (
100+ child: Padding (
101+ padding: EdgeInsets .all (12 ),
102+ child: Material (
103+ elevation: 2 ,
104+ borderRadius: BorderRadius .circular (28 ),
105+ child: TextField (
106+ controller: _textController,
107+ keyboardType: TextInputType .multiline,
108+ onChanged: super .onTextChanged,
109+ decoration: InputDecoration (
110+ suffixIcon: Padding (
111+ padding: EdgeInsets .only (right: 4 ),
112+ child: IconButton (
113+ onPressed: () {
114+ if (_textController.text.trim () != '' ) {
115+ if (onSend != null ) {
116+ onSend !(_textController.text.trim ());
117+ }
118+ _textController.text = '' ;
119+ }
120+ },
121+ icon: Icon (Icons .send_outlined),
122+ ),
123+ ),
124+ filled: true ,
125+ hintText: "" ,
126+ contentPadding: const EdgeInsets .symmetric (
127+ horizontal: 12.0 ,
128+ vertical: 14.0 ,
129+ ),
130+ fillColor: Colors .purple.shade50,
131+ border: OutlineInputBorder (
132+ borderRadius: BorderRadius .circular (28 ),
133+ borderSide: BorderSide .none
134+ )
135+
136+ ),
137+
138+ ),
139+ )
140+ )
141+ )
142+ ],
143+ ),
144+ )
145+ );
146+ }
147+ }
0 commit comments