LifeMate — Personal, Family & Learning Companion

feat/aparat-learning-library
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
dart 529 lines 17.1 KB
Raw
1 import 'package:flutter/material.dart';
2
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:
41 const InputDecoration(labelText: 'هدف یا نتیجه مطلوب'),
42 ),
43 ],
44 ),
45 actions: [
46 TextButton(
47 onPressed: () => Navigator.pop(dialogContext, false),
48 child: const Text('انصراف'),
49 ),
50 FilledButton(
51 onPressed: () async {
52 if (title.text.trim().isEmpty) return;
53 await widget.api.createLearningGoal(
54 title: title.text.trim(),
55 target: target.text.trim().isEmpty ? null : target.text.trim(),
56 );
57 if (dialogContext.mounted) Navigator.pop(dialogContext, true);
58 },
59 child: const Text('ذخیره'),
60 ),
61 ],
62 ),
63 );
64 title.dispose();
65 target.dispose();
66 if (saved == true && mounted) {
67 setState(() => goals = widget.api.listLearningGoals());
68 }
69 }
70
71 Future<void> learningCheckin(Map<String, dynamic> goal) async {
72 var confidence = 3;
73 var difficulty = 3;
74 final note = TextEditingController();
75 final saved = await showDialog<bool>(
76 context: context,
77 builder: (dialogContext) => StatefulBuilder(
78 builder: (context, setLocalState) => AlertDialog(
79 title: Text('ثبت پیشرفت · ${goal['title'] ?? ''}'),
80 content: Column(
81 mainAxisSize: MainAxisSize.min,
82 children: [
83 _ScaleRow(
84 label: 'اعتماد',
85 value: confidence,
86 onChanged: (v) => setLocalState(() => confidence = v),
87 ),
88 _ScaleRow(
89 label: 'سختی',
90 value: difficulty,
91 onChanged: (v) => setLocalState(() => difficulty = v),
92 ),
93 const SizedBox(height: 12),
94 TextField(
95 controller: note,
96 maxLines: 2,
97 decoration: const InputDecoration(
98 labelText: 'یادداشت کوتاه (اختیاری)',
99 ),
100 ),
101 ],
102 ),
103 actions: [
104 TextButton(
105 onPressed: () => Navigator.pop(dialogContext, false),
106 child: const Text('انصراف'),
107 ),
108 FilledButton(
109 onPressed: () async {
110 await widget.api.createLearningCheckin(
111 learningGoalId: goal['id']?.toString(),
112 confidence: confidence,
113 difficulty: difficulty,
114 note: note.text.trim().isEmpty ? null : note.text.trim(),
115 );
116 if (dialogContext.mounted) Navigator.pop(dialogContext, true);
117 },
118 child: const Text('ثبت'),
119 ),
120 ],
121 ),
122 ),
123 );
124 note.dispose();
125 if (saved == true && mounted) {
126 ScaffoldMessenger.of(context).showSnackBar(
127 const SnackBar(content: Text('پیشرفت یادگیری ثبت شد.')),
128 );
129 }
130 }
131
132 Future<void> wellbeingCheckin() async {
133 var mood = 3;
134 var energy = 3;
135 var stress = 3;
136 var visibility = 'private';
137 final note = TextEditingController();
138
139 final saved = await showDialog<bool>(
140 context: context,
141 builder: (dialogContext) => StatefulBuilder(
142 builder: (context, setLocalState) => AlertDialog(
143 title: const Text('ثبت حال امروز'),
144 content: SingleChildScrollView(
145 child: Column(
146 mainAxisSize: MainAxisSize.min,
147 children: [
148 _ScaleRow(
149 label: 'حال کلی',
150 value: mood,
151 onChanged: (v) => setLocalState(() => mood = v),
152 ),
153 _ScaleRow(
154 label: 'انرژی',
155 value: energy,
156 onChanged: (v) => setLocalState(() => energy = v),
157 ),
158 _ScaleRow(
159 label: 'استرس',
160 value: stress,
161 onChanged: (v) => setLocalState(() => stress = v),
162 ),
163 const SizedBox(height: 12),
164 TextField(
165 controller: note,
166 maxLines: 3,
167 decoration: const InputDecoration(
168 labelText: 'یادداشت خصوصی (اختیاری)',
169 ),
170 ),
171 const SizedBox(height: 12),
172 DropdownButtonFormField<String>(
173 initialValue: visibility,
174 decoration: const InputDecoration(labelText: 'اشتراک وضعیت'),
175 items: const [
176 DropdownMenuItem(
177 value: 'private',
178 child: Text('کاملاً خصوصی'),
179 ),
180 DropdownMenuItem(
181 value: 'guardian_summary',
182 child: Text('فقط در خلاصه آماری والد'),
183 ),
184 ],
185 onChanged: (v) {
186 if (v != null) setLocalState(() => visibility = v);
187 },
188 ),
189 ],
190 ),
191 ),
192 actions: [
193 TextButton(
194 onPressed: () => Navigator.pop(dialogContext, false),
195 child: const Text('انصراف'),
196 ),
197 FilledButton(
198 onPressed: () async {
199 await widget.api.createWellbeingCheckin(
200 mood: mood,
201 energy: energy,
202 stress: stress,
203 note: note.text.trim().isEmpty ? null : note.text.trim(),
204 visibility: visibility,
205 );
206 if (dialogContext.mounted) Navigator.pop(dialogContext, true);
207 },
208 child: const Text('ثبت'),
209 ),
210 ],
211 ),
212 ),
213 );
214 note.dispose();
215 if (saved == true && mounted) {
216 ScaffoldMessenger.of(context).showSnackBar(
217 const SnackBar(
218 content: Text('ثبت شد. متن یادداشت خصوصی باقی می‌ماند.')),
219 );
220 }
221 }
222
223 void openGuide(String kind, String title) {
224 Navigator.of(context).push(
225 MaterialPageRoute(
226 builder: (_) => GuideChatPage(
227 api: widget.api,
228 kind: kind,
229 title: title,
230 ),
231 ),
232 );
233 }
234
235 @override
236 Widget build(BuildContext context) => ListView(
237 padding: const EdgeInsets.all(16),
238 children: [
239 Text(
240 widget.adultShell ? 'راهنمای هوشمند' : 'یادگیری و حال خوب',
241 style: Theme.of(context).textTheme.headlineSmall,
242 ),
243 const SizedBox(height: 6),
244 const Text(
245 'راهنماها پیشنهاد می‌دهند؛ تصمیم و اجرای تغییرها با خودت است.',
246 ),
247 const SizedBox(height: 16),
248 _GuideCard(
249 icon: Icons.school_rounded,
250 title: 'راهنمای مطالعه',
251 subtitle: 'فهم بهتر، مرور فعال و برنامه مطالعه مرحله‌به‌مرحله.',
252 onTap: () => openGuide('study', 'راهنمای مطالعه'),
253 ),
254 _GuideCard(
255 icon: Icons.auto_awesome_rounded,
256 title: 'راهنمای برنامه‌ریزی',
257 subtitle: 'اولویت‌بندی و شکستن کارها بدون تغییر خودکار برنامه.',
258 onTap: () => openGuide('planner', 'راهنمای برنامه‌ریزی'),
259 ),
260 _GuideCard(
261 icon: Icons.self_improvement_rounded,
262 title: 'همراه حال خوب',
263 subtitle:
264 'گفت‌وگوی حمایتی و غیرتشخیصی برای حال، ارتباط و فشار روزمره.',
265 onTap: () => openGuide('wellbeing', 'همراه حال خوب'),
266 ),
267 const SizedBox(height: 8),
268 Row(
269 children: [
270 Expanded(
271 child: FilledButton.icon(
272 onPressed: wellbeingCheckin,
273 icon: const Icon(Icons.favorite_outline),
274 label: const Text('ثبت حال امروز'),
275 ),
276 ),
277 const SizedBox(width: 8),
278 Expanded(
279 child: OutlinedButton.icon(
280 onPressed: addGoal,
281 icon: const Icon(Icons.flag_outlined),
282 label: const Text('هدف یادگیری'),
283 ),
284 ),
285 ],
286 ),
287 const SizedBox(height: 16),
288 const Card(
289 child: Padding(
290 padding: EdgeInsets.all(16),
291 child: Text(
292 'حریم خصوصی: متن گفت‌وگو و یادداشت حال خوب برای خود کاربر است. '
293 'والد فقط خلاصه‌ای را می‌بیند که مجاز شده باشد؛ در وضعیت ایمنی جدی، '
294 'سیگنال ایمنی جدا از متن خصوصی مدیریت می‌شود.',
295 ),
296 ),
297 ),
298 const SizedBox(height: 12),
299 Text('هدف‌های یادگیری',
300 style: Theme.of(context).textTheme.titleMedium),
301 FutureBuilder<List<Map<String, dynamic>>>(
302 future: goals,
303 builder: (context, snapshot) {
304 if (!snapshot.hasData) {
305 return const Padding(
306 padding: EdgeInsets.all(20),
307 child: Center(child: CircularProgressIndicator()),
308 );
309 }
310 if (snapshot.data!.isEmpty) {
311 return const Card(
312 child: ListTile(
313 leading: Icon(Icons.flag_outlined),
314 title: Text('هنوز هدفی ثبت نشده'),
315 ),
316 );
317 }
318 return Column(
319 children: snapshot.data!
320 .map(
321 (goal) => Card(
322 child: ListTile(
323 leading: const Icon(Icons.flag_circle_outlined),
324 title: Text(goal['title']?.toString() ?? ''),
325 subtitle: Text(goal['target']?.toString() ?? ''),
326 trailing: const Icon(Icons.add_chart_outlined),
327 onTap: () => learningCheckin(goal),
328 ),
329 ),
330 )
331 .toList(),
332 );
333 },
334 ),
335 ],
336 );
337 }
338
339 class GuideChatPage extends StatefulWidget {
340 const GuideChatPage({
341 super.key,
342 required this.api,
343 required this.kind,
344 required this.title,
345 });
346
347 final IdentityApi api;
348 final String kind;
349 final String title;
350
351 @override
352 State<GuideChatPage> createState() => _GuideChatPageState();
353 }
354
355 class _GuideChatPageState extends State<GuideChatPage> {
356 final input = TextEditingController();
357 final messages = <Map<String, String>>[];
358 String? sessionId;
359 bool busy = false;
360
361 Future<void> send() async {
362 final text = input.text.trim();
363 if (text.isEmpty || busy) return;
364 setState(() {
365 busy = true;
366 messages.add({'role': 'user', 'body': text});
367 input.clear();
368 });
369 try {
370 final session = sessionId == null
371 ? await widget.api.createAiSession(widget.kind)
372 : null;
373 sessionId ??= session?['id']?.toString();
374 if (sessionId == null) throw StateError('session_not_created');
375 final reply = await widget.api.sendAiMessage(sessionId!, text);
376 if (mounted) {
377 setState(() {
378 messages.add({
379 'role': 'assistant',
380 'body': reply['body']?.toString() ?? '',
381 });
382 });
383 }
384 } catch (_) {
385 if (mounted) {
386 setState(() {
387 messages.add({
388 'role': 'assistant',
389 'body': 'ارسال انجام نشد. دوباره تلاش کن.',
390 });
391 });
392 }
393 } finally {
394 if (mounted) setState(() => busy = false);
395 }
396 }
397
398 @override
399 void dispose() {
400 input.dispose();
401 super.dispose();
402 }
403
404 @override
405 Widget build(BuildContext context) => Scaffold(
406 appBar: AppBar(title: Text(widget.title)),
407 body: Column(
408 children: [
409 const Padding(
410 padding: EdgeInsets.fromLTRB(16, 8, 16, 4),
411 child: Text(
412 'این راهنما جای پزشک، روان‌شناس یا خدمات اضطراری نیست و تشخیص پزشکی نمی‌دهد.',
413 textAlign: TextAlign.center,
414 ),
415 ),
416 Expanded(
417 child: ListView.builder(
418 padding: const EdgeInsets.all(16),
419 itemCount: messages.length,
420 itemBuilder: (context, index) {
421 final item = messages[index];
422 final mine = item['role'] == 'user';
423 return Align(
424 alignment:
425 mine ? Alignment.centerRight : Alignment.centerLeft,
426 child: Card(
427 child: Padding(
428 padding: const EdgeInsets.all(12),
429 child: ConstrainedBox(
430 constraints: const BoxConstraints(maxWidth: 560),
431 child: Text(item['body'] ?? ''),
432 ),
433 ),
434 ),
435 );
436 },
437 ),
438 ),
439 SafeArea(
440 top: false,
441 child: Padding(
442 padding: const EdgeInsets.all(12),
443 child: Row(
444 children: [
445 Expanded(
446 child: TextField(
447 controller: input,
448 minLines: 1,
449 maxLines: 4,
450 onSubmitted: (_) => send(),
451 decoration: const InputDecoration(
452 hintText: 'اینجا بنویس...',
453 ),
454 ),
455 ),
456 const SizedBox(width: 8),
457 IconButton.filled(
458 onPressed: busy ? null : send,
459 icon: busy
460 ? const SizedBox(
461 width: 18,
462 height: 18,
463 child: CircularProgressIndicator(strokeWidth: 2),
464 )
465 : const Icon(Icons.send_rounded),
466 ),
467 ],
468 ),
469 ),
470 ),
471 ],
472 ),
473 );
474 }
475
476 class _GuideCard extends StatelessWidget {
477 const _GuideCard({
478 required this.icon,
479 required this.title,
480 required this.subtitle,
481 required this.onTap,
482 });
483
484 final IconData icon;
485 final String title;
486 final String subtitle;
487 final VoidCallback onTap;
488
489 @override
490 Widget build(BuildContext context) => Card(
491 child: ListTile(
492 leading: Icon(icon),
493 title: Text(title),
494 subtitle: Text(subtitle),
495 trailing: const Icon(Icons.chevron_left_rounded),
496 onTap: onTap,
497 ),
498 );
499 }
500
501 class _ScaleRow extends StatelessWidget {
502 const _ScaleRow({
503 required this.label,
504 required this.value,
505 required this.onChanged,
506 });
507
508 final String label;
509 final int value;
510 final ValueChanged<int> onChanged;
511
512 @override
513 Widget build(BuildContext context) => Row(
514 children: [
515 SizedBox(width: 70, child: Text(label)),
516 Expanded(
517 child: Slider(
518 value: value.toDouble(),
519 min: 1,
520 max: 5,
521 divisions: 4,
522 label: value.toString(),
523 onChanged: (v) => onChanged(v.round()),
524 ),
525 ),
526 Text(value.toString()),
527 ],
528 );
529 }