LifeMate — Personal, Family & Learning Companion
feat: add profile editing and guardian management UI
davoodmehraban89 committed
Oct 5, 2026 at 07:59 UTC
b9bcf770df571bbc2e9068bc7917afeecd07e8cd
1 file changed
+266
-1
apps/lifemate/lib/main.dart
+266
-1
@@ -110,6 +110,80 @@ class _SignInPageState extends State<SignInPage> {
110
}
111
}
112
113
+ Future<void> editProfile(Map<String, dynamic> profile) async {
114
+ final name = TextEditingController(
115
+ text: profile['display_name']?.toString() ?? '',
116
+ );
117
+ final theme = ValueNotifier<String>(
118
+ profile['theme_preference']?.toString() ?? 'adult_blue',
119
+ );
120
+ final saved = await showDialog<bool>(
121
+ context: context,
122
+ builder: (dialogContext) => AlertDialog(
123
+ title: const Text('ویرایش پروفایل'),
124
+ content: Column(
125
+ mainAxisSize: MainAxisSize.min,
126
+ children: [
127
+ TextField(
128
+ controller: name,
129
+ decoration: const InputDecoration(labelText: 'نام'),
130
+ ),
131
+ const SizedBox(height: 12),
132
+ ValueListenableBuilder<String>(
133
+ valueListenable: theme,
134
+ builder: (_, value, __) => DropdownButtonFormField<String>(
135
+ initialValue: value,
136
+ decoration: const InputDecoration(labelText: 'تم'),
137
+ items: const [
138
+ DropdownMenuItem(
139
+ value: 'girl_pink',
140
+ child: Text('سفید / صورتی'),
141
+ ),
142
+ DropdownMenuItem(
143
+ value: 'boy_blue',
144
+ child: Text('سفید / آبی نوجوان'),
145
+ ),
146
+ DropdownMenuItem(
147
+ value: 'adult_blue',
148
+ child: Text('سفید / آبی بزرگسال'),
149
+ ),
150
+ ],
151
+ onChanged: (v) {
152
+ if (v != null) {
153
+ theme.value = v;
154
+ }
155
+ },
156
+ ),
157
+ ),
158
+ ],
159
+ ),
160
+ actions: [
161
+ TextButton(
162
+ onPressed: () => Navigator.pop(dialogContext, false),
163
+ child: const Text('انصراف'),
164
+ ),
165
+ FilledButton(
166
+ onPressed: () async {
167
+ await widget.api.updateProfile(
168
+ displayName: name.text.trim(),
169
+ themePreference: theme.value,
170
+ );
171
+ if (dialogContext.mounted) {
172
+ Navigator.pop(dialogContext, true);
173
+ }
174
+ },
175
+ child: const Text('ذخیره'),
176
+ ),
177
+ ],
178
+ ),
179
+ );
180
+ name.dispose();
181
+ theme.dispose();
182
+ if (saved == true && mounted) {
183
+ setState(() => data = load());
184
+ }
185
+ }
186
+
187
@override
188
Widget build(BuildContext context) => Scaffold(
189
body: SafeArea(
@@ -600,6 +674,10 @@ class _ProfileFamilyPageState extends State<ProfileFamilyPage> {
674
leading: const CircleAvatar(child: Icon(Icons.person)),
675
title: Text(profile['display_name']?.toString() ?? 'پروفایل من'),
676
subtitle: Text(profile['email_normalized']?.toString() ?? ''),
677
+ trailing: IconButton(
678
+ onPressed: () => editProfile(profile),
679
+ icon: const Icon(Icons.edit_outlined),
680
+ ),
681
),
682
),
683
const SizedBox(height: 12),
@@ -618,7 +696,20 @@ class _ProfileFamilyPageState extends State<ProfileFamilyPage> {
696
leading: const Icon(Icons.family_restroom),
697
title: Text(family['name']?.toString() ?? 'خانواده'),
698
subtitle: Text((family['is_admin'] == true ? 'مدیر خانواده · ' : '') + (family['role']?.toString() ?? '')),
621
- trailing: IconButton(onPressed: () => invite(family['id'].toString()), icon: const Icon(Icons.person_add_alt)),
699
+ onTap: () => Navigator.of(context).push(
700
+ MaterialPageRoute(
701
+ builder: (_) => FamilyMembersPage(
702
+ api: widget.api,
703
+ familyId: family['id'].toString(),
704
+ familyName: family['name']?.toString() ?? 'خانواده',
705
+ isAdmin: family['is_admin'] == true,
706
+ ),
707
+ ),
708
+ ),
709
+ trailing: IconButton(
710
+ onPressed: () => invite(family['id'].toString()),
711
+ icon: const Icon(Icons.person_add_alt),
712
+ ),
713
),
714
),
715
),
@@ -639,3 +730,177 @@ class _ProfileFamilyPageState extends State<ProfileFamilyPage> {
730
floatingActionButton: FloatingActionButton.extended(onPressed: createFamily, icon: const Icon(Icons.add), label: const Text('خانواده')),
731
);
732
}
733
+
734
+
735
+class FamilyMembersPage extends StatefulWidget {
736
+ const FamilyMembersPage({
737
+ super.key,
738
+ required this.api,
739
+ required this.familyId,
740
+ required this.familyName,
741
+ required this.isAdmin,
742
+ });
743
+
744
+ final IdentityApi api;
745
+ final String familyId;
746
+ final String familyName;
747
+ final bool isAdmin;
748
+
749
+ @override
750
+ State<FamilyMembersPage> createState() => _FamilyMembersPageState();
751
+}
752
+
753
+class _FamilyMembersPageState extends State<FamilyMembersPage> {
754
+ late Future<List<Map<String, dynamic>>> members;
755
+
756
+ @override
757
+ void initState() {
758
+ super.initState();
759
+ members = widget.api.listFamilyMembers(widget.familyId);
760
+ }
761
+
762
+ String roleLabel(String role) => switch (role) {
763
+ 'parent_guardian' => 'والد / سرپرست',
764
+ 'teen_minor' => 'فرزند / نوجوان',
765
+ 'adult_member' => 'عضو بزرگسال',
766
+ _ => role,
767
+ };
768
+
769
+ Future<void> configureGuardian(List<Map<String, dynamic>> items) async {
770
+ final guardians = items
771
+ .where((member) => member['role'] == 'parent_guardian')
772
+ .toList();
773
+ final minors = items.where((member) => member['role'] == 'teen_minor').toList();
774
+ if (guardians.isEmpty || minors.isEmpty) {
775
+ ScaffoldMessenger.of(context).showSnackBar(
776
+ const SnackBar(
777
+ content: Text('برای ثبت سرپرستی، حداقل یک والد و یک فرزند لازم است.'),
778
+ ),
779
+ );
780
+ return;
781
+ }
782
+
783
+ final guardianId = ValueNotifier<String>(guardians.first['user_id'].toString());
784
+ final minorId = ValueNotifier<String>(minors.first['user_id'].toString());
785
+
786
+ final saved = await showDialog<bool>(
787
+ context: context,
788
+ builder: (dialogContext) => AlertDialog(
789
+ title: const Text('رابطه والد و فرزند'),
790
+ content: Column(
791
+ mainAxisSize: MainAxisSize.min,
792
+ children: [
793
+ ValueListenableBuilder<String>(
794
+ valueListenable: guardianId,
795
+ builder: (_, value, __) => DropdownButtonFormField<String>(
796
+ initialValue: value,
797
+ decoration: const InputDecoration(labelText: 'والد / سرپرست'),
798
+ items: guardians
799
+ .map(
800
+ (member) => DropdownMenuItem(
801
+ value: member['user_id'].toString(),
802
+ child: Text(member['display_name'].toString()),
803
+ ),
804
+ )
805
+ .toList(),
806
+ onChanged: (v) {
807
+ if (v != null) guardianId.value = v;
808
+ },
809
+ ),
810
+ ),
811
+ const SizedBox(height: 12),
812
+ ValueListenableBuilder<String>(
813
+ valueListenable: minorId,
814
+ builder: (_, value, __) => DropdownButtonFormField<String>(
815
+ initialValue: value,
816
+ decoration: const InputDecoration(labelText: 'فرزند'),
817
+ items: minors
818
+ .map(
819
+ (member) => DropdownMenuItem(
820
+ value: member['user_id'].toString(),
821
+ child: Text(member['display_name'].toString()),
822
+ ),
823
+ )
824
+ .toList(),
825
+ onChanged: (v) {
826
+ if (v != null) minorId.value = v;
827
+ },
828
+ ),
829
+ ),
830
+ ],
831
+ ),
832
+ actions: [
833
+ TextButton(
834
+ onPressed: () => Navigator.pop(dialogContext, false),
835
+ child: const Text('انصراف'),
836
+ ),
837
+ FilledButton(
838
+ onPressed: () async {
839
+ await widget.api.setGuardian(
840
+ familyId: widget.familyId,
841
+ guardianUserId: guardianId.value,
842
+ minorUserId: minorId.value,
843
+ );
844
+ if (dialogContext.mounted) {
845
+ Navigator.pop(dialogContext, true);
846
+ }
847
+ },
848
+ child: const Text('ثبت رابطه'),
849
+ ),
850
+ ],
851
+ ),
852
+ );
853
+
854
+ guardianId.dispose();
855
+ minorId.dispose();
856
+
857
+ if (saved == true && mounted) {
858
+ ScaffoldMessenger.of(context).showSnackBar(
859
+ const SnackBar(content: Text('رابطه سرپرستی ثبت شد.')),
860
+ );
861
+ }
862
+ }
863
+
864
+ @override
865
+ Widget build(BuildContext context) => Scaffold(
866
+ appBar: AppBar(title: Text(widget.familyName)),
867
+ body: FutureBuilder<List<Map<String, dynamic>>>(
868
+ future: members,
869
+ builder: (context, snapshot) {
870
+ if (snapshot.hasError) {
871
+ return Center(child: Text(errorText(snapshot.error!)));
872
+ }
873
+ if (!snapshot.hasData) {
874
+ return const Center(child: CircularProgressIndicator());
875
+ }
876
+ final items = snapshot.data!;
877
+ return ListView(
878
+ padding: const EdgeInsets.all(20),
879
+ children: [
880
+ for (final member in items)
881
+ Card(
882
+ child: ListTile(
883
+ leading: const CircleAvatar(child: Icon(Icons.person)),
884
+ title: Text(member['display_name']?.toString() ?? 'عضو'),
885
+ subtitle: Text(
886
+ roleLabel(member['role']?.toString() ?? ''),
887
+ ),
888
+ trailing: member['is_admin'] == true
889
+ ? const Chip(label: Text('مدیر'))
890
+ : null,
891
+ ),
892
+ ),
893
+ if (widget.isAdmin) ...[
894
+ const SizedBox(height: 12),
895
+ FilledButton.icon(
896
+ onPressed: () => configureGuardian(items),
897
+ icon: const Icon(Icons.supervisor_account_outlined),
898
+ label: const Text('تنظیم رابطه والد و فرزند'),
899
+ ),
900
+ ],
901
+ ],
902
+ );
903
+ },
904
+ ),
905
+ );
906
+}