@@ -10,32 +10,90 @@ import 'package:google_fonts/google_fonts.dart';
10
10
String uID = FirebaseAuthService ().getUID;
11
11
DatabaseService _databaseService = DatabaseService (uid: uID);
12
12
13
+ class GenreChoice extends StatefulWidget {
14
+ // const GenreChoice({ Key? key }) : super(key: key);
15
+
16
+ @override
17
+ _GenreChoiceState createState () => _GenreChoiceState ();
18
+ }
19
+
13
20
class UserPreference extends StatefulWidget {
14
21
final UserData ? userData;
15
22
const UserPreference (this .userData);
16
23
@override
17
24
_UserPreferenceState createState () => _UserPreferenceState ();
18
25
}
19
26
27
+ class _GenreChoiceState extends State <GenreChoice > {
28
+ List <String > tags = [];
29
+ @override
30
+ Widget build (BuildContext context) {
31
+ print ('*******' );
32
+ // print(_author.text);
33
+ // print(a);
34
+ print (tags);
35
+ // print(_user.value)
36
+
37
+ return ChipsChoice <String >.multiple (
38
+ value: tags,
39
+ onChanged: (List <String > val) => setState (() {
40
+ tags = val;
41
+ // a = a;
42
+ // b = b;
43
+ }),
44
+ choiceItems: C2Choice .listFrom <String , String >(
45
+ source: genres,
46
+ value: (int i, String v) => v,
47
+ label: (int i, String v) => v,
48
+ tooltip: (int i, String v) => v,
49
+ ),
50
+ choiceStyle: const C2ChoiceStyle (
51
+ color: Colors .blue,
52
+ borderRadius: BorderRadius .all (Radius .circular (5 )),
53
+ borderColor: Colors .green,
54
+ ),
55
+ choiceActiveStyle: const C2ChoiceStyle (
56
+ color: blackButton,
57
+ ),
58
+ wrapped: true ,
59
+ );
60
+ }
61
+ }
62
+
20
63
class _UserPreferenceState extends State <UserPreference > {
21
64
final GlobalKey <FormState > _formKey = GlobalKey <FormState >();
22
65
23
- // final TextEditingController _author = TextEditingController();
24
- // final TextEditingController _book = TextEditingController();
66
+ final TextEditingController _author = TextEditingController ();
67
+ final TextEditingController _book = TextEditingController ();
25
68
26
69
List <String > tags = [];
70
+ String a = '' ;
71
+ String b = '' ;
27
72
28
73
@override
29
74
Widget build (BuildContext context) {
75
+ String favBook;
76
+ String favAuthor;
30
77
// final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
31
- final String ? favBook = widget.userData! .preferences! ['favBook' ] as String ? ;
32
- final String ? favAuthor =
78
+ if (a == null && b == null ) {
79
+ print ('helllllloooo' );
80
+ favBook = widget.userData! .preferences! ['favBook' ] as String ;
81
+ favAuthor = widget.userData! .preferences! ['favAuthor' ] as String ;
82
+ a = favBook;
83
+ b = favAuthor;
84
+ } else {
85
+ print ('{{}}}' );
86
+ favBook = a;
87
+ favAuthor = b;
88
+ }
89
+ // final String? favBook = widget.userData!.preferences!['favBook'] as String?;
90
+ // final String? favAuthor =
33
91
widget.userData! .preferences! ['favAuthor' ] as String ? ;
34
92
// final String location =
35
93
// widget.userData.preferences['locationRange'] as String;
36
94
37
95
final TextEditingController _author =
38
- TextEditingController (text: favAuthor);
96
+ TextEditingController (text: favAuthor);
39
97
final TextEditingController _book = TextEditingController (text: favBook);
40
98
41
99
return Form (
@@ -46,7 +104,7 @@ class _UserPreferenceState extends State<UserPreference> {
46
104
child: Text (
47
105
'User Preferences' ,
48
106
style:
49
- GoogleFonts .lato (color: Theme .of (context).colorScheme.primary),
107
+ GoogleFonts .lato (color: Theme .of (context).colorScheme.primary),
50
108
),
51
109
),
52
110
shape: const RoundedRectangleBorder (
@@ -57,15 +115,14 @@ class _UserPreferenceState extends State<UserPreference> {
57
115
width: 250 ,
58
116
child: SingleChildScrollView (
59
117
child: Column (
60
- children: < Widget > [
118
+ children: [
61
119
const SizedBox (
62
120
height: 20 ,
63
121
),
64
122
TextFormField (
65
123
controller: _book,
66
124
keyboardType: TextInputType .name,
67
125
textAlign: TextAlign .start,
68
- // initialValue: favBook,
69
126
decoration: InputDecoration (
70
127
hintText: 'Favourite Book' ,
71
128
fillColor: Theme .of (context).colorScheme.primary,
@@ -79,6 +136,7 @@ class _UserPreferenceState extends State<UserPreference> {
79
136
return null ;
80
137
},
81
138
onChanged: (String v) {
139
+ a = v;
82
140
print (v);
83
141
},
84
142
onSaved: (String ? val) {
@@ -89,7 +147,6 @@ class _UserPreferenceState extends State<UserPreference> {
89
147
height: 10 ,
90
148
),
91
149
TextFormField (
92
- // initialValue: favAuthor,
93
150
controller: _author,
94
151
keyboardType: TextInputType .name,
95
152
textAlign: TextAlign .start,
@@ -105,12 +162,44 @@ class _UserPreferenceState extends State<UserPreference> {
105
162
}
106
163
return null ;
107
164
},
165
+ onChanged: (String v) {
166
+ b = v;
167
+ // _author.text=v;
168
+ print (v);
169
+ },
108
170
),
109
171
const SizedBox (
110
172
height: 20 ,
111
173
),
112
174
Text ('Select Book genres' , style: GoogleFonts .lato ()),
113
- _genresChoice (),
175
+ // _genresChoice(),
176
+ Builder (builder: (BuildContext ctx) {
177
+ return ChipsChoice <String >.multiple (
178
+ value: tags,
179
+ onChanged: (List <String > val) => setState (() {
180
+ tags = val;
181
+ a = a;
182
+ b = b;
183
+ }),
184
+ choiceItems: C2Choice .listFrom <String , String >(
185
+ source: genres,
186
+ value: (int i, String v) => v,
187
+ label: (int i, String v) => v,
188
+ tooltip: (int i, String v) => v,
189
+ ),
190
+ choiceStyle: const C2ChoiceStyle (
191
+ color: Colors .blue,
192
+ borderRadius: BorderRadius .all (Radius .circular (5 )),
193
+ borderColor: Colors .green,
194
+ ),
195
+ choiceActiveStyle: const C2ChoiceStyle (
196
+ color: blackButton,
197
+ ),
198
+ wrapped: true ,
199
+ );
200
+ }),
201
+
202
+ // GenreChoice(),
114
203
],
115
204
),
116
205
),
@@ -120,9 +209,15 @@ class _UserPreferenceState extends State<UserPreference> {
120
209
onPressed: () async {
121
210
//Validate Author and BookName
122
211
if (_formKey.currentState! .validate ()) {
123
- _onSubmitTap ();
124
212
_formKey.currentState! .save ();
125
- Navigator .pop (context);
213
+ _onSubmitTap ();
214
+ if (mounted) {
215
+ ScaffoldMessenger .of (context).showSnackBar (const SnackBar (
216
+ content: Text ('User preferences have been saved' ,
217
+ style: TextStyle (color: blackButton)),
218
+ duration: Duration (seconds: 3 ),
219
+ ));
220
+ }
126
221
}
127
222
},
128
223
child: Text (
@@ -146,37 +241,18 @@ class _UserPreferenceState extends State<UserPreference> {
146
241
);
147
242
}
148
243
149
- ChipsChoice <String > _genresChoice () {
150
- return ChipsChoice <String >.multiple (
151
- value: tags,
152
- onChanged: (List <String > val) => setState (() => tags = val),
153
- choiceItems: C2Choice .listFrom <String , String >(
154
- source: genres,
155
- value: (i, v) => v,
156
- label: (i, v) => v,
157
- ),
158
- choiceStyle: const C2ChoiceStyle (
159
- borderRadius: BorderRadius .all (Radius .circular (5 )),
160
- borderColor: Colors .grey,
161
- ),
162
- choiceActiveStyle: const C2ChoiceStyle (
163
- color: blackButton,
164
- ),
165
- wrapped: true ,
166
- );
167
- }
168
-
169
- Future <void > _onSubmitTap () async {
244
+ Future _onSubmitTap () async {
170
245
final List <dynamic > items = tags.toList ();
171
- items.removeRange (0 , 1 );
172
- print (items);
246
+
173
247
final List <String > selectedGenres = < String > [];
174
248
for (final dynamic element in items) {
175
249
final String x = element.toString ();
176
- print (genres[ int . parse (x)]);
177
- selectedGenres.add (genres[ int . parse (x)] );
250
+
251
+ selectedGenres.add (x );
178
252
}
179
- print (selectedGenres);
253
+
180
254
await _databaseService.updateGenres (selectedGenres);
255
+ await _databaseService.updatePreferences (b, a);
256
+ Navigator .pop (context);
181
257
}
182
- }
258
+ }
0 commit comments