@@ -2,8 +2,8 @@ import 'package:corecoder_develop/main.dart';
2
2
import 'package:corecoder_develop/plugins_browser.dart' ;
3
3
import 'package:corecoder_develop/util/modules_manager.dart' ;
4
4
import 'package:corecoder_develop/util/theme_manager.dart' ;
5
- import 'package:flutter/cupertino.dart' ;
6
5
import 'package:flutter/material.dart' ;
6
+ import 'package:shared_preferences/shared_preferences.dart' ;
7
7
8
8
import 'util/plugins_manager.dart' ;
9
9
@@ -16,60 +16,103 @@ class Settings {
16
16
}
17
17
18
18
enum SettingsPageItemType {
19
- TypeString ,
20
- TypeStringList ,
21
- TypeBoolean ,
22
- TypeInteger ,
23
- TypeFloat
19
+ typeString ,
20
+ typeStringList ,
21
+ typeBoolean ,
22
+ typeInteger ,
23
+ typeFloat
24
24
}
25
25
26
26
class SettingsPageItem {
27
- Function (dynamic val) onSet;
27
+ Function (SettingsPageItem item, dynamic val) onSet;
28
+ Function (SettingsPageItem item) onInitialized;
28
29
String name;
29
30
String description;
30
31
SettingsPageItemType type;
31
32
dynamic provided; // the string list for list type
32
33
dynamic defaultVal;
33
34
dynamic currentVal;
34
35
35
- SettingsPageItem (this .name, this .description, this .onSet, this .type,
36
- this .provided, this .defaultVal);
36
+ SettingsPageItem (
37
+ {required this .name,
38
+ required this .description,
39
+ required this .onSet,
40
+ required this .onInitialized,
41
+ required this .type,
42
+ this .provided,
43
+ this .defaultVal});
37
44
}
38
45
39
46
class SettingsPage extends StatefulWidget {
40
47
final ModulesManager modulesManager;
41
- const SettingsPage (this .modulesManager,{Key ? key}) : super (key: key);
48
+
49
+ const SettingsPage (this .modulesManager, {Key ? key}) : super (key: key);
50
+
42
51
@override
43
52
State <StatefulWidget > createState () => SettingsPageState ();
44
53
}
54
+
45
55
class SettingsPageState extends State <SettingsPage > {
56
+ static final Future <SharedPreferences > _pref =
57
+ SharedPreferences .getInstance ();
46
58
static var routeName = "/SettingsPage" ;
47
59
var tabs = < Widget > [
48
- const Tab (text: "General" ,),
60
+ const Tab (
61
+ text: "General" ,
62
+ ),
49
63
const Tab (text: "Plugins" ),
50
64
const Tab (text: "About" ),
51
65
];
66
+
52
67
Widget getSettingsTabContent (BuildContext context) {
53
68
return Column (
54
69
children: List .generate (items.length, (index) {
70
+
55
71
return generateListItem (index, context);
56
72
}));
57
73
}
58
74
59
75
List <SettingsPageItem > items = [
60
76
SettingsPageItem (
61
- "Theme" ,
62
- "The theme for entire app" ,
63
- (dynamic val) => {ThemeManager .setTheme (val)},
64
- SettingsPageItemType .TypeStringList ,
65
- < String > ["core-coder-dark" , "core-coder-light" ],
66
- ThemeManager .currentTheme.value)
77
+ name: "Theme" ,
78
+ description: "The theme for entire app" ,
79
+ onSet: (SettingsPageItem item, dynamic val) async {
80
+ ThemeManager .setTheme (val);
81
+
82
+ // Set the value to be stored
83
+ (await _pref).setString ("theme" , val);
84
+ },
85
+ onInitialized: (SettingsPageItem item)async {
86
+ // Get the item value from prefs
87
+ var val = (await _pref).getString ("theme" );
88
+ item.currentVal ?? = val;
89
+ },
90
+ type: SettingsPageItemType .typeStringList,
91
+ provided: < String > ["core-coder-dark" , "core-coder-light" ],
92
+ defaultVal: ThemeManager .currentTheme.value),
93
+ SettingsPageItem (
94
+ name: "Open last project on startup" ,
95
+ description: "Open last project when the app started" ,
96
+ onSet: (SettingsPageItem item, dynamic val) async {
97
+ item.currentVal = val;
98
+ // Set the value to be stored
99
+ (await _pref).setBool ("openLastProjectOnStartup" , val);
100
+ },
101
+ onInitialized: (SettingsPageItem item)async {
102
+ // Get the item value from prefs
103
+ var val = (await _pref).getBool ("openLastProjectOnStartup" );
104
+ item.currentVal ?? = val;
105
+ },
106
+ type: SettingsPageItemType .typeBoolean,
107
+ provided: < bool > [true , false ],
108
+ defaultVal: true )
67
109
];
68
110
69
111
Widget generateListItem (int index, BuildContext context) {
70
112
SettingsPageItem item = items[index];
113
+ item.onInitialized (item);
71
114
switch (item.type) {
72
- case SettingsPageItemType .TypeStringList :
115
+ case SettingsPageItemType .typeStringList :
73
116
var list = (item.provided as List <String >);
74
117
return ListTile (
75
118
title: Text (item.name),
@@ -88,7 +131,7 @@ class SettingsPageState extends State<SettingsPage> {
88
131
return ListTile (
89
132
title: Text (list[index]),
90
133
onTap: () {
91
- item.onSet (list[index]);
134
+ item.onSet (item, list[index]);
92
135
item.currentVal = list[index];
93
136
},
94
137
);
@@ -108,17 +151,24 @@ class SettingsPageState extends State<SettingsPage> {
108
151
);
109
152
},
110
153
);
111
- break ;
112
- case SettingsPageItemType .TypeString :
113
- // TODO: Handle this case.
114
- break ;
115
- case SettingsPageItemType .TypeBoolean :
154
+ case SettingsPageItemType .typeString:
116
155
// TODO: Handle this case.
117
156
break ;
118
- case SettingsPageItemType .TypeInteger :
157
+ case SettingsPageItemType .typeBoolean:
158
+ return CheckboxListTile (
159
+ title: Text (item.name),
160
+ subtitle: Text (item.description),
161
+ value: item.currentVal ?? item.defaultVal,
162
+ onChanged: (bool ? val) {
163
+ item.onSet (item, val);
164
+ setState (() {
165
+ item.currentVal = val;
166
+ });
167
+ });
168
+ case SettingsPageItemType .typeInteger:
119
169
// TODO: Handle this case.
120
170
break ;
121
- case SettingsPageItemType .TypeFloat :
171
+ case SettingsPageItemType .typeFloat :
122
172
// TODO: Handle this case.
123
173
break ;
124
174
}
@@ -136,48 +186,59 @@ class SettingsPageState extends State<SettingsPage> {
136
186
tabs: tabs,
137
187
),
138
188
),
139
- body: FutureBuilder (builder: (BuildContext context,
140
- AsyncSnapshot <String > snapshot,){
141
- return TabBarView (
142
- children: [
143
- /// General Page
144
- getSettingsTabContent (context),
145
- /// Plugins Page
146
- Column (children: [
147
- if (snapshot.hasData)
148
- Visibility (
149
- visible: snapshot.hasData,
150
- child: Text (
151
- snapshot.data! ,
189
+ body: FutureBuilder (
190
+ builder: (
191
+ BuildContext context,
192
+ AsyncSnapshot <String > snapshot,
193
+ ) {
194
+ return TabBarView (
195
+ children: [
196
+ /// General Page
197
+ getSettingsTabContent (context),
198
+
199
+ /// Plugins Page
200
+ Column (children: [
201
+ if (snapshot.hasData)
202
+ Visibility (
203
+ visible: snapshot.hasData,
204
+ child: Text (
205
+ snapshot.data! ,
206
+ ),
207
+ ),
208
+ ListTile (
209
+ leading: const Icon (
210
+ Icons .download,
211
+ size: 48 ,
212
+ ),
213
+ title: const Text ("Download Plugins" ),
214
+ subtitle: const Text ("Get plugins from the internet" ),
215
+ onTap: () {
216
+ Navigator .pushNamed (
217
+ context, PluginsBrowser .routeName);
218
+ },
152
219
),
153
- ),
154
- ListTile (
155
- leading: const Icon (Icons .download,size: 48 ,),
156
- title: const Text ("Download Plugins" ),
157
- subtitle: const Text ("Get plugins from the internet" ),
158
- onTap: () {
159
- Navigator .pushNamed (context, PluginsBrowser .routeName);
160
- },
161
- ),
162
- ListTile (
163
- leading: const Icon (Icons .refresh,size: 48 ,),
164
- title: const Text ("Reload Plugins" ),
165
- subtitle: const Text ("Reload plugins from the disk" ),
166
- onTap: () {
167
- widget.modulesManager.initialize (context);
168
- setState (() {});
169
- },
170
- ),
171
- const Text ("Installed Plugins" ),
172
- Column (children: List .generate (ModulesManager .modules.length, (index) {
173
- var mod = ModulesManager .modules[index];
174
- return ListTile (
175
- onTap: () {
176
- },
220
+ ListTile (
221
+ leading: const Icon (
222
+ Icons .refresh,
223
+ size: 48 ,
224
+ ),
225
+ title: const Text ("Reload Plugins" ),
226
+ subtitle: const Text ("Reload plugins from the disk" ),
227
+ onTap: () {
228
+ widget.modulesManager.initialize (context);
229
+ setState (() {});
230
+ },
231
+ ),
232
+ const Text ("Installed Plugins" ),
233
+ Column (
234
+ children: List .generate (ModulesManager .modules.length,
235
+ (index) {
236
+ var mod = ModulesManager .modules[index];
237
+ return ListTile (
238
+ onTap: () {},
177
239
leading: mod.icon,
178
240
title: Text (mod.name),
179
- subtitle: Text (
180
- mod.desc + " version:" + mod.version),
241
+ subtitle: Text (mod.desc + " version:" + mod.version),
181
242
// trailing: PopupMenuButton<String>(
182
243
// onSelected: (String result) {
183
244
// switch (result) {
@@ -248,17 +309,20 @@ class SettingsPageState extends State<SettingsPage> {
248
309
// ),
249
310
// ],
250
311
// ))
251
- );
252
- }))
253
- ]),
254
- /// About page
255
- ListTile (
256
- leading: Image .asset ("assets/logo.png" ),
257
- title: const Text ("CoreCoder Develop" ),
258
- subtitle: const Text (CoreCoderApp .version),
259
- )
260
- ],
261
- );
262
- },future: PluginsManager .pluginsPath,)));
312
+ );
313
+ }))
314
+ ]),
315
+
316
+ /// About page
317
+ ListTile (
318
+ leading: Image .asset ("assets/logo.png" ),
319
+ title: const Text ("CoreCoder Develop" ),
320
+ subtitle: const Text (CoreCoderApp .version),
321
+ )
322
+ ],
323
+ );
324
+ },
325
+ future: PluginsManager .pluginsPath,
326
+ )));
263
327
}
264
328
}
0 commit comments