-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
353 lines (300 loc) · 10.1 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
// ignore_for_file: avoid_single_cascade_in_expression_statements, invalid_override, mixin_application_not_implemented_interface, non_abstract_class_inherits_abstract_member, unused_element
//=========================================================
// This file works as both a demonstration and tests.
// Your editor shows no error if lint warnings are being
// successfully suppressed by the `expect_lint` comments.
//=========================================================
import 'package:flutter/material.dart';
import 'package:grab/grab.dart';
final valueNotifier = ValueNotifier(0);
final changeNotifier1 = MyChangeNotifier1(0);
final changeNotifier2 = MyChangeNotifier2(0);
final changeNotifier3 = MyChangeNotifier3(0);
final textController = TextEditingController();
class MyChangeNotifier1 extends ChangeNotifier {
MyChangeNotifier1(this.value);
final int value;
}
class MyChangeNotifier2 extends MyChangeNotifier1 {
MyChangeNotifier2(super.value);
}
class MyChangeNotifier3 with ChangeNotifier {
MyChangeNotifier3(this.value);
final int value;
}
void main() => runApp(const App());
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_StatelessWidget1(),
_StatelessWidget2(),
_StatelessWidget3(),
_StatelessWidget4(),
_StatefulWidget1(),
_StatefulWidget2(),
_StatefulWidget3(),
_StatefulWidget4(),
],
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () => valueNotifier.value++,
),
),
);
}
}
// expect_lint: unnecessary_grab_mixin
class _StatelessWidget1 extends StatelessWidget with Grab {
const _StatelessWidget1();
@override
Widget build(BuildContext context) {
return const SizedBox.shrink();
}
}
class _StatelessWidget2 extends StatelessWidget with Grab {
const _StatelessWidget2();
@override
Widget build(BuildContext context) {
// Using grab in an if condition or a block is fine.
if (valueNotifier.grab(context).isEven) {
valueNotifier.grab(context);
}
int localFunc1() {
// It is acceptable to using the BuildContext parameter
// in a local function.
return valueNotifier.grab(context);
}
int localFunc2(BuildContext context) {
// Warned if the BuildContext param is not directly used.
// expect_lint: maybe_wrong_build_context_for_grab
return valueNotifier.grab(context);
}
final ctx = context;
// The BuildContext parameter of the build method should be used directly.
// expect_lint: maybe_wrong_build_context_for_grab
valueNotifier.grab(ctx);
return Center(
child: SizedBox.square(
dimension: 500.0,
child: Column(
children: [
Padding(
// Using grab in a deeply nested location is fine.
padding: EdgeInsets.all(valueNotifier.grab(context).toDouble()),
child: Text('${valueNotifier.grab(context)}'),
),
...[
Text('${valueNotifier.grab(context)}'),
],
Builder(
builder: (_) {
// It is acceptable to using the BuildContext parameter
// in a callback function.
// However, note that this causes a rebuild of the parent
// widget as well as this Builder.
return Text('${valueNotifier.grab(context)}');
},
),
Builder(
builder: (context) {
// expect_lint: maybe_wrong_build_context_for_grab
return Text('${valueNotifier.grab(context)}');
},
),
Builder(
builder: (context2) {
final context = context2;
// expect_lint: maybe_wrong_build_context_for_grab
return Text('${valueNotifier.grab(context)}');
},
),
],
),
),
);
}
}
// expect_lint: wrong_grab_mixin
class _StatelessWidget3 extends StatelessWidget with Grabful {
const _StatelessWidget3();
@override
Widget build(BuildContext context) {
_func(context);
// expect_lint: missing_grab_mixin
return Text('${valueNotifier.grab(context)}');
}
void _func(BuildContext context) {
// expect_lint: avoid_grab_outside_build
valueNotifier.grab(context);
}
}
class _StatelessWidget4 extends StatelessWidget {
const _StatelessWidget4();
@override
Widget build(BuildContext context) {
// Either Grab or StatelessGrabMixin is necessary.
// expect_lint: missing_grab_mixin
final count = valueNotifier.grab(context);
// expect_lint: missing_grab_mixin
changeNotifier1.grab(context);
// expect_lint: missing_grab_mixin
changeNotifier2.grab(context);
// expect_lint: missing_grab_mixin
changeNotifier3.grab(context);
// expect_lint: missing_grab_mixin
textController.grab(context);
// expect_lint: missing_grab_mixin
valueNotifier.grabAt(context, (v) => v);
// expect_lint: missing_grab_mixin
changeNotifier1.grabAt(context, (MyChangeNotifier1 n) => n.value);
// expect_lint: missing_grab_mixin
changeNotifier2.grabAt(context, (MyChangeNotifier2 n) => n.value);
// expect_lint: missing_grab_mixin
changeNotifier3.grabAt(context, (MyChangeNotifier3 n) => n.value);
// expect_lint: missing_grab_mixin
valueNotifier..grab(context);
return Center(
child: Text('$count'),
);
}
}
// expect_lint: unnecessary_grab_mixin
class _StatefulWidget1 extends StatefulWidget with Grabful {
const _StatefulWidget1();
@override
State<_StatefulWidget1> createState() => _StatefulWidget1State();
}
class _StatefulWidget1State extends State<_StatefulWidget1> {
@override
Widget build(BuildContext context) {
return const SizedBox.shrink();
}
}
class _StatefulWidget2 extends StatefulWidget with Grabful {
const _StatefulWidget2();
@override
State<_StatefulWidget2> createState() => _StatefulWidget2State();
}
class _StatefulWidget2State extends State<_StatefulWidget2> {
@override
Widget build(BuildContext context) {
// Using grab in an if condition or a block is fine.
if (valueNotifier.grab(context).isEven) {
valueNotifier.grab(context);
}
int localFunc1() {
// It is acceptable to using the BuildContext parameter
// in a local function.
return valueNotifier.grab(context);
}
int localFunc2(BuildContext context) {
// Warned if the BuildContext param is not directly used.
// expect_lint: maybe_wrong_build_context_for_grab
return valueNotifier.grab(context);
}
final ctx = context;
// The BuildContext parameter of the build method should be used directly.
// expect_lint: maybe_wrong_build_context_for_grab
valueNotifier.grab(ctx);
return Center(
child: SizedBox.square(
dimension: 500.0,
child: Column(
children: [
Padding(
// Using grab in a deeply nested location is fine.
padding: EdgeInsets.all(
valueNotifier.grab(context).toDouble(),
),
child: Text('${valueNotifier.grab(context)}'),
),
...[
Text('${valueNotifier.grab(context)}'),
],
Builder(
builder: (_) {
// It is acceptable to using the BuildContext parameter
// in a callback function.
// However, note that this causes a rebuild of the parent
// widget as well as this Builder.
return Text('${valueNotifier.grab(context)}');
},
),
Builder(
builder: (context) {
// expect_lint: maybe_wrong_build_context_for_grab
return Text('${valueNotifier.grab(context)}');
},
),
Builder(
builder: (context2) {
final context = context2;
// expect_lint: maybe_wrong_build_context_for_grab
return Text('${valueNotifier.grab(context)}');
},
),
],
),
),
);
}
}
// expect_lint: wrong_grab_mixin
class _StatefulWidget3 extends StatefulWidget with Grab {
const _StatefulWidget3();
@override
State<_StatefulWidget3> createState() => _StatefulWidget3State();
}
class _StatefulWidget3State extends State<_StatefulWidget3> {
@override
Widget build(BuildContext context) {
_func(context);
// expect_lint: missing_grab_mixin
return Text('${valueNotifier.grab(context)}');
}
void _func(BuildContext context) {
// expect_lint: avoid_grab_outside_build
valueNotifier.grab(context);
}
}
class _StatefulWidget4 extends StatefulWidget {
const _StatefulWidget4();
@override
State<_StatefulWidget4> createState() => _StatefulWidget4State();
}
class _StatefulWidget4State extends State<_StatefulWidget4> {
@override
Widget build(BuildContext context) {
// Either Grabful or StatefulGrabMixin is necessary.
// expect_lint: missing_grab_mixin
final count = valueNotifier.grab(context);
// expect_lint: missing_grab_mixin
changeNotifier1.grab(context);
// expect_lint: missing_grab_mixin
changeNotifier2.grab(context);
// expect_lint: missing_grab_mixin
changeNotifier3.grab(context);
// expect_lint: missing_grab_mixin
textController.grab(context);
// expect_lint: missing_grab_mixin
valueNotifier.grabAt(context, (v) => v);
// expect_lint: missing_grab_mixin
changeNotifier1.grabAt(context, (MyChangeNotifier1 n) => n.value);
// expect_lint: missing_grab_mixin
changeNotifier2.grabAt(context, (MyChangeNotifier2 n) => n.value);
// expect_lint: missing_grab_mixin
changeNotifier3.grabAt(context, (MyChangeNotifier3 n) => n.value);
// expect_lint: missing_grab_mixin
valueNotifier..grab(context);
return Center(
child: Text('$count'),
);
}
}