Showing
1 changed file
with
133 additions
and
125 deletions
@@ -14,13 +14,13 @@ Get is an extra-light and powerful library for Flutter that will give you superp | @@ -14,13 +14,13 @@ Get is an extra-light and powerful library for Flutter that will give you superp | ||
14 | ```dart | 14 | ```dart |
15 | // Default Flutter navigator | 15 | // Default Flutter navigator |
16 | Navigator.of(context).push( | 16 | Navigator.of(context).push( |
17 | - context, | ||
18 | - MaterialPageRoute( | ||
19 | - builder: (BuildContext context) { | ||
20 | - return Home(); | ||
21 | - }, | ||
22 | - ), | ||
23 | - ); | 17 | + context, |
18 | + MaterialPageRoute( | ||
19 | + builder: (BuildContext context) { | ||
20 | + return Home(); | ||
21 | + }, | ||
22 | + ), | ||
23 | +); | ||
24 | 24 | ||
25 | // Get syntax | 25 | // Get syntax |
26 | Get.to(Home()); | 26 | Get.to(Home()); |
@@ -124,22 +124,22 @@ Example: | @@ -124,22 +124,22 @@ Example: | ||
124 | 124 | ||
125 | // Default Flutter navigator | 125 | // Default Flutter navigator |
126 | Navigator.of(context).push( | 126 | Navigator.of(context).push( |
127 | - context, | ||
128 | - MaterialPageRoute( | ||
129 | - builder: (BuildContext context) { | ||
130 | - return HomePage(); | ||
131 | - }, | ||
132 | - ), | ||
133 | - ); | 127 | + context, |
128 | + MaterialPageRoute( | ||
129 | + builder: (BuildContext context) { | ||
130 | + return HomePage(); | ||
131 | + }, | ||
132 | + ), | ||
133 | +); | ||
134 | 134 | ||
135 | // Get using Flutter syntax without needing context | 135 | // Get using Flutter syntax without needing context |
136 | navigator.push( | 136 | navigator.push( |
137 | - MaterialPageRoute( | ||
138 | - builder: (_) { | ||
139 | - return HomePage(); | ||
140 | - }, | ||
141 | - ), | ||
142 | - ); | 137 | + MaterialPageRoute( |
138 | + builder: (_) { | ||
139 | + return HomePage(); | ||
140 | + }, | ||
141 | + ), | ||
142 | +); | ||
143 | 143 | ||
144 | // Get syntax (It is much better, but you have the right to disagree) | 144 | // Get syntax (It is much better, but you have the right to disagree) |
145 | Get.to(HomePage()); | 145 | Get.to(HomePage()); |
@@ -152,14 +152,15 @@ Get.to(HomePage()); | @@ -152,14 +152,15 @@ Get.to(HomePage()); | ||
152 | To have a simple SnackBar with Flutter, you must get the context of Scaffold, or you must use a GlobalKey attached to your Scaffold, | 152 | To have a simple SnackBar with Flutter, you must get the context of Scaffold, or you must use a GlobalKey attached to your Scaffold, |
153 | ```dart | 153 | ```dart |
154 | final snackBar = SnackBar( | 154 | final snackBar = SnackBar( |
155 | - content: Text('Hi!'), | ||
156 | - action: SnackBarAction( | ||
157 | - label: 'I am a old and ugly snackbar :(', | ||
158 | - onPressed: (){} | ||
159 | - ), | ||
160 | - // Find the Scaffold in the widget tree and use | ||
161 | - // it to show a SnackBar. | ||
162 | - Scaffold.of(context).showSnackBar(snackBar); | 155 | + content: Text('Hi!'), |
156 | + action: SnackBarAction( | ||
157 | + label: 'I am a old and ugly snackbar :(', | ||
158 | + onPressed: (){} | ||
159 | + ), | ||
160 | +); | ||
161 | +// Find the Scaffold in the widget tree and use | ||
162 | +// it to show a SnackBar. | ||
163 | +Scaffold.of(context).showSnackBar(snackBar); | ||
163 | ``` | 164 | ``` |
164 | 165 | ||
165 | With Get: | 166 | With Get: |
@@ -171,16 +172,16 @@ Get.snackbar('Hi', 'i am a modern snackbar'); | @@ -171,16 +172,16 @@ Get.snackbar('Hi', 'i am a modern snackbar'); | ||
171 | With Get, all you have to do is call your Get.snackbar from anywhere in your code or customize it however you want! | 172 | With Get, all you have to do is call your Get.snackbar from anywhere in your code or customize it however you want! |
172 | 173 | ||
173 | ```dart | 174 | ```dart |
174 | - Get.snackbar( | ||
175 | - "Hey i'm a Get SnackBar!", // title | ||
176 | - "It's unbelievable! I'm using SnackBar without context, without boilerplate, without Scaffold, it is something truly amazing!", // message | ||
177 | - icon: Icon(Icons.alarm), | ||
178 | - shouldIconPulse: true, | ||
179 | - onTap:(){}, | ||
180 | - barBlur: 20, | ||
181 | - isDismissible: true, | ||
182 | - duration: Duration(seconds: 3), | ||
183 | - ); | 175 | +Get.snackbar( |
176 | + "Hey i'm a Get SnackBar!", // title | ||
177 | + "It's unbelievable! I'm using SnackBar without context, without boilerplate, without Scaffold, it is something truly amazing!", // message | ||
178 | + icon: Icon(Icons.alarm), | ||
179 | + shouldIconPulse: true, | ||
180 | + onTap:(){}, | ||
181 | + barBlur: 20, | ||
182 | + isDismissible: true, | ||
183 | + duration: Duration(seconds: 3), | ||
184 | +); | ||
184 | 185 | ||
185 | 186 | ||
186 | ////////// ALL FEATURES ////////// | 187 | ////////// ALL FEATURES ////////// |
@@ -233,9 +234,10 @@ Get.dialog(YourDialogWidget()); | @@ -233,9 +234,10 @@ Get.dialog(YourDialogWidget()); | ||
233 | To open default dialog: | 234 | To open default dialog: |
234 | 235 | ||
235 | ```dart | 236 | ```dart |
236 | - Get.defaultDialog( | ||
237 | - onConfirm: () => print("Ok"), | ||
238 | - middleText: "Dialog made in 3 lines of code"); | 237 | +Get.defaultDialog( |
238 | + onConfirm: () => print("Ok"), | ||
239 | + middleText: "Dialog made in 3 lines of code" | ||
240 | +); | ||
239 | ``` | 241 | ``` |
240 | You can also use Get.generalDialog instead of showGeneralDialog. | 242 | You can also use Get.generalDialog instead of showGeneralDialog. |
241 | 243 | ||
@@ -248,24 +250,23 @@ Get.bottomSheet is like showModalBottomSheet, but don't need of context. | @@ -248,24 +250,23 @@ Get.bottomSheet is like showModalBottomSheet, but don't need of context. | ||
248 | 250 | ||
249 | ```dart | 251 | ```dart |
250 | Get.bottomSheet( | 252 | Get.bottomSheet( |
251 | - Container( | ||
252 | - child: Wrap( | ||
253 | - children: <Widget>[ | ||
254 | - ListTile( | ||
255 | - leading: Icon(Icons.music_note), | ||
256 | - title: Text('Music'), | ||
257 | - onTap: () => {} | ||
258 | - ), | ||
259 | - ListTile( | ||
260 | - leading: Icon(Icons.videocam), | ||
261 | - title: Text('Video'), | ||
262 | - onTap: () => {}, | ||
263 | - ), | ||
264 | - ], | 253 | + Container( |
254 | + child: Wrap( | ||
255 | + children: <Widget>[ | ||
256 | + ListTile( | ||
257 | + leading: Icon(Icons.music_note), | ||
258 | + title: Text('Music'), | ||
259 | + onTap: () => {} | ||
265 | ), | 260 | ), |
266 | - ); | ||
267 | - } | ||
268 | - ); | 261 | + ListTile( |
262 | + leading: Icon(Icons.videocam), | ||
263 | + title: Text('Video'), | ||
264 | + onTap: () => {}, | ||
265 | + ), | ||
266 | + ], | ||
267 | + ), | ||
268 | + ); | ||
269 | +); | ||
269 | ``` | 270 | ``` |
270 | 271 | ||
271 | ## Simple State Manager | 272 | ## Simple State Manager |
@@ -312,10 +313,11 @@ class Controller extends GetController { | @@ -312,10 +313,11 @@ class Controller extends GetController { | ||
312 | } | 313 | } |
313 | // On your Stateless/Stateful class, use GetBuilder to update Text when increment be called | 314 | // On your Stateless/Stateful class, use GetBuilder to update Text when increment be called |
314 | GetBuilder<Controller>( | 315 | GetBuilder<Controller>( |
315 | - init: Controller(), // INIT IT ONLY THE FIRST TIME | ||
316 | - builder: (_) => Text( | ||
317 | - '${_.counter}', | ||
318 | - )), | 316 | + init: Controller(), // INIT IT ONLY THE FIRST TIME |
317 | + builder: (_) => Text( | ||
318 | + '${_.counter}', | ||
319 | + ), | ||
320 | +) | ||
319 | //Initialize your controller only the first time. The second time you are using ReBuilder for the same controller, do not use it again. Your controller will be automatically removed from memory as soon as the widget that marked it as 'init' is deployed. You don't have to worry about that, Get will do it automatically, just make sure you don't start the same controller twice. | 321 | //Initialize your controller only the first time. The second time you are using ReBuilder for the same controller, do not use it again. Your controller will be automatically removed from memory as soon as the widget that marked it as 'init' is deployed. You don't have to worry about that, Get will do it automatically, just make sure you don't start the same controller twice. |
320 | ``` | 322 | ``` |
321 | **Done!** | 323 | **Done!** |
@@ -327,7 +329,7 @@ GetBuilder<Controller>( | @@ -327,7 +329,7 @@ GetBuilder<Controller>( | ||
327 | If you navigate many routes and need data that was in your previously used controller, you just need to use GetBuilder Again (with no init): | 329 | If you navigate many routes and need data that was in your previously used controller, you just need to use GetBuilder Again (with no init): |
328 | 330 | ||
329 | ```dart | 331 | ```dart |
330 | -class OtherClasse extends StatelessWidget { | 332 | +class OtherClass extends StatelessWidget { |
331 | @override | 333 | @override |
332 | Widget build(BuildContext context) { | 334 | Widget build(BuildContext context) { |
333 | return Scaffold( | 335 | return Scaffold( |
@@ -361,11 +363,11 @@ class Controller extends GetController { | @@ -361,11 +363,11 @@ class Controller extends GetController { | ||
361 | And then you can access your controller directly, that way: | 363 | And then you can access your controller directly, that way: |
362 | ```dart | 364 | ```dart |
363 | FloatingActionButton( | 365 | FloatingActionButton( |
364 | - onPressed:(){ | ||
365 | - Controller.to.increment(), | ||
366 | - } // This is incredibly simple! | ||
367 | - child: Text("${Controller.to.counter}"), | ||
368 | - ), | 366 | + onPressed: () { |
367 | + Controller.to.increment(), | ||
368 | + } // This is incredibly simple! | ||
369 | + child: Text("${Controller.to.counter}"), | ||
370 | +), | ||
369 | ``` | 371 | ``` |
370 | When you press FloatingActionButton, all widgets that are listening to the 'counter' variable will be updated automatically. | 372 | When you press FloatingActionButton, all widgets that are listening to the 'counter' variable will be updated automatically. |
371 | 373 | ||
@@ -379,10 +381,10 @@ If you need to call initState() or dispose() method for example, you can call th | @@ -379,10 +381,10 @@ If you need to call initState() or dispose() method for example, you can call th | ||
379 | 381 | ||
380 | ```dart | 382 | ```dart |
381 | GetBuilder<Controller>( | 383 | GetBuilder<Controller>( |
382 | - initState(_) => Controller.to.fetchApi(), | ||
383 | - dispose(_) => Controller.to.closeStreams(), | ||
384 | - builder: (s) => Text('${s.username}'), | ||
385 | - ), | 384 | + initState: (_) => Controller.to.fetchApi(), |
385 | + dispose: (_) => Controller.to.closeStreams(), | ||
386 | + builder: (s) => Text('${s.username}'), | ||
387 | +), | ||
386 | ``` | 388 | ``` |
387 | 389 | ||
388 | A much better approach than this is to use the onInit() and onClose() method directly from your controller. | 390 | A much better approach than this is to use the onInit() and onClose() method directly from your controller. |
@@ -406,17 +408,17 @@ Do not call a dispose method inside GetController, it will not do anything, reme | @@ -406,17 +408,17 @@ Do not call a dispose method inside GetController, it will not do anything, reme | ||
406 | 408 | ||
407 | ```dart | 409 | ```dart |
408 | class Controller extends GetController { | 410 | class Controller extends GetController { |
409 | -StreamController<User> user = StreamController<User>(); | ||
410 | -StreamController<String> name = StreamController<String>(); | 411 | + StreamController<User> user = StreamController<User>(); |
412 | + StreamController<String> name = StreamController<String>(); | ||
411 | 413 | ||
412 | -/// close stream = onClose method, not dispose. | ||
413 | -@override | ||
414 | -void onClose() { | ||
415 | - user.close(); | ||
416 | - name.close(); | ||
417 | - super.onClose(); | 414 | + /// close stream = onClose method, not dispose. |
415 | + @override | ||
416 | + void onClose() { | ||
417 | + user.close(); | ||
418 | + name.close(); | ||
419 | + super.onClose(); | ||
420 | + } | ||
418 | } | 421 | } |
419 | - | ||
420 | ``` | 422 | ``` |
421 | Controller life cycle: | 423 | Controller life cycle: |
422 | - onInit() where it is created. | 424 | - onInit() where it is created. |
@@ -430,11 +432,12 @@ Controller life cycle: | @@ -430,11 +432,12 @@ Controller life cycle: | ||
430 | You can use Controller instance directly on GetBuilder value: | 432 | You can use Controller instance directly on GetBuilder value: |
431 | 433 | ||
432 | ```dart | 434 | ```dart |
433 | -GetBuilder<Controller>( | ||
434 | - init: Controller(), | ||
435 | - builder: (value) => Text( | ||
436 | - '${value.counter}', //here | ||
437 | - )), | 435 | +GetBuilder<Controller>( |
436 | + init: Controller(), | ||
437 | + builder: (value) => Text( | ||
438 | + '${value.counter}', //here | ||
439 | + ), | ||
440 | +), | ||
438 | ``` | 441 | ``` |
439 | You may also need an instance of your controller outside of your GetBuilder, and you can use these approaches to achieve this: | 442 | You may also need an instance of your controller outside of your GetBuilder, and you can use these approaches to achieve this: |
440 | 443 | ||
@@ -445,35 +448,38 @@ class Controller extends GetController { | @@ -445,35 +448,38 @@ class Controller extends GetController { | ||
445 | } | 448 | } |
446 | // on stateful/stateless class | 449 | // on stateful/stateless class |
447 | GetBuilder<Controller>( | 450 | GetBuilder<Controller>( |
448 | - init: Controller(), // use it only first time on each controller | ||
449 | - builder: (_) => Text( | ||
450 | - '${Controller.to.counter}', //here | ||
451 | - )), | 451 | + init: Controller(), // use it only first time on each controller |
452 | + builder: (_) => Text( | ||
453 | + '${Controller.to.counter}', //here | ||
454 | + ) | ||
455 | +), | ||
456 | +``` | ||
452 | or | 457 | or |
453 | - | 458 | +```dart |
454 | class Controller extends GetController { | 459 | class Controller extends GetController { |
455 | // static Controller get to => Get.find(); // with no static get | 460 | // static Controller get to => Get.find(); // with no static get |
456 | [...] | 461 | [...] |
457 | } | 462 | } |
458 | // on stateful/stateless class | 463 | // on stateful/stateless class |
459 | GetBuilder<Controller>( | 464 | GetBuilder<Controller>( |
460 | - init: Controller(), // use it only first time on each controller | ||
461 | - builder: (_) => Text( | ||
462 | - '${Get.find<Controller>().counter}', //here | ||
463 | - )), | 465 | + init: Controller(), // use it only first time on each controller |
466 | + builder: (_) => Text( | ||
467 | + '${Get.find<Controller>().counter}', //here | ||
468 | + ), | ||
469 | +), | ||
464 | ``` | 470 | ``` |
465 | 471 | ||
466 | - You can use "non-canonical" approaches to do this. If you are using some other dependency manager, like get_it, modular, etc., and just want to deliver the controller instance, you can do this: | 472 | - You can use "non-canonical" approaches to do this. If you are using some other dependency manager, like get_it, modular, etc., and just want to deliver the controller instance, you can do this: |
467 | 473 | ||
468 | ```dart | 474 | ```dart |
469 | - | ||
470 | Controller controller = Controller(); | 475 | Controller controller = Controller(); |
471 | [...] | 476 | [...] |
472 | GetBuilder( // you dont need to type on this way | 477 | GetBuilder( // you dont need to type on this way |
473 | - init: controller, //here | ||
474 | - builder: (_) => Text( | ||
475 | - '${controller.counter}', // here | ||
476 | - )), | 478 | + init: controller, //here |
479 | + builder: (_) => Text( | ||
480 | + '${controller.counter}', // here | ||
481 | + ), | ||
482 | +), | ||
477 | 483 | ||
478 | ``` | 484 | ``` |
479 | This approach is not recommended, as you will have to manually dispose of your controllers, close your streams manually, and literally give up one of the great benefits of this library, which is intelligent memory control. But if you trust your potential, go ahead! | 485 | This approach is not recommended, as you will have to manually dispose of your controllers, close your streams manually, and literally give up one of the great benefits of this library, which is intelligent memory control. But if you trust your potential, go ahead! |
@@ -481,11 +487,12 @@ This approach is not recommended, as you will have to manually dispose of your c | @@ -481,11 +487,12 @@ This approach is not recommended, as you will have to manually dispose of your c | ||
481 | If you want to refine a widget's update control with GetBuilder, you can assign them unique IDs: | 487 | If you want to refine a widget's update control with GetBuilder, you can assign them unique IDs: |
482 | ```dart | 488 | ```dart |
483 | GetBuilder<Controller>( | 489 | GetBuilder<Controller>( |
484 | - id: 'text' | ||
485 | - init: Controller(), // use it only first time on each controller | ||
486 | - builder: (_) => Text( | ||
487 | - '${Get.find<Controller>().counter}', //here | ||
488 | - )), | 490 | + id: 'text' |
491 | + init: Controller(), // use it only first time on each controller | ||
492 | + builder: (_) => Text( | ||
493 | + '${Get.find<Controller>().counter}', //here | ||
494 | + ), | ||
495 | +), | ||
489 | ``` | 496 | ``` |
490 | And update it this form: | 497 | And update it this form: |
491 | ```dart | 498 | ```dart |
@@ -522,24 +529,24 @@ int get sum => count1.value + count2.value; | @@ -522,24 +529,24 @@ int get sum => count1.value + count2.value; | ||
522 | ``` | 529 | ``` |
523 | 530 | ||
524 | ```dart | 531 | ```dart |
525 | - GetX<Controller>( | ||
526 | - builder: (_) { | ||
527 | - print("count 1 rebuild"); | ||
528 | - return Text('${_.count1.value}'); | ||
529 | - }, | ||
530 | - ), | ||
531 | - GetX<Controller>( | ||
532 | - builder: (_) { | ||
533 | - print("count 2 rebuild"); | ||
534 | - return Text('${_.count2.value}'); | ||
535 | - }, | ||
536 | - ), | ||
537 | - GetX<Controller>( | ||
538 | - builder: (_) { | ||
539 | - print("count 3 rebuild"); | ||
540 | - return Text('${_.sum}'); | ||
541 | - }, | ||
542 | - ), | 532 | +GetX<Controller>( |
533 | + builder: (_) { | ||
534 | + print("count 1 rebuild"); | ||
535 | + return Text('${_.count1.value}'); | ||
536 | + }, | ||
537 | +), | ||
538 | +GetX<Controller>( | ||
539 | + builder: (_) { | ||
540 | + print("count 2 rebuild"); | ||
541 | + return Text('${_.count2.value}'); | ||
542 | + }, | ||
543 | +), | ||
544 | +GetX<Controller>( | ||
545 | + builder: (_) { | ||
546 | + print("count 3 rebuild"); | ||
547 | + return Text('${_.sum}'); | ||
548 | + }, | ||
549 | +), | ||
543 | ``` | 550 | ``` |
544 | 551 | ||
545 | If we increment the number of count 1, only count 1 and count 3 are reconstructed, because count 1 now has a value of 1, and 1 + 0 = 1, changing the sum value. | 552 | If we increment the number of count 1, only count 1 and count 3 are reconstructed, because count 1 now has a value of 1, and 1 + 0 = 1, changing the sum value. |
@@ -615,8 +622,9 @@ You can access list.length, or list.value.length. Most of the time, both ways wi | @@ -615,8 +622,9 @@ You can access list.length, or list.value.length. Most of the time, both ways wi | ||
615 | final list = List<User>().obs; | 622 | final list = List<User>().obs; |
616 | ``` | 623 | ``` |
617 | ```dart | 624 | ```dart |
618 | -ListView.builder ( | ||
619 | -itemCount: list.value.lenght | 625 | +ListView.builder( |
626 | + itemCount: list.value.lenght | ||
627 | +) | ||
620 | ``` | 628 | ``` |
621 | or else create a "get" method for it and abandon "value" for life. example: | 629 | or else create a "get" method for it and abandon "value" for life. example: |
622 | 630 |
-
Please register or login to post a comment