-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchat.dart
274 lines (252 loc) · 7.62 KB
/
chat.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import 'package:bubble/bubble.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
//Chat message
final List<ChatMessage> _messages = <ChatMessage>[];
//text type
final TextEditingController _textController = new TextEditingController();
Widget _buildTextComposer() {
return new IconTheme(
data: new IconThemeData(color: Theme.of(context).accentColor),
child: new Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
//input field
child: new Row(
children: <Widget>[
new Flexible(
//input text fields
child: new TextField(
//input text controller
controller: _textController,
// onSubmitted: _handleSubmitted,
decoration:
new InputDecoration.collapsed(hintText: "Send a message"),
),
),
//container for send button
new Container(
margin: new EdgeInsets.symmetric(horizontal: 4.0),
child: new IconButton(
icon: new Icon(Icons.send),
onPressed: () {
_handleSubmitted(_textController.text);
},
),
),
],
),
),
);
}
void _handleSubmitted(String text) {
_textController.clear();
ChatMessage message = new ChatMessage(
text: text,
//user name
name: "Jaffrin",
type: true,
);
setState(() {
//initialize in top
_messages.insert(0, message);
});
response(text);
}
void response(message) async {
//api of hashbot in PHP
var url = Uri.parse('https://looptune.com/api/hashbot.php');
// req waiting for answer from server Query input message from text field
var body = {"req": request, "query": message};
var response = await http.post(
url,
//convert the req into json type
body: json.encode(body),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
var data = json.decode(response.body);
ChatMessage msg = new ChatMessage(
text: data["response"],
//bot name
name: "HashBot",
type: false,
);
setState(() {
_messages.insert(0, msg);
request = data['for'];
});
}
var request = "normal";
final messageInsert = TextEditingController();
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
title: Center(
child: Text(
"Hashbot",
),
),
),
body: new Column(children: <Widget>[
new Flexible(
child: new ListView.builder(
padding: new EdgeInsets.all(8.0),
reverse: true,
itemBuilder: (_, int index) => _messages[index],
itemCount: _messages.length,
)),
new Divider(height: 1.0),
new Container(
decoration: new BoxDecoration(color: Theme.of(context).cardColor),
child: _buildTextComposer(),
),
]),
);
}
}
class ChatMessage extends StatelessWidget {
ChatMessage({this.text, this.name, this.type});
final String text;
final String name;
final bool type;
List<Widget> otherMessage(context) {
return <Widget>[
/*new Container(
margin: const EdgeInsets.only(right: 16.0),
child:
new CircleAvatar(child: new Image.asset("assets/images/robot.png")),
),*/
new Expanded(
child: Bubble(
nip: BubbleNip.leftTop,
alignment: Alignment.topLeft,
color: Colors.white,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(this.name,
style: new TextStyle(fontWeight: FontWeight.bold)),
new Container(
margin: const EdgeInsets.only(top: 5.0),
child: new Text(text),
),
],
),
),
),
];
}
List<Widget> myMessage(context) {
return <Widget>[
new Expanded(
child: Bubble(
alignment: Alignment.topRight,
nip: BubbleNip.rightTop,
color: Colors.blue[50],
child: new Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
new Text(this.name, style: Theme.of(context).textTheme.subhead),
new Container(
margin: const EdgeInsets.only(top: 5.0),
child: new Text(text),
),
],
),
),
),
new Container(
margin: const EdgeInsets.only(left: 16.0),
child: new CircleAvatar(child: new Text(this.name[0])),
),
];
}
@override
Widget build(BuildContext context) {
return new Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: new Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: this.type ? myMessage(context) : otherMessage(context),
),
);
}
}
/*import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
StreamController<List> _streamController = StreamController<List>();
Timer _timer;
Future getData() async {
/*var url = 'https://rayen.khomsafm.com/s.php';
http.Response response = await http.get(Uri.parse(url));
*/
var url = Uri.parse('https://rayen.khomsafm.com/s.php');
var response = await http.get(url);
print('Response status: ${response.statusCode}');
print('Response body: ${json.decode(response.body)}');
List data = json.decode(response.body);
//print(await http.read('https://example.com/foobar.txt'));
//Add your data to stream
//_streamController.add(data);
}
@override
void initState() {
getData();
//Check the server every 5 seconds
_timer = Timer.periodic(Duration(seconds: 5), (timer) => getData());
super.initState();
}
@override
void dispose() {
//cancel the timer
if (_timer.isActive) _timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
getData();
return Scaffold(
appBar: AppBar(
title: Text("widget.title"),
centerTitle: true,
),
body: StreamBuilder<List>(
stream: _streamController.stream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData)
return ListView(
children: [
for (Map document in snapshot.data)
ListTile(
title: Text(document['title']),
subtitle: Text(document['type']),
),
],
);
return Text('Loading...');
},
),
);
}
}
*/