-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog_flow.dart
107 lines (98 loc) · 3.02 KB
/
dialog_flow.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
import 'package:flutter/material.dart';
import 'package:flutter_bot/facts_message.dart';
import 'package:flutter_dialogflow/dialogflow_v2.dart';
class FlutterFactsChatBot extends StatefulWidget {
FlutterFactsChatBot({Key key, this.title}) : super(key: key);
final String title;
@override
_FlutterFactsChatBotState createState() => new _FlutterFactsChatBotState();
}
class _FlutterFactsChatBotState extends State<FlutterFactsChatBot> {
final List<Facts> messageList = <Facts>[];
final TextEditingController _textController = new TextEditingController();
Widget _queryInputWidget(BuildContext context) {
return Card(
margin: EdgeInsets.all(10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(30))),
child: Padding(
padding: const EdgeInsets.only(left: 8.0, right: 8),
child: Row(
children: <Widget>[
Flexible(
child: TextField(
controller: _textController,
onSubmitted: _submitQuery,
decoration:
InputDecoration.collapsed(hintText: "Send a message"),
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 4.0),
child: IconButton(
icon: Icon(
Icons.send,
color: Colors.green[400],
),
onPressed: () => _submitQuery(_textController.text)),
),
],
),
),
);
}
void agentResponse(query) async {
_textController.clear();
AuthGoogle authGoogle =
await AuthGoogle(fileJson: "assets/firstagent-ywjr-6d05eca6f569.json")
.build();
Dialogflow dialogFlow =
Dialogflow(authGoogle: authGoogle, language: Language.english);
AIResponse response = await dialogFlow.detectIntent(query);
Facts message = Facts(
text: response.getMessage() ??
CardDialogflow(response.getListMessage()[0]).title,
name: "Flutter",
type: false,
);
setState(() {
messageList.insert(0, message);
});
}
void _submitQuery(String text) {
_textController.clear();
Facts message = new Facts(
text: text,
name: "User",
type: true,
);
setState(() {
messageList.insert(0, message);
});
agentResponse(text);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
"Flutter Facts",
style: TextStyle(color: Colors.green[400]),
),
backgroundColor: Colors.white,
elevation: 0,
),
body: Column(children: <Widget>[
Flexible(
child: ListView.builder(
padding: EdgeInsets.all(8.0),
reverse: true, //To keep the latest messages at the bottom
itemBuilder: (_, int index) => messageList[index],
itemCount: messageList.length,
)),
_queryInputWidget(context),
]),
);
}
}