-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
356 lines (319 loc) · 8.47 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Bradley Honeyman
July 14th, 2022
This is an example of how to use memory_and_json_directories
This example includes all the code for:
Basic Example
Custom Item Example
Data and Shared Data Example
Multiple Trees Example
Building a Custom Directory Item
*/
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:memory_and_json_directories/memory_and_json_directories.dart';
import 'package:provider/provider.dart';
/// An example of how to create a custom directory, which will display
/// in a manner you desire
class CustomDirectory implements MAJItemInterface {
/// not required, but recommended
static const String typeName = "custom_directory";
@override
String getTypeName() {
return typeName;
}
@override
Widget majBuild({
required BuildContext context,
required MAJNode nodeReference,
}) {
// return custom directory widget
return CustomDirectoryWidget(
nodeReference: nodeReference,
context: context,
);
}
}
/// widget that displays the custom directory
class CustomDirectoryWidget extends StatefulWidget {
final MAJNode nodeReference;
final BuildContext context;
// get node reference, and context to pass to the state
const CustomDirectoryWidget({
super.key,
required this.nodeReference,
required this.context,
});
@override
State<StatefulWidget> createState() {
return CustomDirectoryWidgetState();
}
}
/// state of the custom directory
class CustomDirectoryWidgetState extends State<CustomDirectoryWidget> {
@override
Widget build(BuildContext context) {
/// builds the buttons that are displayed in the directory widget
List<Widget> buildButtons() {
List<Widget> temp = [];
// add back button
temp.add(
// back button, navigates to parent, unless there is not parent node
ElevatedButton(
onPressed: () {
if (widget.nodeReference.parent != null) {
context.read<MAJProvider>().navigateToByNode(
nodeTo: widget.nodeReference.parent!,
);
}
},
child: const Text("Back"),
),
);
// add children of the current directory
for (int i = 0; i < widget.nodeReference.children.length; i++) {
temp.add(
// build the node when button pressed
OutlinedButton(
onPressed: () {
context.read<MAJProvider>().navigateToByNode(
nodeTo: widget.nodeReference.children[i],
);
},
// display node's path
child: Text(
widget.nodeReference.children[i].path,
),
),
);
}
return temp;
}
// return column of buttons that when pressed load nodes
return Column(
children: buildButtons(),
);
}
}
/// A basic custom item that can display whatever you wish
class CustomItem implements MAJItemInterface {
/// not required, but recommended
static const String typeName = "custom_item";
@override
String getTypeName() {
return typeName;
}
@override
Widget majBuild({
required BuildContext context,
required MAJNode nodeReference,
}) {
// set default data value if no data
if (nodeReference.data == null || nodeReference.data!.keys.isEmpty) {
nodeReference.data = nodeReference.data = <String, bool>{
"pressed": false,
};
}
return StatefulBuilder(
builder: (context, setState) {
return Center(
child: Column(
children: [
// back button, navigates to parent, unless there is not parent node
ElevatedButton(
onPressed: () {
if (nodeReference.parent != null) {
context.read<MAJProvider>().navigateToByNode(
nodeTo: nodeReference.parent!,
);
}
},
child: const Text("Back"),
),
// custom widget to display
const Text("Hello I am a custom item"),
// add text that shows the shared data for CustomItem
Text(
nodeReference.getSharedData().toString(),
),
// add button that shows the data for the instance of CustomItem
OutlinedButton(
// update data value for instance on pressed to opposite of what it was
onPressed: () {
setState(() {
nodeReference.data!["pressed"] =
!nodeReference.data!["pressed"];
});
},
// display data for this instance
child: Text(
nodeReference.data!["pressed"].toString(),
),
),
],
),
);
},
);
}
}
void main() {
// define the custom item, so it can be loaded from json
MAJNode.addDefinition(
typeName: CustomItem.typeName,
item: () => CustomItem(),
);
// define the custom directory, so it can be loaded from json
MAJNode.addDefinition(
typeName: CustomDirectory.typeName,
item: () => CustomDirectory(),
);
// set shared data for custom Item in memory
// will be loaded from json below
MAJNode.setSharedData(
typeName: CustomItem.typeName,
data: {
"data": "Shared Data value",
},
);
// set shared data for custom Item in memory
// will be loaded from json below
// map key for tree 2 set
MAJNode.setSharedData(
typeName: CustomItem.typeName,
data: {
"data": "Shared Data value from tree 2",
},
mapKey: "tree_2",
);
// build a tree in memory
MAJNode root = MAJNode(
name: "root",
child: MAJDirectory(),
);
root
.addChild(
MAJNode(
name: "one",
child: MAJDirectory(),
),
)
.addChild(
MAJNode(
name: "one one level down",
child: MAJDirectory(),
),
);
root.addChild(
MAJNode(
name: "two",
child: MAJDirectory(),
),
);
// add a custom item to the tree
root.addChild(
MAJNode(
name: "Custom Item",
child: CustomItem(),
),
);
// add a second child to show that data can be different between nodes of the
// same type, and that shared data is shared
root.addChild(
MAJNode(
name: "Custom Item 2",
child: CustomItem(),
),
);
// convert to json
String treeAsJson = jsonEncode(
root.breadthFirstToJson(),
);
// load from json string
MAJNode fromJson = MAJNode.breadthFirstFromJson(
jsonDecode(
treeAsJson,
),
);
// create 2nd tree's root
MAJNode root2 = MAJNode(
name: "root 2",
child: MAJDirectory(),
mapKey: "tree_2", // because second tree, avoids naming collisions
);
root2
.addChild(
MAJNode(
name: "one",
child: MAJDirectory(),
mapKey: "tree_2",
),
)
.addChild(
MAJNode(
name: "One down from one",
child: CustomItem(),
mapKey: "tree_2",
),
);
// add custom dirs
root2.addChild(
MAJNode(
name: "Custom Dir 1",
child: CustomDirectory(),
mapKey: "tree_2",
),
)
// add to custom 1
..addChild(
MAJNode(
name: "Custom Dir 1",
child: CustomDirectory(),
mapKey: "tree_2",
),
)
// add to custom 1
..addChild(
MAJNode(
name: "Custom Dir 2",
child: CustomDirectory(),
mapKey: "tree_2",
),
// add to custom 2
).addChild(
MAJNode(
name: "Custom Dir 3",
child: CustomDirectory(),
mapKey: "tree_2",
),
);
// convert tree to json
String tree2FromJson = jsonEncode(
root2.breadthFirstToJson(),
);
// build second tree from json
MAJNode fromJson2 = MAJNode.breadthFirstFromJson(
jsonDecode(tree2FromJson),
);
// display the tree loaded from json
runApp(
MaterialApp(
home: Scaffold(
body: Column(
children: [
// tree 1
MAJBuilder(
root: fromJson,
),
// tree 2, see how it has a map key, this is so the name space
// doesn't collide with the first tree. tree 1 uses the default map key
MAJBuilder(
root: fromJson2,
mapKey: fromJson2.mapKey,
),
],
),
),
),
);
}