Showing
1 changed file
with
36 additions
and
35 deletions
1 | # Get | 1 | # Get |
2 | 2 | ||
3 | -Get is an extra-light and powerful library for Flutter that will give you superpowers and increase your productivity. | ||
4 | -Navigate without context, open dialogs, snackbars or bottomsheets from anywhere in your code in an easy and practical way! | ||
5 | -Get is secure, stable, up-to-date, and offers a huge range of APIs that are not present on default framework. | ||
6 | - | 3 | +Get is an extra-light and powerful library for Flutter that will give you superpowers and increase your productivity. Navigate without context, open dialogs, snackbars or bottomsheets from anywhere in your code in an easy and practical way! Get is secure, stable, up-to-date, and offers a huge range of APIs that are not present on default framework. |
7 | ```dart | 4 | ```dart |
8 | // Default Flutter navigator | 5 | // Default Flutter navigator |
9 | Navigator.of(context).push( | 6 | Navigator.of(context).push( |
@@ -19,30 +16,29 @@ Navigator.of(context).push( | @@ -19,30 +16,29 @@ Navigator.of(context).push( | ||
19 | Get.to(Home()); | 16 | Get.to(Home()); |
20 | ``` | 17 | ``` |
21 | *Languages: [English](README.md), [Brazilian Portuguese](README.pt-br.md).* | 18 | *Languages: [English](README.md), [Brazilian Portuguese](README.pt-br.md).* |
22 | - | ||
23 | ## Getting Started | 19 | ## Getting Started |
24 | 20 | ||
25 | Flutter's conventional navigation has a lot of unnecessary boilerplate, requires context to navigate between screens, open dialogs, and use snackbars on framework is really boring. | 21 | Flutter's conventional navigation has a lot of unnecessary boilerplate, requires context to navigate between screens, open dialogs, and use snackbars on framework is really boring. |
26 | In addition, when a route is pushed, the entire MaterialApp can be rebuilt causing freezes, this does not happen with Get. | 22 | In addition, when a route is pushed, the entire MaterialApp can be rebuilt causing freezes, this does not happen with Get. |
27 | -This library that will change the way you work with the Framework and save your life from boilerplate, increasing your productivity, and eliminating the rebuild bugs of your application. | 23 | +This library that will change the way you work with the Framework and save your life from cliche code, increasing your productivity, and eliminating the rebuild bugs of your application. |
28 | 24 | ||
29 | ## How to use? | 25 | ## How to use? |
30 | 26 | ||
31 | -- Flutter Master/Dev/Beta: version 2.0.0-dev | ||
32 | -- Flutter Stable branch: version 2.0.0 | 27 | +- Flutter Master/Dev/Beta: version 2.0.1-dev |
28 | +- Flutter Stable branch: version 2.0.1 | ||
33 | 29 | ||
34 | Add this to your package's pubspec.yaml file: | 30 | Add this to your package's pubspec.yaml file: |
35 | 31 | ||
36 | ``` | 32 | ``` |
37 | dependencies: | 33 | dependencies: |
38 | - get: ^2.0.0 // ^2.0.0-dev on beta/dev/master | 34 | + get: ^2.0.1 // ^2.0.1-dev on beta/dev/master |
39 | ``` | 35 | ``` |
40 | Exchange your MaterialApp for GetMaterialApp and enjoy! | 36 | Exchange your MaterialApp for GetMaterialApp and enjoy! |
41 | ```dart | 37 | ```dart |
42 | import 'package:get/get.dart'; | 38 | import 'package:get/get.dart'; |
43 | GetMaterialApp( // Before: MaterialApp( | 39 | GetMaterialApp( // Before: MaterialApp( |
44 | home: MyHome(), | 40 | home: MyHome(), |
45 | - ) | 41 | +) |
46 | ``` | 42 | ``` |
47 | ### Navigating without named routes | 43 | ### Navigating without named routes |
48 | To navigate to a new screen: | 44 | To navigate to a new screen: |
@@ -254,39 +250,35 @@ class Controller extends GetController { | @@ -254,39 +250,35 @@ class Controller extends GetController { | ||
254 | } | 250 | } |
255 | } | 251 | } |
256 | // On your Stateless/Stateful class, use GetBuilder to update Text when increment be called | 252 | // On your Stateless/Stateful class, use GetBuilder to update Text when increment be called |
257 | -GetBuilder( | ||
258 | - controller: controller, | 253 | +GetBuilder<Controller>( |
254 | + init: Controller(), // INIT IT ONLY THE FIRST TIME | ||
259 | builder: (_) => Text( | 255 | builder: (_) => Text( |
260 | '${_.counter}', | 256 | '${_.counter}', |
261 | )), | 257 | )), |
258 | +//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. | ||
262 | ``` | 259 | ``` |
263 | **Done!** | 260 | **Done!** |
264 | - You have already learned how to manage states with Get. | 261 | - You have already learned how to manage states with Get. |
265 | 262 | ||
266 | ### Global State manager | 263 | ### Global State manager |
267 | 264 | ||
268 | -If you navigate many routes and need data that was in your previously used controller, you just need to send Get to find the controller in memory for you! | 265 | +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): |
269 | 266 | ||
270 | ```dart | 267 | ```dart |
271 | class OtherClasse extends StatelessWidget { | 268 | class OtherClasse extends StatelessWidget { |
272 | @override | 269 | @override |
273 | Widget build(BuildContext context) { | 270 | Widget build(BuildContext context) { |
274 | - Controller controller = Get.find(); | ||
275 | return Scaffold( | 271 | return Scaffold( |
276 | body: Center( | 272 | body: Center( |
277 | - child: GetBuilder( | ||
278 | - controller: controller, | ||
279 | - builder: (_) => Text( | ||
280 | - '${_.counter}', | ||
281 | - style: Theme.of(context).textTheme.headline4, | ||
282 | - ), | 273 | + child: GetBuilder<Controller>( |
274 | + builder: (s) => Text('${s.counter}'), | ||
283 | ), | 275 | ), |
284 | ), | 276 | ), |
285 | ); | 277 | ); |
286 | } | 278 | } |
287 | 279 | ||
288 | ``` | 280 | ``` |
289 | -If you don't want to instantiate your controller whenever you need to use it, you can create a get directly on your controller and access it statically. | 281 | +If you need to use your controller in many other places, and outside of ReBuilder, just create a get in your controller and have it easily. You can copy this code and just change the name of your controller. |
290 | 282 | ||
291 | ```dart | 283 | ```dart |
292 | class Controller extends GetController { | 284 | class Controller extends GetController { |
@@ -301,8 +293,10 @@ class Controller extends GetController { | @@ -301,8 +293,10 @@ class Controller extends GetController { | ||
301 | And then you can access your controller directly, that way: | 293 | And then you can access your controller directly, that way: |
302 | ```dart | 294 | ```dart |
303 | FloatingActionButton( | 295 | FloatingActionButton( |
304 | - onPressed: Controller.to.increment, // This is incredibly simple! | ||
305 | - child: Icon(Icons.add), | 296 | + onPressed:(){ |
297 | + Controller.to.increment(), | ||
298 | + } // This is incredibly simple! | ||
299 | + child: Text("${Controller.to.counter}"), | ||
306 | ), | 300 | ), |
307 | ``` | 301 | ``` |
308 | When you press FloatingActionButton, all widgets that are listening to the 'counter' variable will be updated automatically. | 302 | When you press FloatingActionButton, all widgets that are listening to the 'counter' variable will be updated automatically. |
@@ -310,31 +304,38 @@ When you press FloatingActionButton, all widgets that are listening to the 'coun | @@ -310,31 +304,38 @@ When you press FloatingActionButton, all widgets that are listening to the 'coun | ||
310 | ##### Different forms of use: | 304 | ##### Different forms of use: |
311 | 305 | ||
312 | You can use controlador instance directly on GetBuilder: | 306 | You can use controlador instance directly on GetBuilder: |
313 | -```dart | ||
314 | -GetBuilder( | ||
315 | - controller: Controller(), //here | ||
316 | - builder: (_) => Text( | ||
317 | - '${_.counter}', | ||
318 | - )), | ||
319 | -``` | ||
320 | -You can type your GetBuilder to access the IDE's autocomplete | 307 | + |
321 | ```dart | 308 | ```dart |
322 | GetBuilder<Controller>( //here | 309 | GetBuilder<Controller>( //here |
323 | - controller: Controller(), | 310 | + init: Controller(), |
324 | builder: (value) => Text( | 311 | builder: (value) => Text( |
325 | '${value.counter}', //here | 312 | '${value.counter}', //here |
326 | )), | 313 | )), |
327 | ``` | 314 | ``` |
328 | 315 | ||
329 | -You can to use controller instance: | 316 | +Or use controller instance: |
330 | ```dart | 317 | ```dart |
331 | Controller controller = Controller(); | 318 | Controller controller = Controller(); |
332 | [...] | 319 | [...] |
333 | -GetBuilder( | ||
334 | - controller: controller, //here | 320 | +GetBuilder( // you dont need to type on this way |
321 | + init: controller, //here | ||
335 | builder: (_) => Text( | 322 | builder: (_) => Text( |
336 | '${controller.counter}', // here | 323 | '${controller.counter}', // here |
337 | )), | 324 | )), |
325 | + | ||
326 | +``` | ||
327 | +Recommended: | ||
328 | +```dart | ||
329 | +class Controller extends GetController { | ||
330 | + static Controller get to => Get.find(); | ||
331 | +[...] | ||
332 | +} | ||
333 | +// on stateful/stateless class | ||
334 | +GetBuilder<Controller>( | ||
335 | + init: Controller(), // use it only first time on each controller | ||
336 | + builder: (_) => Text( | ||
337 | + '${Controller.to.counter}', //here | ||
338 | + )), | ||
338 | ``` | 339 | ``` |
339 | 340 | ||
340 | 341 |
-
Please register or login to post a comment