Showing
1 changed file
with
304 additions
and
254 deletions
@@ -55,22 +55,35 @@ class Other extends StatelessWidget { | @@ -55,22 +55,35 @@ class Other extends StatelessWidget { | ||
55 | 55 | ||
56 | This is a simple project but it already makes clear how powerful Get is. As your project grows, this difference will become more significant. Get was designed to work with teams, but it makes the job of an individual developer simple. Improve your deadlines, deliver everything on time without losing performance. Get is not for everyone, but if you identified with that phrase, Get is for you! | 56 | This is a simple project but it already makes clear how powerful Get is. As your project grows, this difference will become more significant. Get was designed to work with teams, but it makes the job of an individual developer simple. Improve your deadlines, deliver everything on time without losing performance. Get is not for everyone, but if you identified with that phrase, Get is for you! |
57 | 57 | ||
58 | -- [How to use?](#how-to-use) | ||
59 | -- [Navigating without named routes](#navigating-without-named-routes) | 58 | +- [State Management](#state-management) |
59 | + - [Simple State Manager](#simple-state-manager) | ||
60 | + - [Advantages of using it](#advantages-of-using-it) | ||
61 | + - [Simple state manager usage](#simple-state-manager-usage) | ||
62 | + - [How it handles controllers](#how-it-handles-controllers) | ||
63 | + - [You won't need StatefulWidgts anymore](#you-wont-need-statefulwidgts-anymore) | ||
64 | + - [Why it exists](#why-it-exists) | ||
65 | + - [Other ways of using it](#other-ways-of-using-it) | ||
66 | + - [Unique IDs](#unique-ids) | ||
67 | + - [Reactive State Manager](#reactive-state-manager) | ||
68 | + - [Advantages](#advantages) | ||
69 | + - [Usage:](#usage) | ||
70 | + - [Where .obs can be used](#where-obs-can-be-used) | ||
71 | + - [Note about Lists](#note-about-lists) | ||
72 | + - [Why i have to use .value?](#why-i-have-to-use-value) | ||
73 | + - [Obx()](#obx) | ||
74 | + - [Mixing the two state managers](#mixing-the-two-state-managers) | ||
75 | + - [GetX vs GetBuilder vs Obx vs MixinBuilder](#getx-vs-getbuilder-vs-obx-vs-mixinbuilder) | ||
76 | +- [Dependency Management](#dependency-management) | ||
77 | + - [How to use?](#how-to-use) | ||
78 | + - [Navigating without named routes](#navigating-without-named-routes) | ||
60 | - [SnackBars](#snackbars) | 79 | - [SnackBars](#snackbars) |
61 | - [Dialogs](#dialogs) | 80 | - [Dialogs](#dialogs) |
62 | - [BottomSheets](#bottomsheets) | 81 | - [BottomSheets](#bottomsheets) |
63 | -- [Simple State Manager](#simple-state-manager) | ||
64 | - - [Simple state manager usage](#simple-state-manager-usage) | ||
65 | - - [No StatefulWidget:](#no-statefulwidget) | ||
66 | - - [Forms of use:](#forms-of-use) | ||
67 | -- [Reactive State Manager](#reactive-state-manager) | ||
68 | - - [GetX vs GetBuilder vs Obx vs MixinBuilder](#getx-vs-getbuilder-vs-obx-vs-mixinbuilder) | ||
69 | -- [Simple Instance Manager](#simple-instance-manager) | ||
70 | -- [Bindings](#bindings) | 82 | + - [Simple Instance Manager](#simple-instance-manager) |
83 | + - [Bindings](#bindings) | ||
71 | - [To use this API you only need:](#to-use-this-api-you-only-need) | 84 | - [To use this API you only need:](#to-use-this-api-you-only-need) |
72 | -- [Workers:](#workers) | ||
73 | -- [Navigate with named routes:](#navigate-with-named-routes) | 85 | + - [Workers:](#workers) |
86 | + - [Navigate with named routes:](#navigate-with-named-routes) | ||
74 | - [Send data to named Routes:](#send-data-to-named-routes) | 87 | - [Send data to named Routes:](#send-data-to-named-routes) |
75 | - [Dynamic urls links](#dynamic-urls-links) | 88 | - [Dynamic urls links](#dynamic-urls-links) |
76 | - [Middleware](#middleware) | 89 | - [Middleware](#middleware) |
@@ -87,251 +100,41 @@ This is a simple project but it already makes clear how powerful Get is. As your | @@ -87,251 +100,41 @@ This is a simple project but it already makes clear how powerful Get is. As your | ||
87 | - Offering PRs for code/tests. | 100 | - Offering PRs for code/tests. |
88 | - Including new functions. | 101 | - Including new functions. |
89 | 102 | ||
90 | -Any contribution is welcome! | ||
91 | - | ||
92 | -## How to use? | ||
93 | - | ||
94 | -<!-- - Flutter Master/Dev/Beta: version 2.0.x-dev | ||
95 | -- Flutter Stable branch: version 2.0.x | ||
96 | -(look for latest version on pub.dev) --> | ||
97 | - | ||
98 | -Add this to your pubspec.yaml file: | ||
99 | - | ||
100 | -``` | ||
101 | -dependencies: | ||
102 | - get: | ||
103 | -``` | ||
104 | -If you are going to use routes/snackbars/dialogs/bottomsheets without context, or use the high-level Get APIs, you need to simply add "Get" before your MaterialApp, turning it into GetMaterialApp and enjoy! | ||
105 | - | ||
106 | -```dart | ||
107 | -GetMaterialApp( // Before: MaterialApp( | ||
108 | - home: MyHome(), | ||
109 | -) | ||
110 | -``` | ||
111 | -## Navigating without named routes | ||
112 | -To navigate to a new screen: | ||
113 | - | ||
114 | -```dart | ||
115 | -Get.to(NextScreen()); | ||
116 | -``` | ||
117 | - | ||
118 | -To close snackbars, dialogs, bottomsheets, or anything you would normally close with Navigator.pop(context); | ||
119 | - | ||
120 | -```dart | ||
121 | -Get.back(); | ||
122 | -``` | ||
123 | - | ||
124 | -To go to the next screen and no option to go back to the previous screen (for use in SplashScreens, login screens and etc.) | ||
125 | - | ||
126 | -```dart | ||
127 | -Get.off(NextScreen()); | ||
128 | -``` | ||
129 | - | ||
130 | -To go to the next screen and cancel all previous routes (useful in shopping carts, polls, and tests) | ||
131 | - | ||
132 | -```dart | ||
133 | -Get.offAll(NextScreen()); | ||
134 | -``` | ||
135 | - | ||
136 | -To navigate to the next route, and receive or update data as soon as you return from it: | ||
137 | -```dart | ||
138 | -var data = await Get.to(Payment()); | ||
139 | -``` | ||
140 | -on other screen, send a data for previous route: | ||
141 | - | ||
142 | -```dart | ||
143 | -Get.back(result: 'success'); | ||
144 | -``` | ||
145 | -And use it: | ||
146 | - | ||
147 | -ex: | ||
148 | -```dart | ||
149 | -if(data == 'success') madeAnything(); | ||
150 | -``` | ||
151 | 103 | ||
152 | -Don't you want to learn our syntax? | ||
153 | -Just change the Navigator (uppercase) to navigator (lowercase), and you will have all the functions of the standard navigation, without having to use context | ||
154 | -Example: | ||
155 | - | ||
156 | -```dart | 104 | +# State Management |
105 | +There are currently several state managers for Flutter. However, most of them involve using ChangeNotifier to update widgets and this is a bad and very bad approach to performance of medium or large applications. You can check in the official Flutter documentation that ChangeNotifier should be used with 1 or a maximum of 2 listeners (https://api.flutter.dev/flutter/foundation/ChangeNotifier-class.html), making it practically unusable for any application medium or large. | ||
157 | 106 | ||
158 | -// Default Flutter navigator | ||
159 | -Navigator.of(context).push( | ||
160 | - context, | ||
161 | - MaterialPageRoute( | ||
162 | - builder: (BuildContext context) { | ||
163 | - return HomePage(); | ||
164 | - }, | ||
165 | - ), | ||
166 | -); | 107 | +Other state managers are good, but have their nuances: |
108 | +- BLoC is very safe and efficient, but it is very complex for beginners, which has kept people from developing with Flutter. | ||
109 | +- MobX is easier than BLoC and reactive, almost perfect, I would say, but you need to use a code generator, that for large applications, reduces productivity, since you will need to drink a lot of coffees until your code is ready again after a `flutter clean` (And this is not MobX's fault, but the codegen which is really slow!). | ||
110 | +- Provider uses InheritedWidget to deliver the same listener, as a way of solving the problem reported above with ChangeNotifier, which implies that any access to its ChangeNotifier class must be within the widget tree because of the context to access o Inherited. | ||
167 | 111 | ||
168 | -// Get using Flutter syntax without needing context | ||
169 | -navigator.push( | ||
170 | - MaterialPageRoute( | ||
171 | - builder: (_) { | ||
172 | - return HomePage(); | ||
173 | - }, | ||
174 | - ), | ||
175 | -); | ||
176 | - | ||
177 | -// Get syntax (It is much better, but you have the right to disagree) | ||
178 | -Get.to(HomePage()); | ||
179 | - | ||
180 | - | ||
181 | -``` | ||
182 | - | ||
183 | -### SnackBars | ||
184 | - | ||
185 | -To have a simple SnackBar with Flutter, you must get the context of Scaffold, or you must use a GlobalKey attached to your Scaffold, | ||
186 | -```dart | ||
187 | -final snackBar = SnackBar( | ||
188 | - content: Text('Hi!'), | ||
189 | - action: SnackBarAction( | ||
190 | - label: 'I am a old and ugly snackbar :(', | ||
191 | - onPressed: (){} | ||
192 | - ), | ||
193 | -); | ||
194 | -// Find the Scaffold in the widget tree and use | ||
195 | -// it to show a SnackBar. | ||
196 | -Scaffold.of(context).showSnackBar(snackBar); | ||
197 | -``` | ||
198 | - | ||
199 | -With Get: | ||
200 | - | ||
201 | -```dart | ||
202 | -Get.snackbar('Hi', 'i am a modern snackbar'); | ||
203 | -``` | ||
204 | - | ||
205 | -With Get, all you have to do is call your Get.snackbar from anywhere in your code or customize it however you want! | ||
206 | - | ||
207 | -```dart | ||
208 | -Get.snackbar( | ||
209 | - "Hey i'm a Get SnackBar!", // title | ||
210 | - "It's unbelievable! I'm using SnackBar without context, without boilerplate, without Scaffold, it is something truly amazing!", // message | ||
211 | - icon: Icon(Icons.alarm), | ||
212 | - shouldIconPulse: true, | ||
213 | - onTap:(){}, | ||
214 | - barBlur: 20, | ||
215 | - isDismissible: true, | ||
216 | - duration: Duration(seconds: 3), | ||
217 | -); | ||
218 | - | ||
219 | - | ||
220 | - ////////// ALL FEATURES ////////// | ||
221 | - // Color colorText, | ||
222 | - // Duration duration, | ||
223 | - // SnackPosition snackPosition, | ||
224 | - // Widget titleText, | ||
225 | - // Widget messageText, | ||
226 | - // bool instantInit, | ||
227 | - // Widget icon, | ||
228 | - // bool shouldIconPulse, | ||
229 | - // double maxWidth, | ||
230 | - // EdgeInsets margin, | ||
231 | - // EdgeInsets padding, | ||
232 | - // double borderRadius, | ||
233 | - // Color borderColor, | ||
234 | - // double borderWidth, | ||
235 | - // Color backgroundColor, | ||
236 | - // Color leftBarIndicatorColor, | ||
237 | - // List<BoxShadow> boxShadows, | ||
238 | - // Gradient backgroundGradient, | ||
239 | - // FlatButton mainButton, | ||
240 | - // OnTap onTap, | ||
241 | - // bool isDismissible, | ||
242 | - // bool showProgressIndicator, | ||
243 | - // AnimationController progressIndicatorController, | ||
244 | - // Color progressIndicatorBackgroundColor, | ||
245 | - // Animation<Color> progressIndicatorValueColor, | ||
246 | - // SnackStyle snackStyle, | ||
247 | - // Curve forwardAnimationCurve, | ||
248 | - // Curve reverseAnimationCurve, | ||
249 | - // Duration animationDuration, | ||
250 | - // double barBlur, | ||
251 | - // double overlayBlur, | ||
252 | - // Color overlayColor, | ||
253 | - // Form userInputForm | ||
254 | - /////////////////////////////////// | ||
255 | -``` | ||
256 | -If you prefer the traditional snackbar, or want to customize it from scratch, including adding just one line (Get.snackbar makes use of a mandatory title and message), you can use | ||
257 | -`Get.rawSnackbar();` which provides the RAW API on which Get.snackbar was built. | ||
258 | - | ||
259 | -### Dialogs | ||
260 | - | ||
261 | -To open dialog: | ||
262 | - | ||
263 | -```dart | ||
264 | -Get.dialog(YourDialogWidget()); | ||
265 | -``` | ||
266 | - | ||
267 | -To open default dialog: | ||
268 | - | ||
269 | -```dart | ||
270 | -Get.defaultDialog( | ||
271 | - onConfirm: () => print("Ok"), | ||
272 | - middleText: "Dialog made in 3 lines of code" | ||
273 | -); | ||
274 | -``` | ||
275 | -You can also use Get.generalDialog instead of showGeneralDialog. | ||
276 | - | ||
277 | -For all other Flutter dialog widgets, including cupertinos, you can use Get.overlayContext instead of context, and open it anywhere in your code. | ||
278 | -For widgets that don't use Overlay, you can use Get.context. | ||
279 | -These two contexts will work in 99% of cases to replace the context of your UI, except for cases where inheritedWidget is used without a navigation context. | ||
280 | - | ||
281 | -### BottomSheets | ||
282 | -Get.bottomSheet is like showModalBottomSheet, but don't need of context. | ||
283 | - | ||
284 | -```dart | ||
285 | -Get.bottomSheet( | ||
286 | - Container( | ||
287 | - child: Wrap( | ||
288 | - children: <Widget>[ | ||
289 | - ListTile( | ||
290 | - leading: Icon(Icons.music_note), | ||
291 | - title: Text('Music'), | ||
292 | - onTap: () => {} | ||
293 | - ), | ||
294 | - ListTile( | ||
295 | - leading: Icon(Icons.videocam), | ||
296 | - title: Text('Video'), | ||
297 | - onTap: () => {}, | ||
298 | - ), | ||
299 | - ], | ||
300 | - ), | ||
301 | - ); | ||
302 | -); | ||
303 | -``` | ||
304 | - | ||
305 | -## Simple State Manager | ||
306 | -There are currently several state managers for Flutter. However, most of them involve using ChangeNotifier to update widgets and this is a bad and very bad approach to performance of medium or large applications. You can check in the official Flutter documentation that ChangeNotifier should be used with 1 or a maximum of 2 listeners (https://api.flutter.dev/flutter/foundation/ChangeNotifier-class.html), making it practically unusable for any application medium or large. Other state managers are good, but have their nuances. BLoC is very safe and efficient, but it is very complex for beginners, which has kept people from developing with Flutter. MobX is easier than BLoC and reactive, almost perfect, I would say, but you need to use a code generator that for large applications, reduces productivity, you will need to drink a lot of coffees until your code is ready again after a Flutter clean (And this is not MobX's fault, but the codegen which is really slow!). Provider uses InheritedWidget to deliver the same listener, as a way of solving the problem reported above with ChangeNotifier, which implies that any access to its ChangeNotifier class must be within the widget tree because of the context to access o Inherited. | ||
307 | 112 | ||
308 | Get isn't better or worse than any other state manager, but that you should analyze these points as well as the points below to choose between using Get in pure form (Vanilla), or using it in conjunction with another state manager. Definitely, Get is not the enemy of any other state manager, because Get is a microframework, not just a state manager, and can be used either alone or in conjunction with them. | 113 | Get isn't better or worse than any other state manager, but that you should analyze these points as well as the points below to choose between using Get in pure form (Vanilla), or using it in conjunction with another state manager. Definitely, Get is not the enemy of any other state manager, because Get is a microframework, not just a state manager, and can be used either alone or in conjunction with them. |
309 | 114 | ||
310 | -Get has a state manager that is extremely light and easy (written in just 95 lines of code), which does not use ChangeNotifier, will meet the need especially for those new to Flutter, and will not cause problems for large applications. | ||
311 | - | ||
312 | -What performance improvements does Get bring? | 115 | +## Simple State Manager |
313 | 116 | ||
314 | -1- Update only the required widget. | 117 | +Get has a state manager that is extremely light and easy, which does not use ChangeNotifier, will meet the need especially for those new to Flutter, and will not cause problems for large applications. |
315 | 118 | ||
316 | -2- Does not use changeNotifier, it is the state manager that uses less memory (close to 0mb for until). | 119 | +### Advantages of using it |
317 | 120 | ||
318 | -3- Forget StatefulWidget! With Get you will never need it. With the other state managers, you will probably have to use a StatefulWidget to get the instance of your Provider, BLoC, MobX Controller, etc. But have you ever stopped to think that your appBar, your scaffold, and most of the widgets that are in your class are stateless? So why save the state of an entire class, if you can only save the state of the Widget that is stateful? Get solves that, too. Create a Stateless class, make everything stateless. If you need to update a single component, wrap it with GetBuilder, and its state will be maintained. | 121 | +1. Update only the required widgets. |
319 | 122 | ||
320 | -4- Organize your project for real! Controllers must not be in your UI, place your TextEditController, or any controller you use within your Controller class. | 123 | +2. Does not use changeNotifier, it is the state manager that uses less memory (close to 0mb). |
321 | 124 | ||
322 | -5- Do you need to trigger an event to update a widget as soon as it is rendered? GetBuilder has the property "initState", just like StatefulWidget, and you can call events from your controller, directly from it, no more events being placed in your initState. | 125 | +3. Forget StatefulWidget! With Get you will never need it. With the other state managers, you will probably have to use a StatefulWidget to get the instance of your Provider, BLoC, MobX Controller, etc. But have you ever stopped to think that your appBar, your scaffold, and most of the widgets that are in your class are stateless? So why save the state of an entire class, if you can only save the state of the Widget that is stateful? Get solves that, too. Create a Stateless class, make everything stateless. If you need to update a single component, wrap it with GetBuilder, and its state will be maintained. |
323 | 126 | ||
324 | -6- Do you need to trigger an action like closing streams, timers and etc? GetBuilder also has the dispose property, where you can call events as soon as that widget is destroyed. | 127 | +4. Organize your project for real! Controllers must not be in your UI, place your TextEditController, or any controller you use within your Controller class. |
325 | 128 | ||
326 | -7- Use streams only if necessary. You can use your StreamControllers inside your controller normally, and use StreamBuilder also normally, but remember, a stream reasonably consumes memory, reactive programming is beautiful, but you shouldn't abuse it. 30 streams open simultaneously can be worse than changeNotifier (and changeNotifier is very bad). | 129 | +5. Do you need to trigger an event to update a widget as soon as it is rendered? GetBuilder has the property "initState", just like StatefulWidget, and you can call events from your controller, directly from it, no more events being placed in your initState. |
327 | 130 | ||
328 | -8- Update widgets without spending ram for that. Get stores only the GetBuilder creator ID, and updates that GetBuilder when necessary. The memory consumption of the get ID storage in memory is very low even for thousands of GetBuilders. When you create a new GetBuilder, you are actually sharing the state of GetBuilder that has a creator ID. A new state is not created for each GetBuilder, which saves A LOT OF ram for large applications. Basically your application will be entirely Stateless, and the few Widgets that will be Stateful (within GetBuilder) will have a single state, and therefore updating one will update them all. The state is just one. | 131 | +6. Do you need to trigger an action like closing streams, timers and etc? GetBuilder also has the dispose property, where you can call events as soon as that widget is destroyed. |
329 | 132 | ||
330 | -9- Get is omniscient and in most cases it knows exactly the time to take a controller out of memory. You should not worry about when to dispose of a controller, Get knows the best time to do this. Example: | 133 | +7. Use streams only if necessary. You can use your StreamControllers inside your controller normally, and use StreamBuilder also normally, but remember, a stream reasonably consumes memory, reactive programming is beautiful, but you shouldn't abuse it. 30 streams open simultaneously can be worse than changeNotifier (and changeNotifier is very bad). |
331 | 134 | ||
332 | -- Class a => Class B (has controller X) => Class C (has controller X) | 135 | +8. Update widgets without spending ram for that. Get stores only the GetBuilder creator ID, and updates that GetBuilder when necessary. The memory consumption of the get ID storage in memory is very low even for thousands of GetBuilders. When you create a new GetBuilder, you are actually sharing the state of GetBuilder that has a creator ID. A new state is not created for each GetBuilder, which saves A LOT OF ram for large applications. Basically your application will be entirely Stateless, and the few Widgets that will be Stateful (within GetBuilder) will have a single state, and therefore updating one will update them all. The state is just one. |
333 | 136 | ||
334 | -In class A the controller is not yet in memory, because you have not used it yet (Get is lazyLoad). In class B you used the controller, and it entered memory. In class C you used the same controller as in class B, Get will share the state of controller B with controller C, and the same controller is still in memory. If you close screen C and screen B, Get will automatically take controller X out of memory and free up resources, because Class a is not using the controller. If you navigate to B again, controller X will enter memory again, if instead of going to class C, you return to class A again, Get will take the controller out of memory in the same way. If class C didn't use the controller, and you took class B out of memory, no class would be using controller X and likewise it would be disposed of. The only exception that can mess with Get, is if you remove B from the route unexpectedly, and try to use the controller in C. In this case, the creator ID of the controller that was in B was deleted, and Get was programmed to remove it from memory every controller that has no creator ID. If you intend to do this, add the "autoRemove: false" flag to class B's GetBuilder and use adoptID = true; in class C's GetBuilder. | 137 | +9. Get is omniscient and in most cases it knows exactly the time to take a controller out of memory. You should not worry about when to dispose of a controller, Get knows the best time to do this. |
335 | 138 | ||
336 | ### Simple state manager usage | 139 | ### Simple state manager usage |
337 | 140 | ||
@@ -404,7 +207,14 @@ FloatingActionButton( | @@ -404,7 +207,14 @@ FloatingActionButton( | ||
404 | ``` | 207 | ``` |
405 | When you press FloatingActionButton, all widgets that are listening to the 'counter' variable will be updated automatically. | 208 | When you press FloatingActionButton, all widgets that are listening to the 'counter' variable will be updated automatically. |
406 | 209 | ||
407 | -#### No StatefulWidget: | 210 | +### How it handles controllers |
211 | +Let's say we have this: | ||
212 | + | ||
213 | +`Class a => Class B (has controller X) => Class C (has controller X)` | ||
214 | + | ||
215 | +In class A the controller is not yet in memory, because you have not used it yet (Get is lazyLoad). In class B you used the controller, and it entered memory. In class C you used the same controller as in class B, Get will share the state of controller B with controller C, and the same controller is still in memory. If you close screen C and screen B, Get will automatically take controller X out of memory and free up resources, because Class a is not using the controller. If you navigate to B again, controller X will enter memory again, if instead of going to class C, you return to class A again, Get will take the controller out of memory in the same way. If class C didn't use the controller, and you took class B out of memory, no class would be using controller X and likewise it would be disposed of. The only exception that can mess with Get, is if you remove B from the route unexpectedly, and try to use the controller in C. In this case, the creator ID of the controller that was in B was deleted, and Get was programmed to remove it from memory every controller that has no creator ID. If you intend to do this, add the "autoRemove: false" flag to class B's GetBuilder and use adoptID = true; in class C's GetBuilder. | ||
216 | + | ||
217 | +### You won't need StatefulWidgts anymore | ||
408 | Using StatefulWidgets means storing the state of entire screens unnecessarily, even because if you need to minimally rebuild a widget, you will embed it in a Consumer/Observer/BlocProvider/GetBuilder/GetX/Obx, which will be another StatefulWidget. | 218 | Using StatefulWidgets means storing the state of entire screens unnecessarily, even because if you need to minimally rebuild a widget, you will embed it in a Consumer/Observer/BlocProvider/GetBuilder/GetX/Obx, which will be another StatefulWidget. |
409 | The StatefulWidget class is a class larger than StatelessWidget, which will allocate more RAM, and this may not make a significant difference between one or two classes, but it will most certainly do when you have 100 of them! | 219 | The StatefulWidget class is a class larger than StatelessWidget, which will allocate more RAM, and this may not make a significant difference between one or two classes, but it will most certainly do when you have 100 of them! |
410 | Unless you need to use a mixin, like TickerProviderStateMixin, it will be totally unnecessary to use a StatefulWidget with Get. | 220 | Unless you need to use a mixin, like TickerProviderStateMixin, it will be totally unnecessary to use a StatefulWidget with Get. |
@@ -431,6 +241,8 @@ void onInit() { | @@ -431,6 +241,8 @@ void onInit() { | ||
431 | ``` | 241 | ``` |
432 | 242 | ||
433 | - NOTE: If you want to start a method at the moment the controller is called for the first time, you DON'T NEED to use constructors for this, in fact, using a performance-oriented package like Get, this borders on bad practice, because it deviates from the logic in which the controllers are created or allocated (if you create an instance of this controller, the constructor will be called immediately, you will be populating a controller before it is even used, you are allocating memory without it being in use, this definitely hurts the principles of this library). The onInit() methods; and onClose(); were created for this, they will be called when the Controller is created, or used for the first time, depending on whether you are using Get.lazyPut or not. If you want, for example, to make a call to your API to populate data, you can forget about the old-fashioned method of initState/dispose, just start your call to the api in onInit, and if you need to execute any command like closing streams, use the onClose() for that. | 243 | - NOTE: If you want to start a method at the moment the controller is called for the first time, you DON'T NEED to use constructors for this, in fact, using a performance-oriented package like Get, this borders on bad practice, because it deviates from the logic in which the controllers are created or allocated (if you create an instance of this controller, the constructor will be called immediately, you will be populating a controller before it is even used, you are allocating memory without it being in use, this definitely hurts the principles of this library). The onInit() methods; and onClose(); were created for this, they will be called when the Controller is created, or used for the first time, depending on whether you are using Get.lazyPut or not. If you want, for example, to make a call to your API to populate data, you can forget about the old-fashioned method of initState/dispose, just start your call to the api in onInit, and if you need to execute any command like closing streams, use the onClose() for that. |
244 | + | ||
245 | +### Why it exists | ||
434 | The purpose of this package is precisely to give you a complete solution for navigation of routes, management of dependencies and states, using the least possible dependencies, with a high degree of decoupling. Get engages all high and low level Flutter APIs within itself, to ensure that you work with the least possible coupling. We centralize everything in a single package, to ensure that you don't have any kind of coupling in your project. That way, you can put only widgets in your view, and leave the part of your team that works with the business logic free, to work with the business logic without depending on any element of the View. This provides a much cleaner working environment, so that part of your team works only with widgets, without worrying about sending data to your controller, and part of your team works only with the business logic in its breadth, without depending on no element of the view. | 246 | The purpose of this package is precisely to give you a complete solution for navigation of routes, management of dependencies and states, using the least possible dependencies, with a high degree of decoupling. Get engages all high and low level Flutter APIs within itself, to ensure that you work with the least possible coupling. We centralize everything in a single package, to ensure that you don't have any kind of coupling in your project. That way, you can put only widgets in your view, and leave the part of your team that works with the business logic free, to work with the business logic without depending on any element of the View. This provides a much cleaner working environment, so that part of your team works only with widgets, without worrying about sending data to your controller, and part of your team works only with the business logic in its breadth, without depending on no element of the view. |
435 | 247 | ||
436 | So to simplify this: | 248 | So to simplify this: |
@@ -458,7 +270,7 @@ Controller life cycle: | @@ -458,7 +270,7 @@ Controller life cycle: | ||
458 | - onClose() where it is closed to make any changes in preparation for the delete method | 270 | - onClose() where it is closed to make any changes in preparation for the delete method |
459 | - deleted: you do not have access to this API because it is literally removing the controller from memory. It is literally deleted, without leaving any trace. | 271 | - deleted: you do not have access to this API because it is literally removing the controller from memory. It is literally deleted, without leaving any trace. |
460 | 272 | ||
461 | -##### Forms of use: | 273 | +### Other ways of using it |
462 | 274 | ||
463 | You can use Controller instance directly on GetBuilder value: | 275 | You can use Controller instance directly on GetBuilder value: |
464 | 276 | ||
@@ -513,9 +325,8 @@ GetBuilder<Controller>( | @@ -513,9 +325,8 @@ GetBuilder<Controller>( | ||
513 | ), | 325 | ), |
514 | 326 | ||
515 | ``` | 327 | ``` |
516 | -<!-- 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! --> | ||
517 | - | ||
518 | 328 | ||
329 | +### Unique IDs | ||
519 | If you want to refine a widget's update control with GetBuilder, you can assign them unique IDs: | 330 | If you want to refine a widget's update control with GetBuilder, you can assign them unique IDs: |
520 | ```dart | 331 | ```dart |
521 | GetBuilder<Controller>( | 332 | GetBuilder<Controller>( |
@@ -537,22 +348,25 @@ update(['text'], counter < 10); | @@ -537,22 +348,25 @@ update(['text'], counter < 10); | ||
537 | ``` | 348 | ``` |
538 | 349 | ||
539 | GetX does this automatically and only reconstructs the widget that uses the exact variable that was changed, if you change a variable to the same as the previous one and that does not imply a change of state , GetX will not rebuild the widget to save memory and CPU cycles (3 is being displayed on the screen, and you change the variable to 3 again. In most state managers, this will cause a new rebuild, but with GetX the widget will only is rebuilt again, if in fact his state has changed). | 350 | GetX does this automatically and only reconstructs the widget that uses the exact variable that was changed, if you change a variable to the same as the previous one and that does not imply a change of state , GetX will not rebuild the widget to save memory and CPU cycles (3 is being displayed on the screen, and you change the variable to 3 again. In most state managers, this will cause a new rebuild, but with GetX the widget will only is rebuilt again, if in fact his state has changed). |
351 | + | ||
352 | +## Reactive State Manager | ||
353 | + | ||
354 | +### Advantages | ||
540 | GetBuilder is aimed precisely at multiple state control. Imagine that you added 30 products to a cart, you click delete one, at the same time that the list is updated, the price is updated and the badge in the shopping cart is updated to a smaller number. This type of approach makes GetBuilder killer, because it groups states and changes them all at once without any "computational logic" for that. GetBuilder was created with this type of situation in mind, since for ephemeral change of state, you can use setState and you would not need a state manager for this. However, there are situations where you want only the widget where a certain variable has been changed to be rebuilt, and this is what GetX does with a mastery never seen before. | 355 | GetBuilder is aimed precisely at multiple state control. Imagine that you added 30 products to a cart, you click delete one, at the same time that the list is updated, the price is updated and the badge in the shopping cart is updated to a smaller number. This type of approach makes GetBuilder killer, because it groups states and changes them all at once without any "computational logic" for that. GetBuilder was created with this type of situation in mind, since for ephemeral change of state, you can use setState and you would not need a state manager for this. However, there are situations where you want only the widget where a certain variable has been changed to be rebuilt, and this is what GetX does with a mastery never seen before. |
541 | 356 | ||
542 | That way, if you want an individual controller, you can assign IDs for that, or use GetX. This is up to you, remembering that the more "individual" widgets you have, the more the performance of GetX will stand out, while the performance of GetBuilder should be superior, when there is multiple change of state. | 357 | That way, if you want an individual controller, you can assign IDs for that, or use GetX. This is up to you, remembering that the more "individual" widgets you have, the more the performance of GetX will stand out, while the performance of GetBuilder should be superior, when there is multiple change of state. |
543 | 358 | ||
544 | -You can use both in any situation, but if you want to tune their application to the maximum possible performance, I would say that: if your variables are changed at different times, use GetX, because there is no competition for it when the subject is to rebuild only what is necessary, if you do not need unique IDs, because all your variables will be changed when you perform an action, use GetBuilder, because it is a simple state updater in blocks, made in a few lines of code, to make just what he promises to do: update state in blocks. There is no way to compare RAM, CPU, or anything else from a giant state manager to a simple StatefulWidget (like GetBuilder) that is updated when you call update(this). It was done in a simple way, to have the least computational logic involved, just to fulfill a single purpose and spend the minimum resources possible for that purpose. | 359 | +You can use both in any situation, but if you want to tune their application to the maximum possible performance, I would say that: if your variables are changed at different times, use GetX, because there is no competition for it when the subject is to rebuild only what is necessary, if you do not need unique IDs, because all your variables will be changed when you perform an action, use GetBuilder, because it is a simple state updater in blocks, made in a few lines of code, to make just what he promises to do: update state in blocks. There is no way to compare RAM, CPU, or anything else from a giant state manager to a simple StatefulWidget (like GetBuilder) that is updated when you call update(). It was done in a simple way, to have the least computational logic involved, just to fulfill a single purpose and spend the minimum resources possible for that purpose. |
545 | If you want a powerful state manager, you can go without fear to GetX. It does not work with variables, but flows, everything in it is streams under the hood. You can use rxDart in conjunction with it, because everything is stream, you can hear the event of each "variable", because everything in it is stream, it is literally BLoC, easier than MobX, and without code generator or decorations . | 360 | If you want a powerful state manager, you can go without fear to GetX. It does not work with variables, but flows, everything in it is streams under the hood. You can use rxDart in conjunction with it, because everything is stream, you can hear the event of each "variable", because everything in it is stream, it is literally BLoC, easier than MobX, and without code generator or decorations . |
546 | 361 | ||
547 | - | ||
548 | -## Reactive State Manager | ||
549 | - | ||
550 | If you want power, Get gives you the most advanced state manager you could ever have. | 362 | If you want power, Get gives you the most advanced state manager you could ever have. |
551 | GetX was built 100% based on Streams, and give you all the firepower that BLoC gave you, with an easier facility than using MobX. | 363 | GetX was built 100% based on Streams, and give you all the firepower that BLoC gave you, with an easier facility than using MobX. |
552 | Without decorations, you can turn anything into Observable with just a ".obs". | 364 | Without decorations, you can turn anything into Observable with just a ".obs". |
553 | 365 | ||
554 | Maximum performance: In addition to having a smart algorithm for minimal reconstruction, Get uses comparators to make sure the state has changed. If you experience any errors in your application, and send a duplicate change of state, Get will ensure that your application does not collapse. | 366 | Maximum performance: In addition to having a smart algorithm for minimal reconstruction, Get uses comparators to make sure the state has changed. If you experience any errors in your application, and send a duplicate change of state, Get will ensure that your application does not collapse. |
555 | -The state only changes if the values change. That's the main difference between Get, and using Computed from MobX. When joining two observables, when one is changed, the hearing of that observable will change. With Get, if you join two variables (which is unnecessary computed for that), GetX (similar to Observer) will only change if it implies a real change of state. Example: | 367 | +The state only changes if the values change. That's the main difference between Get, and using Computed from MobX. When joining two observables, when one is changed, the hearing of that observable will change. With Get, if you join two variables (which is unnecessary computed for that), GetX (similar to Observer) will only change if it implies a real change of state. |
368 | + | ||
369 | +### Usage: | ||
556 | 370 | ||
557 | ```dart | 371 | ```dart |
558 | final count1 = 0.obs; | 372 | final count1 = 0.obs; |
@@ -609,6 +423,7 @@ ctl.count.value++ | @@ -609,6 +423,7 @@ ctl.count.value++ | ||
609 | 423 | ||
610 | You could update the counter variable in your UI, regardless of where it is stored. | 424 | You could update the counter variable in your UI, regardless of where it is stored. |
611 | 425 | ||
426 | +### Where .obs can be used | ||
612 | You can transform anything on obs: | 427 | You can transform anything on obs: |
613 | 428 | ||
614 | ```dart | 429 | ```dart |
@@ -648,6 +463,7 @@ Camila | @@ -648,6 +463,7 @@ Camila | ||
648 | 463 | ||
649 | ``` | 464 | ``` |
650 | 465 | ||
466 | +### Note about Lists | ||
651 | Working with Lists using Get is the best and most enjoyable thing in the world. They are completely observable as are the objects within it. That way, if you add a value to a list, it will automatically rebuild the widgets that use it. | 467 | Working with Lists using Get is the best and most enjoyable thing in the world. They are completely observable as are the objects within it. That way, if you add a value to a list, it will automatically rebuild the widgets that use it. |
652 | You also don't need to use ".value" with lists, the amazing dart api allowed us to remove that, unfortunate primitive types like String and int cannot be extended, making the use of .value mandatory, but that won't be a problem if you work with gets and setters for these. | 468 | You also don't need to use ".value" with lists, the amazing dart api allowed us to remove that, unfortunate primitive types like String and int cannot be extended, making the use of .value mandatory, but that won't be a problem if you work with gets and setters for these. |
653 | 469 | ||
@@ -665,29 +481,262 @@ You don't have to work with sets if you don't want to. you can use the "assign ' | @@ -665,29 +481,262 @@ You don't have to work with sets if you don't want to. you can use the "assign ' | ||
665 | The "assign" api will clear your list, and add a single object that you want to start there. | 481 | The "assign" api will clear your list, and add a single object that you want to start there. |
666 | The "assignAll" api will clear the existing list and add any iterable objects that you inject into it. | 482 | The "assignAll" api will clear the existing list and add any iterable objects that you inject into it. |
667 | 483 | ||
484 | +### Why i have to use .value? | ||
668 | We could remove the obligation to use 'value' to String and int with a simple decoration and code generator, but the purpose of this lib is precisely not to need any external dependency. It is to offer an environment ready for programming, involving the essentials (management of routes, dependencies and states), in a simple, light and performance way without needing any external package. You can literally add 3 letters to your pubspec (get) and start programming. All solutions included by default, from route management to state management, aim at ease, productivity and performance. The total weight of this library is less than that of a single state manager, even though it is a complete solution, and that is what you must understand. If you are bothered by value, and like a code generator, MobX is a great alternative, and you can use it in conjunction with Get. For those who want to add a single dependency in pubspec and start programming without worrying about the version of a package being incompatible with another, or if the error of a state update is coming from the state manager or dependency, or still, do not want to worrying about the availability of controllers, whether literally "just programming", get is just perfect. | 485 | We could remove the obligation to use 'value' to String and int with a simple decoration and code generator, but the purpose of this lib is precisely not to need any external dependency. It is to offer an environment ready for programming, involving the essentials (management of routes, dependencies and states), in a simple, light and performance way without needing any external package. You can literally add 3 letters to your pubspec (get) and start programming. All solutions included by default, from route management to state management, aim at ease, productivity and performance. The total weight of this library is less than that of a single state manager, even though it is a complete solution, and that is what you must understand. If you are bothered by value, and like a code generator, MobX is a great alternative, and you can use it in conjunction with Get. For those who want to add a single dependency in pubspec and start programming without worrying about the version of a package being incompatible with another, or if the error of a state update is coming from the state manager or dependency, or still, do not want to worrying about the availability of controllers, whether literally "just programming", get is just perfect. |
486 | + | ||
669 | If you have no problem with the MobX code generator, or have no problem with the BLoC boilerplate, you can simply use Get for routes, and forget that it has state manager. Get SEM and RSM were born out of necessity, my company had a project with more than 90 controllers, and the code generator simply took more than 30 minutes to complete its tasks after a Flutter Clean on a reasonably good machine, if your project it has 5, 10, 15 controllers, any state manager will supply you well. If you have an absurdly large project, and code generator is a problem for you, you have been awarded this solution. | 487 | If you have no problem with the MobX code generator, or have no problem with the BLoC boilerplate, you can simply use Get for routes, and forget that it has state manager. Get SEM and RSM were born out of necessity, my company had a project with more than 90 controllers, and the code generator simply took more than 30 minutes to complete its tasks after a Flutter Clean on a reasonably good machine, if your project it has 5, 10, 15 controllers, any state manager will supply you well. If you have an absurdly large project, and code generator is a problem for you, you have been awarded this solution. |
670 | 488 | ||
671 | Obviously, if someone wants to contribute to the project and create a code generator, or something similar, I will link in this readme as an alternative, my need is not the need for all devs, but for now I say, there are good solutions that already do that, like MobX. | 489 | Obviously, if someone wants to contribute to the project and create a code generator, or something similar, I will link in this readme as an alternative, my need is not the need for all devs, but for now I say, there are good solutions that already do that, like MobX. |
672 | 490 | ||
491 | +### Obx() | ||
492 | + | ||
673 | Typing in Get using Bindings is unnecessary. you can use the Obx widget instead of GetX which only receives the anonymous function that creates a widget. | 493 | Typing in Get using Bindings is unnecessary. you can use the Obx widget instead of GetX which only receives the anonymous function that creates a widget. |
674 | Obviously, if you don't use a type, you will need to have an instance of your controller to use the variables, or use `Get.find<Controller>()` .value or Controller.to.value to retrieve the value. | 494 | Obviously, if you don't use a type, you will need to have an instance of your controller to use the variables, or use `Get.find<Controller>()` .value or Controller.to.value to retrieve the value. |
675 | 495 | ||
676 | -### GetX vs GetBuilder vs Obx vs MixinBuilder | 496 | +## Mixing the two state managers |
497 | +Some people opened a feature request, as they wanted to use only one type of reactive variable, and the other mechanics, and needed to insert an Obx into a GetBuilder for this. Thinking about it MixinBuilder was created. It allows both reactive changes by changing ".obs" variables, and mechanical updates via update(). However, of the 4 widgets he is the one that consumes the most resources, since in addition to having a Subscription to receive change events from his children, he subscribes to the update method of his controller. | ||
498 | + | ||
499 | +- Note: To use GetBuilder and MixinBuilder you must use GetController. To use GetX and Obx you must use RxController. | ||
500 | +Probably using a GetController using GetX and Obx will work, but it will not be possible to use an RxController on a GetBuilder. | ||
501 | +Extending these controllers is important, as they have life cycles, and can "start" and "end" events in their onInit() and onClose() methods. | ||
502 | + | ||
503 | +## GetX vs GetBuilder vs Obx vs MixinBuilder | ||
677 | In a decade working with programming I was able to learn some valuable lessons. | 504 | In a decade working with programming I was able to learn some valuable lessons. |
505 | + | ||
678 | My first contact with reactive programming was so "wow, this is incredible" and in fact reactive programming is incredible. | 506 | My first contact with reactive programming was so "wow, this is incredible" and in fact reactive programming is incredible. |
679 | However, it is not suitable for all situations. Often all you need is to change the state of 2 or 3 widgets at the same time, or an ephemeral change of state, in which case reactive programming is not bad, but it is not appropriate. | 507 | However, it is not suitable for all situations. Often all you need is to change the state of 2 or 3 widgets at the same time, or an ephemeral change of state, in which case reactive programming is not bad, but it is not appropriate. |
508 | + | ||
680 | Reactive programming has a higher consumption of RAM consumption that can be compensated for by the individual workflow, which will ensure that only one widget is rebuilt and when necessary, but creating a list with 80 objects, each with several streams is not a good one idea. Open the dart inspect and check how much a StreamBuilder consumes, and you'll understand what I'm trying to tell you. | 509 | Reactive programming has a higher consumption of RAM consumption that can be compensated for by the individual workflow, which will ensure that only one widget is rebuilt and when necessary, but creating a list with 80 objects, each with several streams is not a good one idea. Open the dart inspect and check how much a StreamBuilder consumes, and you'll understand what I'm trying to tell you. |
510 | + | ||
681 | With that in mind, I created the simple state manager. It is simple, and that is exactly what you should demand from it: updating state in blocks in a simple way, and in the most economical way. | 511 | With that in mind, I created the simple state manager. It is simple, and that is exactly what you should demand from it: updating state in blocks in a simple way, and in the most economical way. |
512 | + | ||
682 | GetBuilder is very economical in RAM, and there is hardly a more economical approach than him (at least I can't imagine one, if it exists, please let us know). | 513 | GetBuilder is very economical in RAM, and there is hardly a more economical approach than him (at least I can't imagine one, if it exists, please let us know). |
514 | + | ||
683 | However, GetBuilder is still a mechanical state manager, you need to call update() just like you would need to call Provider's notifyListeners(). | 515 | However, GetBuilder is still a mechanical state manager, you need to call update() just like you would need to call Provider's notifyListeners(). |
516 | + | ||
684 | There are other situations where reactive programming is really interesting, and not working with it is the same as reinventing the wheel. With that in mind, GetX was created to provide everything that is most modern and advanced in a state manager. It updates only what is necessary and when necessary, if you have an error and send 300 state changes simultaneously, GetX will filter and update the screen only if the state actually changes. | 517 | There are other situations where reactive programming is really interesting, and not working with it is the same as reinventing the wheel. With that in mind, GetX was created to provide everything that is most modern and advanced in a state manager. It updates only what is necessary and when necessary, if you have an error and send 300 state changes simultaneously, GetX will filter and update the screen only if the state actually changes. |
518 | + | ||
685 | GetX is still more economical than any other reactive state manager, but it consumes a little more RAM than GetBuilder. Thinking about it and aiming to maximize the consumption of resources that Obx was created. Unlike GetX and GetBuilder, you will not be able to initialize a controller inside an Obx, it is just a Widget with a StreamSubscription that receives change events from your children, that's all. It is more economical than GetX, but loses to GetBuilder, which was to be expected, since it is reactive, and GetBuilder has the most simplistic approach that exists, of storing a widget's hashcode and its StateSetter. With Obx you don't need to write your controller type, and you can hear the change from multiple different controllers, but it needs to be initialized before, either using the example approach at the beginning of this readme, or using the Bindings class. | 519 | GetX is still more economical than any other reactive state manager, but it consumes a little more RAM than GetBuilder. Thinking about it and aiming to maximize the consumption of resources that Obx was created. Unlike GetX and GetBuilder, you will not be able to initialize a controller inside an Obx, it is just a Widget with a StreamSubscription that receives change events from your children, that's all. It is more economical than GetX, but loses to GetBuilder, which was to be expected, since it is reactive, and GetBuilder has the most simplistic approach that exists, of storing a widget's hashcode and its StateSetter. With Obx you don't need to write your controller type, and you can hear the change from multiple different controllers, but it needs to be initialized before, either using the example approach at the beginning of this readme, or using the Bindings class. |
686 | -Finally, some people opened a resource request, as they wanted to use only one type of reactive variable, and the other mechanics, and needed to insert an Obx into a GetBuilder for this. Thinking about it MixinBuilder was created. It allows both reactive changes by changing ".obs" variables, and mechanical updates via update(). However, of the 4 widgets he is the one that consumes the most resources, since in addition to having a Subscription to receive change events from his children, he subscribes to the update method of his controller. | ||
687 | 520 | ||
688 | -- Note: To use GetBuilder and MixinBuilder you must use GetController. To use GetX and Obx you must use RxController. | ||
689 | -Probably using a GetController using GetX and Obx will work, but it will not be possible to use an RxController on a GetBuilder. | ||
690 | -Extending these controllers is important, as they have life cycles, and can "start" and "end" events in their onInit() and onClose() methods. | 521 | + |
522 | + | ||
523 | +# Dependency Management | ||
524 | + | ||
525 | +Any contribution is welcome! | ||
526 | + | ||
527 | +## How to use? | ||
528 | + | ||
529 | +<!-- - Flutter Master/Dev/Beta: version 2.0.x-dev | ||
530 | +- Flutter Stable branch: version 2.0.x | ||
531 | +(look for latest version on pub.dev) --> | ||
532 | + | ||
533 | +Add this to your pubspec.yaml file: | ||
534 | + | ||
535 | +``` | ||
536 | +dependencies: | ||
537 | + get: | ||
538 | +``` | ||
539 | +If you are going to use routes/snackbars/dialogs/bottomsheets without context, or use the high-level Get APIs, you need to simply add "Get" before your MaterialApp, turning it into GetMaterialApp and enjoy! | ||
540 | + | ||
541 | +```dart | ||
542 | +GetMaterialApp( // Before: MaterialApp( | ||
543 | + home: MyHome(), | ||
544 | +) | ||
545 | +``` | ||
546 | +## Navigating without named routes | ||
547 | +To navigate to a new screen: | ||
548 | + | ||
549 | +```dart | ||
550 | +Get.to(NextScreen()); | ||
551 | +``` | ||
552 | + | ||
553 | +To close snackbars, dialogs, bottomsheets, or anything you would normally close with Navigator.pop(context); | ||
554 | + | ||
555 | +```dart | ||
556 | +Get.back(); | ||
557 | +``` | ||
558 | + | ||
559 | +To go to the next screen and no option to go back to the previous screen (for use in SplashScreens, login screens and etc.) | ||
560 | + | ||
561 | +```dart | ||
562 | +Get.off(NextScreen()); | ||
563 | +``` | ||
564 | + | ||
565 | +To go to the next screen and cancel all previous routes (useful in shopping carts, polls, and tests) | ||
566 | + | ||
567 | +```dart | ||
568 | +Get.offAll(NextScreen()); | ||
569 | +``` | ||
570 | + | ||
571 | +To navigate to the next route, and receive or update data as soon as you return from it: | ||
572 | +```dart | ||
573 | +var data = await Get.to(Payment()); | ||
574 | +``` | ||
575 | +on other screen, send a data for previous route: | ||
576 | + | ||
577 | +```dart | ||
578 | +Get.back(result: 'success'); | ||
579 | +``` | ||
580 | +And use it: | ||
581 | + | ||
582 | +ex: | ||
583 | +```dart | ||
584 | +if(data == 'success') madeAnything(); | ||
585 | +``` | ||
586 | + | ||
587 | +Don't you want to learn our syntax? | ||
588 | +Just change the Navigator (uppercase) to navigator (lowercase), and you will have all the functions of the standard navigation, without having to use context | ||
589 | +Example: | ||
590 | + | ||
591 | +```dart | ||
592 | + | ||
593 | +// Default Flutter navigator | ||
594 | +Navigator.of(context).push( | ||
595 | + context, | ||
596 | + MaterialPageRoute( | ||
597 | + builder: (BuildContext context) { | ||
598 | + return HomePage(); | ||
599 | + }, | ||
600 | + ), | ||
601 | +); | ||
602 | + | ||
603 | +// Get using Flutter syntax without needing context | ||
604 | +navigator.push( | ||
605 | + MaterialPageRoute( | ||
606 | + builder: (_) { | ||
607 | + return HomePage(); | ||
608 | + }, | ||
609 | + ), | ||
610 | +); | ||
611 | + | ||
612 | +// Get syntax (It is much better, but you have the right to disagree) | ||
613 | +Get.to(HomePage()); | ||
614 | + | ||
615 | + | ||
616 | +``` | ||
617 | + | ||
618 | +### SnackBars | ||
619 | + | ||
620 | +To have a simple SnackBar with Flutter, you must get the context of Scaffold, or you must use a GlobalKey attached to your Scaffold, | ||
621 | +```dart | ||
622 | +final snackBar = SnackBar( | ||
623 | + content: Text('Hi!'), | ||
624 | + action: SnackBarAction( | ||
625 | + label: 'I am a old and ugly snackbar :(', | ||
626 | + onPressed: (){} | ||
627 | + ), | ||
628 | +); | ||
629 | +// Find the Scaffold in the widget tree and use | ||
630 | +// it to show a SnackBar. | ||
631 | +Scaffold.of(context).showSnackBar(snackBar); | ||
632 | +``` | ||
633 | + | ||
634 | +With Get: | ||
635 | + | ||
636 | +```dart | ||
637 | +Get.snackbar('Hi', 'i am a modern snackbar'); | ||
638 | +``` | ||
639 | + | ||
640 | +With Get, all you have to do is call your Get.snackbar from anywhere in your code or customize it however you want! | ||
641 | + | ||
642 | +```dart | ||
643 | +Get.snackbar( | ||
644 | + "Hey i'm a Get SnackBar!", // title | ||
645 | + "It's unbelievable! I'm using SnackBar without context, without boilerplate, without Scaffold, it is something truly amazing!", // message | ||
646 | + icon: Icon(Icons.alarm), | ||
647 | + shouldIconPulse: true, | ||
648 | + onTap:(){}, | ||
649 | + barBlur: 20, | ||
650 | + isDismissible: true, | ||
651 | + duration: Duration(seconds: 3), | ||
652 | +); | ||
653 | + | ||
654 | + | ||
655 | + ////////// ALL FEATURES ////////// | ||
656 | + // Color colorText, | ||
657 | + // Duration duration, | ||
658 | + // SnackPosition snackPosition, | ||
659 | + // Widget titleText, | ||
660 | + // Widget messageText, | ||
661 | + // bool instantInit, | ||
662 | + // Widget icon, | ||
663 | + // bool shouldIconPulse, | ||
664 | + // double maxWidth, | ||
665 | + // EdgeInsets margin, | ||
666 | + // EdgeInsets padding, | ||
667 | + // double borderRadius, | ||
668 | + // Color borderColor, | ||
669 | + // double borderWidth, | ||
670 | + // Color backgroundColor, | ||
671 | + // Color leftBarIndicatorColor, | ||
672 | + // List<BoxShadow> boxShadows, | ||
673 | + // Gradient backgroundGradient, | ||
674 | + // FlatButton mainButton, | ||
675 | + // OnTap onTap, | ||
676 | + // bool isDismissible, | ||
677 | + // bool showProgressIndicator, | ||
678 | + // AnimationController progressIndicatorController, | ||
679 | + // Color progressIndicatorBackgroundColor, | ||
680 | + // Animation<Color> progressIndicatorValueColor, | ||
681 | + // SnackStyle snackStyle, | ||
682 | + // Curve forwardAnimationCurve, | ||
683 | + // Curve reverseAnimationCurve, | ||
684 | + // Duration animationDuration, | ||
685 | + // double barBlur, | ||
686 | + // double overlayBlur, | ||
687 | + // Color overlayColor, | ||
688 | + // Form userInputForm | ||
689 | + /////////////////////////////////// | ||
690 | +``` | ||
691 | +If you prefer the traditional snackbar, or want to customize it from scratch, including adding just one line (Get.snackbar makes use of a mandatory title and message), you can use | ||
692 | +`Get.rawSnackbar();` which provides the RAW API on which Get.snackbar was built. | ||
693 | + | ||
694 | +### Dialogs | ||
695 | + | ||
696 | +To open dialog: | ||
697 | + | ||
698 | +```dart | ||
699 | +Get.dialog(YourDialogWidget()); | ||
700 | +``` | ||
701 | + | ||
702 | +To open default dialog: | ||
703 | + | ||
704 | +```dart | ||
705 | +Get.defaultDialog( | ||
706 | + onConfirm: () => print("Ok"), | ||
707 | + middleText: "Dialog made in 3 lines of code" | ||
708 | +); | ||
709 | +``` | ||
710 | +You can also use Get.generalDialog instead of showGeneralDialog. | ||
711 | + | ||
712 | +For all other Flutter dialog widgets, including cupertinos, you can use Get.overlayContext instead of context, and open it anywhere in your code. | ||
713 | +For widgets that don't use Overlay, you can use Get.context. | ||
714 | +These two contexts will work in 99% of cases to replace the context of your UI, except for cases where inheritedWidget is used without a navigation context. | ||
715 | + | ||
716 | +### BottomSheets | ||
717 | +Get.bottomSheet is like showModalBottomSheet, but don't need of context. | ||
718 | + | ||
719 | +```dart | ||
720 | +Get.bottomSheet( | ||
721 | + Container( | ||
722 | + child: Wrap( | ||
723 | + children: <Widget>[ | ||
724 | + ListTile( | ||
725 | + leading: Icon(Icons.music_note), | ||
726 | + title: Text('Music'), | ||
727 | + onTap: () => {} | ||
728 | + ), | ||
729 | + ListTile( | ||
730 | + leading: Icon(Icons.videocam), | ||
731 | + title: Text('Video'), | ||
732 | + onTap: () => {}, | ||
733 | + ), | ||
734 | + ], | ||
735 | + ), | ||
736 | + ); | ||
737 | +); | ||
738 | +``` | ||
739 | + | ||
691 | 740 | ||
692 | ## Simple Instance Manager | 741 | ## Simple Instance Manager |
693 | - Note: If you are using Get's State Manager, you don't have to worry about that, just read for information, but pay more attention to the bindings api, which will do all of this automatically for you. | 742 | - Note: If you are using Get's State Manager, you don't have to worry about that, just read for information, but pay more attention to the bindings api, which will do all of this automatically for you. |
@@ -744,6 +793,7 @@ To remove a instance of Get: | @@ -744,6 +793,7 @@ To remove a instance of Get: | ||
744 | Get.delete<Controller>(); | 793 | Get.delete<Controller>(); |
745 | ``` | 794 | ``` |
746 | 795 | ||
796 | + | ||
747 | ## Bindings | 797 | ## Bindings |
748 | One of the great differentials of this package, perhaps, is the possibility of full integration of the routes, state manager and dependency manager. | 798 | One of the great differentials of this package, perhaps, is the possibility of full integration of the routes, state manager and dependency manager. |
749 | When a route is removed from the Stack, all controllers, variables, and instances of objects related to it are removed from memory. If you are using streams or timers, they will be closed automatically, and you don't have to worry about any of that. | 799 | When a route is removed from the Stack, all controllers, variables, and instances of objects related to it are removed from memory. If you are using streams or timers, they will be closed automatically, and you don't have to worry about any of that. |
-
Please register or login to post a comment