-
Notifications
You must be signed in to change notification settings - Fork 1
/
appointment_page.dart
470 lines (428 loc) · 11.5 KB
/
appointment_page.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(AppointmentPage());
}
class AppointmentPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ChatScreen(),
);
}
}
class ChatScreen extends StatefulWidget {
@override
State createState() => ChatScreenState();
}
class ChatScreenState extends State<ChatScreen> {
final List<ChatMessage> _messages = <ChatMessage>[];
final TextEditingController _textController = TextEditingController();
int step = 1;
String _userName = '';
String _userAge = '';
String _userGender = '';
String _userHeight = '';
List<String> userSymptoms = [];
List<dynamic> symptomSpecialistData = [];
@override
void initState() {
super.initState();
_botGreeting();
_loadSymptomSpecialistData(); // Load symptom-specialist mapping data
}
Future<void> _loadSymptomSpecialistData() async {
try {
final String data =
await rootBundle.loadString('assets/symptom_specialist_mapping.json');
setState(() {
symptomSpecialistData = json.decode(data)['symptomSpecialistMapping'];
});
} catch (e) {
print('Error loading JSON data: $e');
}
}
Future<void> _botGreeting() async {
await Future.delayed(Duration(milliseconds: 500));
setState(() {
_messages.insert(
0,
ChatMessage(
text: "Hello! How are you doing today?",
isUser: false,
),
);
});
_botProvideOptions();
}
void _botProvideOptions() {
setState(() {
_messages.insert(
0,
ChatMessage(
text: "1. Doing Great\n2. Not feeling so good",
isUser: false,
),
);
});
}
void _handleUserResponse(String text) async {
_textController.clear();
setState(() {
_messages.insert(
0,
ChatMessage(
text: text,
isUser: true,
),
);
});
text = _capitalizeFirstLetterOfEachWord(
text); // Convert the first letter of each word to uppercase
if (step == 1) {
bool containsNegativeKeyword = false;
final List<String> words = text.split(' ');
// Check each word for negative keywords
for (String word in words) {
if (word.contains('no') ||
word.contains("i don't") ||
word.contains('not') ||
word.contains('problem') ||
word.contains('Problem') ||
word.contains('Not') ||
word.contains('No') ||
word.contains('I don' 't') ||
word.contains('nope')) {
containsNegativeKeyword = true;
break; // Stop checking if a negative keyword is found
}
}
if (containsNegativeKeyword) {
// Display a reassuring message
_messages.insert(
0,
ChatMessage(
text: "No worries, I am here to help. Let's proceed with...",
isUser: false,
),
);
_botAskName(); // Proceed with the second option
step = 2;
} else {
_botProvideOptions(); // Proceed with the first option
}
} else if (step == 2) {
_handleUserName(text);
} else if (step == 3) {
_handleUserAge(text);
} else if (step == 4) {
_handleUserGender(text);
} else if (step == 5) {
_handleUserHeight(text);
} else if (step == 6) {
_handleUserWeight(text);
} else if (step == 7) {
_handleUserSymptoms(text);
}
}
String _capitalizeFirstLetterOfEachWord(String input) {
List<String> words = input.split(' ');
List<String> capitalizedWords = [];
for (String word in words) {
if (word.isNotEmpty) {
capitalizedWords.add('${word[0].toUpperCase()}${word.substring(1)}');
}
}
return capitalizedWords.join(' ');
}
void _botAskName() {
setState(() {
_messages.insert(
0,
ChatMessage(
text: "Please enter your name.",
isUser: false,
),
);
});
}
void _handleUserName(String name) {
setState(() {
_userName = name;
_messages.insert(
0,
ChatMessage(
text: "Your name is: $name",
isUser: false,
),
);
});
_botAskAge();
step = 3;
}
void _botAskAge() {
setState(() {
_messages.insert(
0,
ChatMessage(
text: "Please enter your age.",
isUser: false,
),
);
});
}
void _handleUserAge(String age) {
setState(() {
_userAge = age;
_messages.insert(
0,
ChatMessage(
text: "Your age is: $age",
isUser: false,
),
);
});
_botAskGender();
step = 4;
}
void _botAskGender() {
setState(() {
_messages.insert(
0,
ChatMessage(
text: "Please enter your gender.",
isUser: false,
),
);
});
}
void _handleUserGender(String gender) {
setState(() {
_userGender = gender;
_messages.insert(
0,
ChatMessage(
text: "Your gender is: $gender",
isUser: false,
),
);
});
_botAskHeight();
step = 5;
}
void _botAskHeight() {
setState(() {
_messages.insert(
0,
ChatMessage(
text: "Please enter your height (in cms).",
isUser: false,
),
);
});
}
void _handleUserHeight(String height) {
setState(() {
_userHeight = height;
_messages.insert(
0,
ChatMessage(
text: "Your height is: $height cm",
isUser: false,
),
);
});
_botAskWeight();
step = 6;
}
void _botAskWeight() {
setState(() {
_messages.insert(
0,
ChatMessage(
text: "Please enter your weight (in kgs).",
isUser: false,
),
);
});
}
void _handleUserWeight(String weight) async {
final prefs = await SharedPreferences.getInstance();
final userJson = {
'Name': _userName,
'Age': _userAge,
'Gender': _userGender,
'Height': _userHeight,
'Weight': weight,
};
await prefs.setString('userDetails', json.encode(userJson));
setState(() {
_messages.insert(
0,
ChatMessage(
text:
"Your weight is: $weight kg. Thank you! Your information has been saved.",
isUser: false,
),
);
});
// Continue the conversation or perform other actions here
_botAskSymptoms();
step = 7;
}
void _botAskSymptoms() {
setState(() {
_messages.insert(
0,
ChatMessage(
text:
"Great! Now can you tell me your symptoms? (Please separate multiple symptoms with commas)",
isUser: false,
),
);
});
}
void _handleUserSymptoms(String symptomsText) {
final List<String> symptoms = symptomsText.split(',');
userSymptoms = symptoms.map((symptom) => symptom.trim()).toList();
if (userSymptoms.isEmpty) {
_botAskSymptoms(); // Ask again if no symptoms provided
} else {
final List<String> specialists = matchSymptomsToSpecialists(userSymptoms);
_botRecommendSpecialists(specialists);
}
}
List<String> matchSymptomsToSpecialists(List<String> symptoms) {
List<String> specialists = [];
for (String symptom in symptoms) {
for (var mapping in symptomSpecialistData) {
if (mapping['symptom'].toLowerCase() == symptom.toLowerCase()) {
// Cast the elements in mapping['specialists'] to strings
List<dynamic> specialistList = mapping['specialists'];
specialists.addAll(
specialistList.map((specialist) => specialist.toString()));
}
}
}
return specialists.toSet().toList(); // Remove duplicates
}
void _botRecommendSpecialists(List<String> specialists) {
setState(() {
if (specialists.isNotEmpty) {
_messages.insert(
0,
ChatMessage(
text:
"Based on your symptoms, you may consider consulting the following specialist(s):\n${specialists.join(', ')}",
isUser: false,
),
);
} else {
_messages.insert(
0,
ChatMessage(
text:
"I couldn't find any specialists based on your symptoms. Please provide more details or consult a general physician.",
isUser: false,
),
);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Healthcare Chatbot'),
),
body: Column(
children: <Widget>[
Flexible(
child: ListView.builder(
padding: EdgeInsets.all(8.0),
reverse: true,
itemCount: _messages.length,
itemBuilder: (_, int index) => _messages[index],
),
),
Divider(height: 1.0),
Container(
decoration: BoxDecoration(color: Theme.of(context).cardColor),
child: _buildTextComposer(),
),
],
),
);
}
Widget _buildTextComposer() {
return IconTheme(
data: IconThemeData(color: Colors.blue),
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
child: Row(
children: <Widget>[
Flexible(
child: TextField(
controller: _textController,
onSubmitted: _handleUserResponse,
decoration:
InputDecoration.collapsed(hintText: 'Send a message'),
),
),
IconButton(
icon: Icon(Icons.send),
onPressed: () => _handleUserResponse(_textController.text),
),
],
),
),
);
}
}
class ChatMessage extends StatelessWidget {
final String text;
final bool isUser;
ChatMessage({required this.text, required this.isUser});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(vertical: 10.0),
child: Row(
mainAxisAlignment:
isUser ? MainAxisAlignment.end : MainAxisAlignment.start,
children: <Widget>[
Expanded(
// Wrap the message with an Expanded widget
child: Container(
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: isUser ? Colors.blueAccent : Colors.greenAccent,
borderRadius: isUser
? BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
bottomLeft: Radius.circular(20.0),
)
: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
bottomRight: Radius.circular(20.0),
),
),
child: Text(
text,
style: TextStyle(
color: Colors.white,
fontSize: 16, // Adjust the font size as needed
fontWeight: FontWeight.bold, // Make text bold
),
),
),
),
],
),
);
}
}