-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.md
276 lines (235 loc) · 7.56 KB
/
README.md
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
# nester
Flutter library to convert a list of widgets in a nested group of
widgets.
A beautifier plugin to easing your code syntax.
## Features
- Ease the code syntax
- Can manage like a generic list
- Can manage like a queue
## Installation
- Add the dependency
```
dependencies:
nester: ^1.2.1
```
- Import the package
```
import 'package:nester/nester.dart';
```
## Example Usage
#### NESTER LIST
Will treated the as simple list. You are allowed to have just one
**"next"**
reference for each item in the list.
This is the most simple way to use **Nester**.
Recommended when you mostly have "one child" Widget inside your list.
###### Original code
```dart
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text("Example"),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: Container(
color: Colors.black12,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[Text("Just a description")],
),
),
),
),
);
```
###### Using Nester
```dart
return Nester.list([
(next) => MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: next,
),
(next) => Scaffold(
appBar: AppBar(title: const Text("Example")),
body: next,
),
(next) => Padding(
padding: const EdgeInsets.all(20),
child: next,
),
(next) => Container(
color: Colors.black12,
child: next,
),
(next) => Center(child: next),
(_) => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[Text("Just a text")],
),
]);
```
**NOTE:** The last `next` item in list will be always an empty
`Container` object. So is useless to pass it.
---
#### NESTER QUEUE
The Widget list will be treated like a queue.
Every **"next"** calling will consume the next item in the list.
This is useful when you using "multi-child" Widgets inside your list.
###### Original code
```dart
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: Scaffold(
appBar: AppBar(
title: const Text("Example"),
),
body: Padding(
padding: const EdgeInsets.all(50),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: const [
Text("Left", textAlign: TextAlign.left),
Text("Center", textAlign: TextAlign.center),
Text("Right", textAlign: TextAlign.right),
],
),
),
),
);
```
###### Using Nester
```dart
return Nester.queue([
(next) => MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: next(),
),
(next) => Scaffold(
appBar: next() as PreferredSizeWidget,
body: next(),
),
(_) => AppBar(title: const Text("Example")),
(next) => Padding(
padding: const EdgeInsets.all(50),
child: next(),
),
(next) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: next(take: 3), // or [ next(), next(), next() ],
),
(_) => const Text("Left", textAlign: TextAlign.left),
(_) => const Text("Center", textAlign: TextAlign.center),
(_) => const Text("Right", textAlign: TextAlign.right),
]);
```
**Skip and Take**
The `next({int skip, int take})` function accept two parameters.
* Param `skip` will skip n calls on the queue list. Note that the
function wil NOT check for nested calling, just skip the next n items
inside the list. `skip` will be always applied before `take`.
* Param `take` will consuming n items on the same level. If a item have a
nested calling will not count as consumed. The result will be an array of
Widgets.
**Constraints**
* if `skip` and `take` are less than 0 will be considered 0
* if `skip` is not null or 0 and `take` is null an empty Container
will be returned.
**Throw Range Exception**
The `queue` constructor accept `throwRangeException` (default:
`false`) to avoid the RangeError check.
If `true` every calling of `next` then exceed the list boundaries will
throw a `RangeError` exception.
When use `take` param the resulting array will not exceed the number of
remaining items in list.
When NOT use `take` and the `next` calling exceed the list bounds an
empty `Container` will be returned.
---
##### Deep in queue mode
Using of `skip` and `take` parameters could create confusion so
write some examples to explain better them behavior.
Suppose the have a variable `trigger` boolean to use to skip some items
inside a `Column` widget.
```dart
return Nester.queue([
...,
(next) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
next(skip: trigger ? 0: 2),
next(),
next(),
]),
(next) => Padding(..., child: next()),
(_) => const Text("If trigger true"),
(_) => const Text("Always show"),
(_) => const Text("Always show"),
]);
```
**OR**
```dart
return Nester.queue([
...,
(next) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: []
..addAll(next(skip: trigger ? 0: 2, take: trigger ? 1: 0))
..addAll(next(take: 2)),
),
(next) => Padding(..., child: next()),
(_) => const Text("If trigger true"),
(_) => const Text("Always show"),
(_) => const Text("Always show"),
]);
```
If `trigger` is true `skip` 0 and `take` 1.
If `trigger` is false `skip` 2 and `take` 0.
**Why skip = 2?**
Because skip will not go inside the tree to check the nested items.
Just skip the items on the raw list.
So need to skip the next `Padding` and `Text` (*two items*).
Instead `take` will consume all the item inside the tree. Take 1 it
mean consuming the next `Padding` then will consume the next `Text`
(*two items*).
**Why addAll?**
Because when you use `take` parameter the result will be always an
Array of Widgets, also if taking is 0 or 1.
---
#### NESTER EXTENDED
A extension of **Queue**.
Will be treated exactly like `queue` but extended it adding an extra
param in the `next` function than will allow to pass a `dynamic` value
to the next calling.
```dart
return Nester.extended([
(next, _) => MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: next(),
),
(next, _) => Scaffold(
appBar: next(param: "Example") as PreferredSizeWidget,
body: next(),
),
(_, title) => AppBar(title: Text(title)),
(next, _) => Padding(
padding: const EdgeInsets.all(50),
child: next(),
),
(next, _) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: next(take: 3, param: ["Left", "Center", "Right"]),
),
(_, param) => Text(param[0], textAlign: TextAlign.left),
(_, param) => Text(param[1], textAlign: TextAlign.center),
(_, param) => Text(param[2], textAlign: TextAlign.right),
]);
```