LifeMate — Personal, Family & Learning Companion

test/personal-notification-settings
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 166 lines 6.04 KB
Raw
1 import 'package:flutter/material.dart';
2 import 'package:shamsi_date/shamsi_date.dart';
3
4 import 'personal_dates.dart';
5
6 /// Presentation-only calendar: the result is still a local Gregorian DateTime.
7 /// No stored date, weekday or notification timestamp is converted in place.
8 Future<DateTime?> showPersonalDatePicker({
9 required BuildContext context,
10 required DateTime initialDate,
11 required DateTime firstDate,
12 required DateTime lastDate,
13 }) {
14 final first = DateUtils.dateOnly(firstDate);
15 final last = DateUtils.dateOnly(lastDate);
16 if (last.isBefore(first)) throw ArgumentError('Invalid calendar date range.');
17 final initial = DateUtils.dateOnly(initialDate);
18 return showDialog<DateTime>(
19 context: context,
20 builder: (_) => _PersianDateDialog(
21 initial: initial.isBefore(first)
22 ? first
23 : initial.isAfter(last)
24 ? last
25 : initial,
26 first: first,
27 last: last,
28 ),
29 );
30 }
31
32 class _PersianDateDialog extends StatefulWidget {
33 const _PersianDateDialog({
34 required this.initial,
35 required this.first,
36 required this.last,
37 });
38 final DateTime initial;
39 final DateTime first;
40 final DateTime last;
41
42 @override
43 State<_PersianDateDialog> createState() => _PersianDateDialogState();
44 }
45
46 class _PersianDateDialogState extends State<_PersianDateDialog> {
47 late final _first = Jalali.fromDateTime(widget.first);
48 late final _last = Jalali.fromDateTime(widget.last);
49 late Jalali _selected = Jalali.fromDateTime(widget.initial);
50
51 int _firstMonth(int year) => year == _first.year ? _first.month : 1;
52 int _lastMonth(int year) => year == _last.year ? _last.month : 12;
53 int _firstDay(int year, int month) =>
54 year == _first.year && month == _first.month ? _first.day : 1;
55 int _lastDay(int year, int month) =>
56 year == _last.year && month == _last.month
57 ? _last.day
58 : Jalali(year, month).monthLength;
59
60 void _change({int? year, int? month, int? day}) {
61 final y = (year ?? _selected.year).clamp(_first.year, _last.year);
62 final m = (month ?? _selected.month).clamp(_firstMonth(y), _lastMonth(y));
63 final d = (day ?? _selected.day).clamp(_firstDay(y, m), _lastDay(y, m));
64 setState(() => _selected = Jalali(y, m, d));
65 }
66
67 Widget _field({
68 required String id,
69 required String label,
70 required int value,
71 required int first,
72 required int last,
73 required String Function(int) text,
74 required ValueChanged<int> changed,
75 }) =>
76 InputDecorator(
77 key: ValueKey('personal-date-$id'),
78 decoration: InputDecoration(labelText: label),
79 child: DropdownButtonHideUnderline(
80 child: ConstrainedBox(
81 constraints: const BoxConstraints(minHeight: 48),
82 child: DropdownButton<int>(
83 value: value,
84 isExpanded: true,
85 itemHeight: null,
86 menuMaxHeight: MediaQuery.sizeOf(context).height * .6,
87 items: [
88 for (var item = first; item <= last; item++)
89 DropdownMenuItem(
90 value: item,
91 child: ConstrainedBox(
92 constraints: const BoxConstraints(minHeight: 48),
93 child: Align(
94 alignment: AlignmentDirectional.centerStart,
95 child: Text(text(item))),
96 ),
97 ),
98 ],
99 onChanged: (selected) {
100 if (selected != null) changed(selected);
101 },
102 ),
103 ),
104 ),
105 );
106
107 @override
108 Widget build(BuildContext context) => AlertDialog(
109 key: const Key('personal-date-dialog'),
110 insetPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
111 title: const Text('انتخاب تاریخ شمسی'),
112 content: SizedBox(
113 width: 320,
114 child: SingleChildScrollView(
115 child: Column(
116 mainAxisSize: MainAxisSize.min,
117 crossAxisAlignment: CrossAxisAlignment.stretch,
118 children: [
119 const SizedBox(height: 8),
120 _field(
121 id: 'year',
122 label: 'سال',
123 value: _selected.year,
124 first: _first.year,
125 last: _last.year,
126 text: personalDateDigits,
127 changed: (value) => _change(year: value)),
128 const SizedBox(height: 20),
129 _field(
130 id: 'month',
131 label: 'ماه',
132 value: _selected.month,
133 first: _firstMonth(_selected.year),
134 last: _lastMonth(_selected.year),
135 text: (month) => Jalali(_selected.year, month).formatter.mN,
136 changed: (value) => _change(month: value)),
137 const SizedBox(height: 20),
138 _field(
139 id: 'day',
140 label: 'روز',
141 value: _selected.day,
142 first: _firstDay(_selected.year, _selected.month),
143 last: _lastDay(_selected.year, _selected.month),
144 text: personalDateDigits,
145 changed: (value) => _change(day: value)),
146 const SizedBox(height: 20),
147 Text(personalDateLabel(_selected.toDateTime()),
148 key: const Key('personal-date-preview'),
149 style: Theme.of(context).textTheme.titleMedium),
150 const SizedBox(height: 12),
151 const Text(
152 'اگر روز انتخابی در ماه یا سال جدید نباشد، آخرین روز مجاز انتخاب می‌شود.'),
153 ],
154 ),
155 ),
156 ),
157 actions: [
158 TextButton(
159 onPressed: () => Navigator.pop(context),
160 child: const Text('انصراف')),
161 FilledButton(
162 onPressed: () => Navigator.pop(context, _selected.toDateTime()),
163 child: const Text('انتخاب تاریخ')),
164 ],
165 );
166 }