LifeMate — Personal, Family & Learning Companion
feat: build interactive phase 4 guide and wellbeing UI
davoodmehraban89 committed
Oct 5, 2026 at 16:15 UTC
dcbaedba84e333f80a715828868a641620c7c1c4
1 file changed
+459
-11
apps/lifemate/lib/phase4_ui.dart
+459
-11
@@ -1,14 +1,462 @@
1
import 'package:flutter/material.dart';
2
3
-class Phase4Hub extends StatelessWidget {
4
- const Phase4Hub({super.key});
5
- @override Widget build(BuildContext context)=>Scaffold(appBar:AppBar(title:const Text('راهنمای هوشمند')),body:ListView(padding:const EdgeInsets.all(20),children:[
6
- const Text('یادگیری و حال خوب',style:TextStyle(fontSize:28,fontWeight:FontWeight.w700)),const SizedBox(height:8),
7
- const Text('راهنماها پیشنهاد میدهند؛ تصمیم و اجرای تغییرها با خودت است.'),const SizedBox(height:20),
8
- _card(context,Icons.school_rounded,'راهنمای مطالعه','هدف مطالعه، مرور فعال و برنامههای کوتاه و قابل انجام.'),
9
- _card(context,Icons.auto_awesome_rounded,'راهنمای برنامهریزی','سه اولویت، شکستن کارها و پیشنهاد برنامه بدون تغییر خودکار.'),
10
- _card(context,Icons.self_improvement_rounded,'حال خوب','ثبت خصوصی حال، انرژی و استرس و پیشنهادهای حمایتی؛ نه تشخیص پزشکی.'),
11
- const Card(child:Padding(padding:EdgeInsets.all(16),child:Text('حریم خصوصی: گفتوگوهای راهنما و یادداشتهای حال خوب خصوصیاند. عضویت خانواده بهتنهایی دسترسی به متن آنها ایجاد نمیکند.'))),
12
- ]));
13
- Widget _card(BuildContext c,IconData icon,String title,String subtitle)=>Card(child:ListTile(leading:Icon(icon),title:Text(title),subtitle:Text(subtitle),trailing:const Icon(Icons.chevron_left_rounded)));
3
+import 'api.dart';
4
+
5
+class Phase4Hub extends StatefulWidget {
6
+ const Phase4Hub({super.key, required this.api, this.adultShell = false});
7
+ final IdentityApi api;
8
+ final bool adultShell;
9
+
10
+ @override
11
+ State<Phase4Hub> createState() => _Phase4HubState();
12
+}
13
+
14
+class _Phase4HubState extends State<Phase4Hub> {
15
+ late Future<List<Map<String, dynamic>>> goals;
16
+
17
+ @override
18
+ void initState() {
19
+ super.initState();
20
+ goals = widget.api.listLearningGoals();
21
+ }
22
+
23
+ Future<void> addGoal() async {
24
+ final title = TextEditingController();
25
+ final target = TextEditingController();
26
+ final saved = await showDialog<bool>(
27
+ context: context,
28
+ builder: (dialogContext) => AlertDialog(
29
+ title: const Text('هدف یادگیری جدید'),
30
+ content: Column(
31
+ mainAxisSize: MainAxisSize.min,
32
+ children: [
33
+ TextField(
34
+ controller: title,
35
+ decoration: const InputDecoration(labelText: 'عنوان'),
36
+ ),
37
+ const SizedBox(height: 12),
38
+ TextField(
39
+ controller: target,
40
+ decoration: const InputDecoration(labelText: 'هدف یا نتیجه مطلوب'),
41
+ ),
42
+ ],
43
+ ),
44
+ actions: [
45
+ TextButton(
46
+ onPressed: () => Navigator.pop(dialogContext, false),
47
+ child: const Text('انصراف'),
48
+ ),
49
+ FilledButton(
50
+ onPressed: () async {
51
+ if (title.text.trim().isEmpty) return;
52
+ await widget.api.createLearningGoal(
53
+ title: title.text.trim(),
54
+ target: target.text.trim().isEmpty ? null : target.text.trim(),
55
+ );
56
+ if (dialogContext.mounted) Navigator.pop(dialogContext, true);
57
+ },
58
+ child: const Text('ذخیره'),
59
+ ),
60
+ ],
61
+ ),
62
+ );
63
+ title.dispose();
64
+ target.dispose();
65
+ if (saved == true && mounted) {
66
+ setState(() => goals = widget.api.listLearningGoals());
67
+ }
68
+ }
69
+
70
+ Future<void> wellbeingCheckin() async {
71
+ var mood = 3;
72
+ var energy = 3;
73
+ var stress = 3;
74
+ var visibility = 'private';
75
+ final note = TextEditingController();
76
+
77
+ final saved = await showDialog<bool>(
78
+ context: context,
79
+ builder: (dialogContext) => StatefulBuilder(
80
+ builder: (context, setLocalState) => AlertDialog(
81
+ title: const Text('ثبت حال امروز'),
82
+ content: SingleChildScrollView(
83
+ child: Column(
84
+ mainAxisSize: MainAxisSize.min,
85
+ children: [
86
+ _ScaleRow(
87
+ label: 'حال کلی',
88
+ value: mood,
89
+ onChanged: (v) => setLocalState(() => mood = v),
90
+ ),
91
+ _ScaleRow(
92
+ label: 'انرژی',
93
+ value: energy,
94
+ onChanged: (v) => setLocalState(() => energy = v),
95
+ ),
96
+ _ScaleRow(
97
+ label: 'استرس',
98
+ value: stress,
99
+ onChanged: (v) => setLocalState(() => stress = v),
100
+ ),
101
+ const SizedBox(height: 12),
102
+ TextField(
103
+ controller: note,
104
+ maxLines: 3,
105
+ decoration: const InputDecoration(
106
+ labelText: 'یادداشت خصوصی (اختیاری)',
107
+ ),
108
+ ),
109
+ const SizedBox(height: 12),
110
+ DropdownButtonFormField<String>(
111
+ initialValue: visibility,
112
+ decoration: const InputDecoration(labelText: 'اشتراک وضعیت'),
113
+ items: const [
114
+ DropdownMenuItem(
115
+ value: 'private',
116
+ child: Text('کاملاً خصوصی'),
117
+ ),
118
+ DropdownMenuItem(
119
+ value: 'guardian_summary',
120
+ child: Text('فقط در خلاصه آماری والد'),
121
+ ),
122
+ ],
123
+ onChanged: (v) {
124
+ if (v != null) setLocalState(() => visibility = v);
125
+ },
126
+ ),
127
+ ],
128
+ ),
129
+ ),
130
+ actions: [
131
+ TextButton(
132
+ onPressed: () => Navigator.pop(dialogContext, false),
133
+ child: const Text('انصراف'),
134
+ ),
135
+ FilledButton(
136
+ onPressed: () async {
137
+ await widget.api.createWellbeingCheckin(
138
+ mood: mood,
139
+ energy: energy,
140
+ stress: stress,
141
+ note: note.text.trim().isEmpty ? null : note.text.trim(),
142
+ visibility: visibility,
143
+ );
144
+ if (dialogContext.mounted) Navigator.pop(dialogContext, true);
145
+ },
146
+ child: const Text('ثبت'),
147
+ ),
148
+ ],
149
+ ),
150
+ ),
151
+ );
152
+ note.dispose();
153
+ if (saved == true && mounted) {
154
+ ScaffoldMessenger.of(context).showSnackBar(
155
+ const SnackBar(content: Text('ثبت شد. متن یادداشت خصوصی باقی میماند.')),
156
+ );
157
+ }
158
+ }
159
+
160
+ void openGuide(String kind, String title) {
161
+ Navigator.of(context).push(
162
+ MaterialPageRoute(
163
+ builder: (_) => GuideChatPage(
164
+ api: widget.api,
165
+ kind: kind,
166
+ title: title,
167
+ ),
168
+ ),
169
+ );
170
+ }
171
+
172
+ @override
173
+ Widget build(BuildContext context) => ListView(
174
+ padding: const EdgeInsets.all(16),
175
+ children: [
176
+ Text(
177
+ widget.adultShell ? 'راهنمای هوشمند' : 'یادگیری و حال خوب',
178
+ style: Theme.of(context).textTheme.headlineSmall,
179
+ ),
180
+ const SizedBox(height: 6),
181
+ const Text(
182
+ 'راهنماها پیشنهاد میدهند؛ تصمیم و اجرای تغییرها با خودت است.',
183
+ ),
184
+ const SizedBox(height: 16),
185
+ _GuideCard(
186
+ icon: Icons.school_rounded,
187
+ title: 'راهنمای مطالعه',
188
+ subtitle: 'فهم بهتر، مرور فعال و برنامه مطالعه مرحلهبهمرحله.',
189
+ onTap: () => openGuide('study', 'راهنمای مطالعه'),
190
+ ),
191
+ _GuideCard(
192
+ icon: Icons.auto_awesome_rounded,
193
+ title: 'راهنمای برنامهریزی',
194
+ subtitle: 'اولویتبندی و شکستن کارها بدون تغییر خودکار برنامه.',
195
+ onTap: () => openGuide('planner', 'راهنمای برنامهریزی'),
196
+ ),
197
+ _GuideCard(
198
+ icon: Icons.self_improvement_rounded,
199
+ title: 'همراه حال خوب',
200
+ subtitle: 'گفتوگوی حمایتی و غیرتشخیصی برای حال، ارتباط و فشار روزمره.',
201
+ onTap: () => openGuide('wellbeing', 'همراه حال خوب'),
202
+ ),
203
+ const SizedBox(height: 8),
204
+ Row(
205
+ children: [
206
+ Expanded(
207
+ child: FilledButton.icon(
208
+ onPressed: wellbeingCheckin,
209
+ icon: const Icon(Icons.favorite_outline),
210
+ label: const Text('ثبت حال امروز'),
211
+ ),
212
+ ),
213
+ const SizedBox(width: 8),
214
+ Expanded(
215
+ child: OutlinedButton.icon(
216
+ onPressed: addGoal,
217
+ icon: const Icon(Icons.flag_outlined),
218
+ label: const Text('هدف یادگیری'),
219
+ ),
220
+ ),
221
+ ],
222
+ ),
223
+ const SizedBox(height: 16),
224
+ const Card(
225
+ child: Padding(
226
+ padding: EdgeInsets.all(16),
227
+ child: Text(
228
+ 'حریم خصوصی: متن گفتوگو و یادداشت حال خوب برای خود کاربر است. '
229
+ 'والد فقط خلاصهای را میبیند که مجاز شده باشد؛ در وضعیت ایمنی جدی، '
230
+ 'سیگنال ایمنی جدا از متن خصوصی مدیریت میشود.',
231
+ ),
232
+ ),
233
+ ),
234
+ const SizedBox(height: 12),
235
+ Text('هدفهای یادگیری', style: Theme.of(context).textTheme.titleMedium),
236
+ FutureBuilder<List<Map<String, dynamic>>>(
237
+ future: goals,
238
+ builder: (context, snapshot) {
239
+ if (!snapshot.hasData) {
240
+ return const Padding(
241
+ padding: EdgeInsets.all(20),
242
+ child: Center(child: CircularProgressIndicator()),
243
+ );
244
+ }
245
+ if (snapshot.data!.isEmpty) {
246
+ return const Card(
247
+ child: ListTile(
248
+ leading: Icon(Icons.flag_outlined),
249
+ title: Text('هنوز هدفی ثبت نشده'),
250
+ ),
251
+ );
252
+ }
253
+ return Column(
254
+ children: snapshot.data!
255
+ .map(
256
+ (goal) => Card(
257
+ child: ListTile(
258
+ leading: const Icon(Icons.flag_circle_outlined),
259
+ title: Text(goal['title']?.toString() ?? ''),
260
+ subtitle: Text(goal['target']?.toString() ?? ''),
261
+ ),
262
+ ),
263
+ )
264
+ .toList(),
265
+ );
266
+ },
267
+ ),
268
+ ],
269
+ );
270
+}
271
+
272
+class GuideChatPage extends StatefulWidget {
273
+ const GuideChatPage({
274
+ super.key,
275
+ required this.api,
276
+ required this.kind,
277
+ required this.title,
278
+ });
279
+
280
+ final IdentityApi api;
281
+ final String kind;
282
+ final String title;
283
+
284
+ @override
285
+ State<GuideChatPage> createState() => _GuideChatPageState();
286
+}
287
+
288
+class _GuideChatPageState extends State<GuideChatPage> {
289
+ final input = TextEditingController();
290
+ final messages = <Map<String, String>>[];
291
+ String? sessionId;
292
+ bool busy = false;
293
+
294
+ Future<void> send() async {
295
+ final text = input.text.trim();
296
+ if (text.isEmpty || busy) return;
297
+ setState(() {
298
+ busy = true;
299
+ messages.add({'role': 'user', 'body': text});
300
+ input.clear();
301
+ });
302
+ try {
303
+ final session = sessionId == null
304
+ ? await widget.api.createAiSession(widget.kind)
305
+ : null;
306
+ sessionId ??= session?['id']?.toString();
307
+ if (sessionId == null) throw StateError('session_not_created');
308
+ final reply = await widget.api.sendAiMessage(sessionId!, text);
309
+ if (mounted) {
310
+ setState(() {
311
+ messages.add({
312
+ 'role': 'assistant',
313
+ 'body': reply['body']?.toString() ?? '',
314
+ });
315
+ });
316
+ }
317
+ } catch (_) {
318
+ if (mounted) {
319
+ setState(() {
320
+ messages.add({
321
+ 'role': 'assistant',
322
+ 'body': 'ارسال انجام نشد. دوباره تلاش کن.',
323
+ });
324
+ });
325
+ }
326
+ } finally {
327
+ if (mounted) setState(() => busy = false);
328
+ }
329
+ }
330
+
331
+ @override
332
+ void dispose() {
333
+ input.dispose();
334
+ super.dispose();
335
+ }
336
+
337
+ @override
338
+ Widget build(BuildContext context) => Scaffold(
339
+ appBar: AppBar(title: Text(widget.title)),
340
+ body: Column(
341
+ children: [
342
+ const Padding(
343
+ padding: EdgeInsets.fromLTRB(16, 8, 16, 4),
344
+ child: Text(
345
+ 'این راهنما جای پزشک، روانشناس یا خدمات اضطراری نیست و تشخیص پزشکی نمیدهد.',
346
+ textAlign: TextAlign.center,
347
+ ),
348
+ ),
349
+ Expanded(
350
+ child: ListView.builder(
351
+ padding: const EdgeInsets.all(16),
352
+ itemCount: messages.length,
353
+ itemBuilder: (context, index) {
354
+ final item = messages[index];
355
+ final mine = item['role'] == 'user';
356
+ return Align(
357
+ alignment:
358
+ mine ? Alignment.centerRight : Alignment.centerLeft,
359
+ child: Card(
360
+ child: Padding(
361
+ padding: const EdgeInsets.all(12),
362
+ child: ConstrainedBox(
363
+ constraints: const BoxConstraints(maxWidth: 560),
364
+ child: Text(item['body'] ?? ''),
365
+ ),
366
+ ),
367
+ ),
368
+ );
369
+ },
370
+ ),
371
+ ),
372
+ SafeArea(
373
+ top: false,
374
+ child: Padding(
375
+ padding: const EdgeInsets.all(12),
376
+ child: Row(
377
+ children: [
378
+ Expanded(
379
+ child: TextField(
380
+ controller: input,
381
+ minLines: 1,
382
+ maxLines: 4,
383
+ onSubmitted: (_) => send(),
384
+ decoration: const InputDecoration(
385
+ hintText: 'اینجا بنویس...',
386
+ ),
387
+ ),
388
+ ),
389
+ const SizedBox(width: 8),
390
+ IconButton.filled(
391
+ onPressed: busy ? null : send,
392
+ icon: busy
393
+ ? const SizedBox(
394
+ width: 18,
395
+ height: 18,
396
+ child: CircularProgressIndicator(strokeWidth: 2),
397
+ )
398
+ : const Icon(Icons.send_rounded),
399
+ ),
400
+ ],
401
+ ),
402
+ ),
403
+ ),
404
+ ],
405
+ ),
406
+ );
407
+}
408
+
409
+class _GuideCard extends StatelessWidget {
410
+ const _GuideCard({
411
+ required this.icon,
412
+ required this.title,
413
+ required this.subtitle,
414
+ required this.onTap,
415
+ });
416
+
417
+ final IconData icon;
418
+ final String title;
419
+ final String subtitle;
420
+ final VoidCallback onTap;
421
+
422
+ @override
423
+ Widget build(BuildContext context) => Card(
424
+ child: ListTile(
425
+ leading: Icon(icon),
426
+ title: Text(title),
427
+ subtitle: Text(subtitle),
428
+ trailing: const Icon(Icons.chevron_left_rounded),
429
+ onTap: onTap,
430
+ ),
431
+ );
432
+}
433
+
434
+class _ScaleRow extends StatelessWidget {
435
+ const _ScaleRow({
436
+ required this.label,
437
+ required this.value,
438
+ required this.onChanged,
439
+ });
440
+
441
+ final String label;
442
+ final int value;
443
+ final ValueChanged<int> onChanged;
444
+
445
+ @override
446
+ Widget build(BuildContext context) => Row(
447
+ children: [
448
+ SizedBox(width: 70, child: Text(label)),
449
+ Expanded(
450
+ child: Slider(
451
+ value: value.toDouble(),
452
+ min: 1,
453
+ max: 5,
454
+ divisions: 4,
455
+ label: value.toString(),
456
+ onChanged: (v) => onChanged(v.round()),
457
+ ),
458
+ ),
459
+ Text(value.toString()),
460
+ ],
461
+ );
462
}