-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
125 lines (101 loc) · 3.25 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import 'package:chatbot_app/message.dart';
import 'package:dialog_flowtter/dialog_flowtter.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Chatbot() ,
);
}
}
class Chatbot extends StatefulWidget {
Chatbot({super.key});
@override
State<Chatbot> createState() => _ChatbotState();
}
class _ChatbotState extends State<Chatbot> {
late DialogFlowtter dialogFlowtter;
final TextEditingController controller=TextEditingController();
void initState() {
DialogFlowtter.fromFile().then((instance) => dialogFlowtter = instance);
super.initState();
}
List<Map<String, dynamic>> messages = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Finance Bot"),
backgroundColor: Color.fromARGB(255, 65, 118, 197)
),
body: Container(
child: Column(
children: [
Expanded(child: Messagescreen(messages: messages)),
Row(
children: [
SizedBox(width: 5,),
Expanded(
child: Material(
elevation: 40,
borderRadius: BorderRadius.circular(25),
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20,vertical: 5),
child: TextField(
style: TextStyle(fontSize: 20),
controller: controller,
cursorColor: Color.fromARGB(255, 65, 118, 197) ,
decoration: InputDecoration(
hintText: "Type message here",
hintStyle: TextStyle(fontSize: 20),
focusColor: Color.fromARGB(255, 65, 118, 197),
border: InputBorder.none
),
),
),
),
),
SizedBox(width: 5,),
IconButton(
onPressed: () {
sendMessage(controller.text);
controller.clear();
},
icon: Icon(Icons.send,color: Color.fromARGB(255, 65, 118, 197),)),
],
),
SizedBox(height: 7,)
],
),
),
);
}
void sendMessage(String text) async {
if(text.isEmpty){
print("Wow so empty");
}
else{
setState(() {
addMessage(Message(text: DialogText(text: [text])), true);
});
DetectIntentResponse response=await dialogFlowtter.detectIntent(queryInput: QueryInput(text: TextInput(text: text)));
if(response==null)return;
setState(() {
addMessage(response.message!);
});
}
}
void addMessage(Message message, [bool isUserMessage = false]) {
messages.add({'message': message, 'isUserMessage': isUserMessage});
}
}