Merge branch 'master' of https://github.com/jonataslaw/getx
Showing
11 changed files
with
4762 additions
and
6 deletions
Too many changes to show.
To preserve performance only 11 of 11+ files are displayed.
.vscode/settings.json
0 → 100644
1 | +## [4.3.8] | ||
2 | +- Fix nav2 toNamed remove the route | ||
3 | + | ||
4 | +## [4.3.7] | ||
5 | +- Fix wrong currentRoute when a route is removed | ||
6 | +- Remove take that limits the router outlet depth (@steven-spiel) | ||
7 | + | ||
8 | +## [4.3.6] | ||
9 | +- Fix error with autodispose of additional dependencies beyond GetxController | ||
10 | +- Added ability to add your own delegate to RouterOutlet (@steven-spiel) | ||
11 | +- Added listenAndPump to Rx to give Rx the same behavior as BehaviorSubject (@steven-spiel) | ||
12 | + | ||
13 | +## [4.3.5] | ||
14 | +- Fix GetConnect timeout (@jasonlaw) | ||
15 | +- Improve Vietnamese docs (@hp1909) | ||
16 | +- Refactor placeholder name route to unnamed routes (@roipeker). | ||
17 | +- Fix: Navigate to a page identical to Get.offNamed. | ||
18 | +- Fix: Wrong nameRoute after a route is removed | ||
19 | +- Added assert to prevent the user from starting a route name without slash. | ||
20 | + | ||
1 | ## [4.3.4] | 21 | ## [4.3.4] |
2 | - Improve docs | 22 | - Improve docs |
3 | 23 |
README-ar.md
0 → 100644
1 | + | ||
2 | + | ||
3 | +[](https://pub.dev/packages/get) | ||
4 | +[](https://pub.dev/packages/get/score) | ||
5 | + | ||
6 | +[](https://pub.dev/packages/effective_dart) | ||
7 | +[](https://discord.com/invite/9Hpt99N) | ||
8 | +[](https://communityinviter.com/apps/getxworkspace/getx) | ||
9 | +[](https://t.me/joinchat/PhdbJRmsZNpAqSLJL6bH7g) | ||
10 | +<a href="https://github.com/Solido/awesome-flutter"> | ||
11 | +<img alt="Awesome Flutter" src="https://img.shields.io/badge/Awesome-Flutter-blue.svg?longCache=true&style=flat-square" /> | ||
12 | +</a> | ||
13 | +<a href="https://www.buymeacoffee.com/jonataslaw" target="_blank"><img src="https://i.imgur.com/aV6DDA7.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important; box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" > </a> | ||
14 | + | ||
15 | + | ||
16 | + | ||
17 | +<div align="center"> | ||
18 | + | ||
19 | +**Languages:** | ||
20 | + | ||
21 | +[](README.md) | ||
22 | +[](README-vi.md) | ||
23 | +[](README.id-ID.md) | ||
24 | +[](README.ur-PK.md) | ||
25 | +[](README.zh-cn.md) | ||
26 | +[](README.pt-br.md) | ||
27 | +[](README-es.md) | ||
28 | +[](README.ru.md) | ||
29 | +[](README.pl.md) | ||
30 | +[](README.ko-kr.md) | ||
31 | +[](README-fr.md) | ||
32 | +[](README-ar.md) | ||
33 | + | ||
34 | +</div> | ||
35 | +<div dir="rtl"> | ||
36 | + | ||
37 | +- [عن المكتبة](#عن-المكتبة) | ||
38 | +- [التركيب](#التركيب) | ||
39 | +- [بناء تطبيق العداد 🔢](#بناء-تطبيق-العداد-) | ||
40 | +- [The Three pillars](#the-three-pillars) | ||
41 | + - [State management](#state-management) | ||
42 | + - [Reactive State Manager](#reactive-state-manager) | ||
43 | + - [More details about state management](#more-details-about-state-management) | ||
44 | + - [Route management](#route-management) | ||
45 | + - [More details about route management](#more-details-about-route-management) | ||
46 | + - [Dependency management](#dependency-management) | ||
47 | + - [More details about dependency management](#more-details-about-dependency-management) | ||
48 | +- [Utils](#utils) | ||
49 | + - [Internationalization](#internationalization) | ||
50 | + - [Translations](#translations) | ||
51 | + - [Using translations](#using-translations) | ||
52 | + - [Using translation with singular and plural](#using-translation-with-singular-and-plural) | ||
53 | + - [Using translation with parameters](#using-translation-with-parameters) | ||
54 | + - [Locales](#locales) | ||
55 | + - [Change locale](#change-locale) | ||
56 | + - [System locale](#system-locale) | ||
57 | + - [Change Theme](#change-theme) | ||
58 | + - [GetConnect](#getconnect) | ||
59 | + - [Default configuration](#default-configuration) | ||
60 | + - [Custom configuration](#custom-configuration) | ||
61 | + - [GetPage Middleware](#getpage-middleware) | ||
62 | + - [Priority](#priority) | ||
63 | + - [Redirect](#redirect) | ||
64 | + - [onPageCalled](#onpagecalled) | ||
65 | + - [OnBindingsStart](#onbindingsstart) | ||
66 | + - [OnPageBuildStart](#onpagebuildstart) | ||
67 | + - [OnPageBuilt](#onpagebuilt) | ||
68 | + - [OnPageDispose](#onpagedispose) | ||
69 | + - [Other Advanced APIs](#other-advanced-apis) | ||
70 | + - [Optional Global Settings and Manual configurations](#optional-global-settings-and-manual-configurations) | ||
71 | + - [Local State Widgets](#local-state-widgets) | ||
72 | + - [ValueBuilder](#valuebuilder) | ||
73 | + - [ObxValue](#obxvalue) | ||
74 | + - [Useful tips](#useful-tips) | ||
75 | + - [StateMixin](#statemixin) | ||
76 | + - [GetView](#getview) | ||
77 | + - [GetResponsiveView](#getresponsiveview) | ||
78 | + - [How to use it](#how-to-use-it) | ||
79 | + - [GetWidget](#getwidget) | ||
80 | + - [GetxService](#getxservice) | ||
81 | + - [Tests](#tests) | ||
82 | + - [Tips](#tips) | ||
83 | + - [Mockito or mocktail](#mockito-or-mocktail) | ||
84 | + - [Using Get.reset()](#using-getreset) | ||
85 | + - [Get.testMode](#gettestmode) | ||
86 | +- [Breaking changes from 2.0](#breaking-changes-from-20) | ||
87 | +- [Why Getx?](#why-getx) | ||
88 | +- [Community](#community) | ||
89 | + - [Community channels](#community-channels) | ||
90 | + - [How to contribute](#how-to-contribute) | ||
91 | + - [Articles and videos](#articles-and-videos) | ||
92 | + | ||
93 | +# عن المكتبة | ||
94 | + | ||
95 | +- `GetX` مكتبه خفيفه وقوية لفلاتر , توفر المكتبه السرعه العاليه في التحكم في الحاله , نظام حقن `Ddependency injection` ذكي , والتحكم في التنقل بين الصفحات بسرعه وسهوله | ||
96 | + | ||
97 | +- `GetX` | ||
98 | +- تعتمد علي 3 نقاط اساسية . **الانتاجية والسرعه والتنظيم** | ||
99 | + | ||
100 | + - **السرعه:** `GetX` تركز علي السرعه واقل استخدام للموارد,`GetX` لا تستخدم `Streams` او `ChangeNotifier`. | ||
101 | + | ||
102 | + - **الانتاجية:** `GetX` تستخدم طريقه سهله ومريحة في كتابة الكود , لا يهم ماذا تريد انت تبني , يوجد دائما طريقه اسهل لبناء باستخدام `GetX` , ستوفر ساعات من العمل وتوفر لك اعلي سرعه يمكن الوصل لها في تطبيقاتك عموما , يجب ان يهتم المطور بالتخلص من الموارد الغير مستخدمه من الذاكرة , مع `GetX` هذا غير ضروري لانه يتم التخلص من الموارد الغير مستخدمه من الذاكره تلقائيا, اذا اردت تركهم دائما في الذاكرة يمكنك ذلك لكن يجب عليك ان تستخدم `permanent: true` بالاضافه الي توفير الوقت تم تقليل امكانية ترك الموارد في الذاكره بدون التخلص منها , يتم حقن الموارد `lazy` افتراضيا | ||
103 | + | ||
104 | + - **التنظيم:** `GetX` تسمح لك بفصل الـ `view` عن الـ `presentation logic` و `business logic` باكامل, | ||
105 | + بالنسبة للحقن `dependency injection` و التنقل بين الشاشات لا تحتاج فيهم `context` للتنقل بين الصفحات , ولا تحتاك `context` للوصول للموارد عن طريق widget tree, لذلك يتم الفصل بالكامل بين `presentation logic` و `business logic` لا تحتاج لحقن ال `Controllers/Models/Blocs` | ||
106 | + داخل شجره العناصر `Widget Tree` خلال `MultiProvider`s. | ||
107 | + لان , `GetX` تستخدم نظام حقن خاص بها ويمكنك من فصل الـ `DI` عن الوجهات بالكامل . | ||
108 | + | ||
109 | +- مع `Getx` تعرف ايه يكون الكود الخاص ب كل جزء في التطبيق , تساعدك في كتابة كود نظيف , بالاضافه الي سهوله التطوير مستقبلا , وهذا يمكنك من مشاركه الاجزاء `modules` امر صعب ليصبح سهل جدا . | ||
110 | + `BLOC` كان نقطه البداية لهذا الامر وتظيم الكود بهذه الطريقه في فلاتر , عن طريق فصل كود البزنس عن الواجهات , `GetX` هي التطور لذلك الامر , وذلك عن طريق الاضافه الي ذلك فصل حقن الموارد وفصل التنقل بين الشاشات ايضا , وطبقه البيانات بالكامل ايضا , تعلم اين يكون كل شي في المشروع | ||
111 | + | ||
112 | +- `Getx` توفر لك السهوله في بناء المشروع والاستقرار كلما كبر حجم المشروع واقصي سرعه ممكن , توفر لك ايضا نظام كامل يعمل في تجانس تام , سهل للمبتدئين , ومنظم للخبراء , امن , مستقر , ومحدث باستمرار ويوفر لك موجموعه من الادوات لتسهل عليك | ||
113 | + | ||
114 | +- `GetX` ليست ضخمه , تمتلك المكتبة العديد من المميزات تجعلك تبدا في البرمجه بدون القلق عن اي شي كل ميزه منهم منقسمه عن الاخري ولا يبداو الا عندما تستخدمهم , اذا استخدمت جزء التحكم في الحاله فقط لن يتم استخدام جزء التنقل بين الشاشات في تطبيقك الا `Compiled` والعكس صحيح ! . | ||
115 | + | ||
116 | +-`Getx` لديها نظام شامل , ومجتمع كبير , وعداد كبير من المطورين , وسوف يتم تحديثها باستمرار , تعمل المكتبة علي كل الانظمه بنفس الكود دون تغيير `Android`, `iOS`, `Web`, `Mac`, `Linux`, `Windows` حتي علي الخادم يمكنك استخدام `Getx` لبناء تطبيقات الويب | ||
117 | +**[Get Server](https://github.com/jonataslaw/get_server)**. | ||
118 | + | ||
119 | +**بالاضافه الي ذلك يمكن محاكاه الامر اكثر في فلاتر والخادم عن طريق [Get CLI](https://github.com/jonataslaw/get_cli)**. | ||
120 | + | ||
121 | +**وللمزيد من الانتاجية يمكنك استخدام اضافه للـ** | ||
122 | + | ||
123 | +- [فيجوال ستوديو كود](https://marketplace.visualstudio.com/items?itemName=get-snippets.get-snippets) | ||
124 | +- [اندرويد استوديو و انتلج](https://plugins.jetbrains.com/plugin/14975-getx-snippets) | ||
125 | + | ||
126 | +# التركيب | ||
127 | + | ||
128 | +استخدم المكتبة في ملف `pubspec.yaml` | ||
129 | + | ||
130 | +<div dir="ltr" > | ||
131 | + | ||
132 | +```yaml | ||
133 | +dependencies: | ||
134 | + get: | ||
135 | +``` | ||
136 | + | ||
137 | +</div> | ||
138 | +استدعي المكتبة في الملفات الي ستستخدمها | ||
139 | +<div dir="ltr" > | ||
140 | + | ||
141 | +```dart | ||
142 | +import 'package:get/get.dart'; | ||
143 | +``` | ||
144 | + | ||
145 | +</div> | ||
146 | + | ||
147 | +# بناء تطبيق العداد 🔢 | ||
148 | + | ||
149 | +تطبيق العداد الذي يتم انشاء مع كل مشروع جديد يتعدي ال 100 سطر (بالتعليقات) ولكي اريك مدي قوه `GetX` | ||
150 | +ساوضح لك كيفيه بناء التطبيق مع تغير قيمه العداد مع كل ضغطه زر والتقل بين الشاشات ومشاركه الحاله كل ذلك بطريقه منذمه وفصل تام لكود البزنس عن الواجهات فقط ب 26 سطر من ضمنهم التعليقات 🔥 | ||
151 | + | ||
152 | +- الخطوه الاولي : | ||
153 | + اكتب `Get` امام `MaterialApp` لتصبح `GetMaterialApp` | ||
154 | + | ||
155 | +<div dir="ltr" > | ||
156 | + | ||
157 | +```dart | ||
158 | +void main() => runApp(GetMaterialApp(home: Home())); | ||
159 | +``` | ||
160 | + | ||
161 | +</div> | ||
162 | + | ||
163 | +- ملحوظه : هذا لا يعتبر تعديل علي `MaterialApp` لان , `GetMaterialApp` عباره عن عنصر معد مسبقا ويستخدم `MaterialApp` تحت الغطاء , يمكن تغير الاعدادات يدوين لكن هذا غير ضروري لان ``سيقوم بعمل المسارات و حقن العناصر والترجمه وكل شي تحتاجه ولكن اذا كنت تنوي لاستخدام المكتبة فقط للتحكم في الحاله`State managment`فهذه الخطوه غير ضرورية تكون هذه الخطوه ضرورية عندما تريد التنقل بين الشاشات او عرض`snackbars`والترجمه و اي شي يعتمد علي`context`وتقوم`getx` بتوفيره | ||
164 | + | ||
165 | +- الخطوه الثانية | ||
166 | + قم بكتابة الكود داخل `class` وكتابة المتغيرات والدوال , يمكنك جعل المتغير قابلع لاعاده بناء الواجها عند تغير قيمته باستخدام ال `getter` `.obs` . | ||
167 | + | ||
168 | +<div dir="ltr" > | ||
169 | + | ||
170 | +```dart | ||
171 | +class Controller extends GetxController{ | ||
172 | + var count = 0.obs; | ||
173 | + increment() => count++; | ||
174 | +} | ||
175 | +``` | ||
176 | + | ||
177 | +</div> | ||
178 | + | ||
179 | +- الخطوه الثالثه | ||
180 | +ابني الواجهه واستخدم `StatelessWidget` لتوفير الموارد , مع `Getx` يمكنك الاستغناء عن `StatefulWidget`. | ||
181 | +<div dir="ltr" > | ||
182 | + | ||
183 | +```dart | ||
184 | +class Home extends StatelessWidget { | ||
185 | + | ||
186 | + @override | ||
187 | + Widget build(context) { | ||
188 | + | ||
189 | + // Instantiate your class using Get.put() to make it available for all "child" routes there. | ||
190 | + final c = Get.put(Controller()); | ||
191 | + | ||
192 | + return Scaffold( | ||
193 | + // Use Obx(()=> to update Text() whenever count is changed. | ||
194 | + appBar: AppBar(title: Obx(() => Text("Clicks: ${c.count}"))), | ||
195 | + | ||
196 | + // Replace the 8 lines Navigator.push by a simple Get.to(). You don't need context | ||
197 | + body: Center(child: ElevatedButton( | ||
198 | + child: Text("Go to Other"), onPressed: () => Get.to(Other()))), | ||
199 | + floatingActionButton: | ||
200 | + FloatingActionButton(child: Icon(Icons.add), onPressed: c.increment)); | ||
201 | + } | ||
202 | +} | ||
203 | + | ||
204 | +class Other extends StatelessWidget { | ||
205 | + // You can ask Get to find a Controller that is being used by another page and redirect you to it. | ||
206 | + final Controller c = Get.find(); | ||
207 | + | ||
208 | + @override | ||
209 | + Widget build(context){ | ||
210 | + // Access the updated count variable | ||
211 | + return Scaffold(body: Center(child: Text("${c.count}"))); | ||
212 | + } | ||
213 | +} | ||
214 | +``` | ||
215 | + | ||
216 | +</div> | ||
217 | + | ||
218 | +Result: | ||
219 | + | ||
220 | + | ||
221 | + | ||
222 | +This is a simple project but it already makes clear how powerful Get is. As your project grows, this difference will become more significant. | ||
223 | + | ||
224 | +Get was designed to work with teams, but it makes the job of an individual developer simple. | ||
225 | + | ||
226 | +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! | ||
227 | + | ||
228 | +# The Three pillars | ||
229 | + | ||
230 | +## State management | ||
231 | + | ||
232 | +Get has two different state managers: the simple state manager (we'll call it GetBuilder) and the reactive state manager (GetX/Obx) | ||
233 | + | ||
234 | +### Reactive State Manager | ||
235 | + | ||
236 | +Reactive programming can alienate many people because it is said to be complicated. GetX turns reactive programming into something quite simple: | ||
237 | + | ||
238 | +- You won't need to create StreamControllers. | ||
239 | +- You won't need to create a StreamBuilder for each variable | ||
240 | +- You will not need to create a class for each state. | ||
241 | +- You will not need to create a get for an initial value. | ||
242 | +- You will not need to use code generators | ||
243 | + | ||
244 | +Reactive programming with Get is as easy as using setState. | ||
245 | + | ||
246 | +Let's imagine that you have a name variable and want that every time you change it, all widgets that use it are automatically changed. | ||
247 | + | ||
248 | +This is your count variable: | ||
249 | + | ||
250 | +```dart | ||
251 | +var name = 'Jonatas Borges'; | ||
252 | +``` | ||
253 | + | ||
254 | +To make it observable, you just need to add ".obs" to the end of it: | ||
255 | + | ||
256 | +```dart | ||
257 | +var name = 'Jonatas Borges'.obs; | ||
258 | +``` | ||
259 | + | ||
260 | +And in the UI, when you want to show that value and update the screen whenever the values changes, simply do this: | ||
261 | + | ||
262 | +```dart | ||
263 | +Obx(() => Text("${controller.name}")); | ||
264 | +``` | ||
265 | + | ||
266 | +That's all. It's _that_ simple. | ||
267 | + | ||
268 | +### More details about state management | ||
269 | + | ||
270 | +**See an more in-depth explanation of state management [here](./documentation/en_US/state_management.md). There you will see more examples and also the difference between the simple state manager and the reactive state manager** | ||
271 | + | ||
272 | +You will get a good idea of GetX power. | ||
273 | + | ||
274 | +## Route management | ||
275 | + | ||
276 | +If you are going to use routes/snackbars/dialogs/bottomsheets without context, GetX is excellent for you too, just see it: | ||
277 | + | ||
278 | +Add "Get" before your MaterialApp, turning it into GetMaterialApp | ||
279 | + | ||
280 | +```dart | ||
281 | +GetMaterialApp( // Before: MaterialApp( | ||
282 | + home: MyHome(), | ||
283 | +) | ||
284 | +``` | ||
285 | + | ||
286 | +Navigate to a new screen: | ||
287 | + | ||
288 | +```dart | ||
289 | + | ||
290 | +Get.to(NextScreen()); | ||
291 | +``` | ||
292 | + | ||
293 | +Navigate to new screen with name. See more details on named routes [here](./documentation/en_US/route_management.md#navigation-with-named-routes) | ||
294 | + | ||
295 | +```dart | ||
296 | + | ||
297 | +Get.toNamed('/details'); | ||
298 | +``` | ||
299 | + | ||
300 | +To close snackbars, dialogs, bottomsheets, or anything you would normally close with Navigator.pop(context); | ||
301 | + | ||
302 | +```dart | ||
303 | +Get.back(); | ||
304 | +``` | ||
305 | + | ||
306 | +To go to the next screen and no option to go back to the previous screen (for use in SplashScreens, login screens, etc.) | ||
307 | + | ||
308 | +```dart | ||
309 | +Get.off(NextScreen()); | ||
310 | +``` | ||
311 | + | ||
312 | +To go to the next screen and cancel all previous routes (useful in shopping carts, polls, and tests) | ||
313 | + | ||
314 | +```dart | ||
315 | +Get.offAll(NextScreen()); | ||
316 | +``` | ||
317 | + | ||
318 | +Noticed that you didn't have to use context to do any of these things? That's one of the biggest advantages of using Get route management. With this, you can execute all these methods from within your controller class, without worries. | ||
319 | + | ||
320 | +### More details about route management | ||
321 | + | ||
322 | +**Get works with named routes and also offers lower-level control over your routes! There is in-depth documentation [here](./documentation/en_US/route_management.md)** | ||
323 | + | ||
324 | +## Dependency management | ||
325 | + | ||
326 | +Get has a simple and powerful dependency manager that allows you to retrieve the same class as your Bloc or Controller with just 1 lines of code, no Provider context, no inheritedWidget: | ||
327 | + | ||
328 | +```dart | ||
329 | +Controller controller = Get.put(Controller()); // Rather Controller controller = Controller(); | ||
330 | +``` | ||
331 | + | ||
332 | +- Note: If you are using Get's State Manager, pay more attention to the bindings API, which will make it easier to connect your view to your controller. | ||
333 | + | ||
334 | +Instead of instantiating your class within the class you are using, you are instantiating it within the Get instance, which will make it available throughout your App. | ||
335 | +So you can use your controller (or class Bloc) normally | ||
336 | + | ||
337 | +**Tip:** Get dependency management is decoupled from other parts of the package, so if for example, your app is already using a state manager (any one, it doesn't matter), you don't need to rewrite it all, you can use this dependency injection with no problems at all | ||
338 | + | ||
339 | +```dart | ||
340 | +controller.fetchApi(); | ||
341 | +``` | ||
342 | + | ||
343 | +Imagine that you have navigated through numerous routes, and you need data that was left behind in your controller, you would need a state manager combined with the Provider or Get_it, correct? Not with Get. You just need to ask Get to "find" for your controller, you don't need any additional dependencies: | ||
344 | + | ||
345 | +```dart | ||
346 | +Controller controller = Get.find(); | ||
347 | +//Yes, it looks like Magic, Get will find your controller, and will deliver it to you. You can have 1 million controllers instantiated, Get will always give you the right controller. | ||
348 | +``` | ||
349 | + | ||
350 | +And then you will be able to recover your controller data that was obtained back there: | ||
351 | + | ||
352 | +```dart | ||
353 | +Text(controller.textFromApi); | ||
354 | +``` | ||
355 | + | ||
356 | +### More details about dependency management | ||
357 | + | ||
358 | +**See a more in-depth explanation of dependency management [here](./documentation/en_US/dependency_management.md)** | ||
359 | + | ||
360 | +# Utils | ||
361 | + | ||
362 | +## Internationalization | ||
363 | + | ||
364 | +### Translations | ||
365 | + | ||
366 | +Translations are kept as a simple key-value dictionary map. | ||
367 | +To add custom translations, create a class and extend `Translations`. | ||
368 | + | ||
369 | +```dart | ||
370 | +import 'package:get/get.dart'; | ||
371 | + | ||
372 | +class Messages extends Translations { | ||
373 | + @override | ||
374 | + Map<String, Map<String, String>> get keys => { | ||
375 | + 'en_US': { | ||
376 | + 'hello': 'Hello World', | ||
377 | + }, | ||
378 | + 'de_DE': { | ||
379 | + 'hello': 'Hallo Welt', | ||
380 | + } | ||
381 | + }; | ||
382 | +} | ||
383 | +``` | ||
384 | + | ||
385 | +#### Using translations | ||
386 | + | ||
387 | +Just append `.tr` to the specified key and it will be translated, using the current value of `Get.locale` and `Get.fallbackLocale`. | ||
388 | + | ||
389 | +```dart | ||
390 | +Text('title'.tr); | ||
391 | +``` | ||
392 | + | ||
393 | +#### Using translation with singular and plural | ||
394 | + | ||
395 | +```dart | ||
396 | +var products = []; | ||
397 | +Text('singularKey'.trPlural('pluralKey', products.length, Args)); | ||
398 | +``` | ||
399 | + | ||
400 | +#### Using translation with parameters | ||
401 | + | ||
402 | +```dart | ||
403 | +import 'package:get/get.dart'; | ||
404 | + | ||
405 | + | ||
406 | +Map<String, Map<String, String>> get keys => { | ||
407 | + 'en_US': { | ||
408 | + 'logged_in': 'logged in as @name with email @email', | ||
409 | + }, | ||
410 | + 'es_ES': { | ||
411 | + 'logged_in': 'iniciado sesión como @name con e-mail @email', | ||
412 | + } | ||
413 | +}; | ||
414 | + | ||
415 | +Text('logged_in'.trParams({ | ||
416 | + 'name': 'Jhon', | ||
417 | + 'email': 'jhon@example.com' | ||
418 | + })); | ||
419 | +``` | ||
420 | + | ||
421 | +### Locales | ||
422 | + | ||
423 | +Pass parameters to `GetMaterialApp` to define the locale and translations. | ||
424 | + | ||
425 | +```dart | ||
426 | +return GetMaterialApp( | ||
427 | + translations: Messages(), // your translations | ||
428 | + locale: Locale('en', 'US'), // translations will be displayed in that locale | ||
429 | + fallbackLocale: Locale('en', 'UK'), // specify the fallback locale in case an invalid locale is selected. | ||
430 | +); | ||
431 | +``` | ||
432 | + | ||
433 | +#### Change locale | ||
434 | + | ||
435 | +Call `Get.updateLocale(locale)` to update the locale. Translations then automatically use the new locale. | ||
436 | + | ||
437 | +```dart | ||
438 | +var locale = Locale('en', 'US'); | ||
439 | +Get.updateLocale(locale); | ||
440 | +``` | ||
441 | + | ||
442 | +#### System locale | ||
443 | + | ||
444 | +To read the system locale, you could use `Get.deviceLocale`. | ||
445 | + | ||
446 | +```dart | ||
447 | +return GetMaterialApp( | ||
448 | + locale: Get.deviceLocale, | ||
449 | +); | ||
450 | +``` | ||
451 | + | ||
452 | +## Change Theme | ||
453 | + | ||
454 | +Please do not use any higher level widget than `GetMaterialApp` in order to update it. This can trigger duplicate keys. A lot of people are used to the prehistoric approach of creating a "ThemeProvider" widget just to change the theme of your app, and this is definitely NOT necessary with **GetX™**. | ||
455 | + | ||
456 | +You can create your custom theme and simply add it within `Get.changeTheme` without any boilerplate for that: | ||
457 | + | ||
458 | +```dart | ||
459 | +Get.changeTheme(ThemeData.light()); | ||
460 | +``` | ||
461 | + | ||
462 | +If you want to create something like a button that changes the Theme in `onTap`, you can combine two **GetX™** APIs for that: | ||
463 | + | ||
464 | +- The api that checks if the dark `Theme` is being used. | ||
465 | +- And the `Theme` Change API, you can just put this within an `onPressed`: | ||
466 | + | ||
467 | +```dart | ||
468 | +Get.changeTheme(Get.isDarkMode? ThemeData.light(): ThemeData.dark()); | ||
469 | +``` | ||
470 | + | ||
471 | +When `.darkmode` is activated, it will switch to the _light theme_, and when the _light theme_ becomes active, it will change to _dark theme_. | ||
472 | + | ||
473 | +## GetConnect | ||
474 | + | ||
475 | +GetConnect is an easy way to communicate from your back to your front with http or websockets | ||
476 | + | ||
477 | +### Default configuration | ||
478 | + | ||
479 | +You can simply extend GetConnect and use the GET/POST/PUT/DELETE/SOCKET methods to communicate with your Rest API or websockets. | ||
480 | + | ||
481 | +```dart | ||
482 | +class UserProvider extends GetConnect { | ||
483 | + // Get request | ||
484 | + Future<Response> getUser(int id) => get('http://youapi/users/$id'); | ||
485 | + // Post request | ||
486 | + Future<Response> postUser(Map data) => post('http://youapi/users', body: data); | ||
487 | + // Post request with File | ||
488 | + Future<Response<CasesModel>> postCases(List<int> image) { | ||
489 | + final form = FormData({ | ||
490 | + 'file': MultipartFile(image, filename: 'avatar.png'), | ||
491 | + 'otherFile': MultipartFile(image, filename: 'cover.png'), | ||
492 | + }); | ||
493 | + return post('http://youapi/users/upload', form); | ||
494 | + } | ||
495 | + | ||
496 | + GetSocket userMessages() { | ||
497 | + return socket('https://yourapi/users/socket'); | ||
498 | + } | ||
499 | +} | ||
500 | +``` | ||
501 | + | ||
502 | +### Custom configuration | ||
503 | + | ||
504 | +GetConnect is highly customizable You can define base Url, as answer modifiers, as Requests modifiers, define an authenticator, and even the number of attempts in which it will try to authenticate itself, in addition to giving the possibility to define a standard decoder that will transform all your requests into your Models without any additional configuration. | ||
505 | + | ||
506 | +```dart | ||
507 | +class HomeProvider extends GetConnect { | ||
508 | + @override | ||
509 | + void onInit() { | ||
510 | + // All request will pass to jsonEncode so CasesModel.fromJson() | ||
511 | + httpClient.defaultDecoder = CasesModel.fromJson; | ||
512 | + httpClient.baseUrl = 'https://api.covid19api.com'; | ||
513 | + // baseUrl = 'https://api.covid19api.com'; // It define baseUrl to | ||
514 | + // Http and websockets if used with no [httpClient] instance | ||
515 | + | ||
516 | + // It's will attach 'apikey' property on header from all requests | ||
517 | + httpClient.addRequestModifier((request) { | ||
518 | + request.headers['apikey'] = '12345678'; | ||
519 | + return request; | ||
520 | + }); | ||
521 | + | ||
522 | + // Even if the server sends data from the country "Brazil", | ||
523 | + // it will never be displayed to users, because you remove | ||
524 | + // that data from the response, even before the response is delivered | ||
525 | + httpClient.addResponseModifier<CasesModel>((request, response) { | ||
526 | + CasesModel model = response.body; | ||
527 | + if (model.countries.contains('Brazil')) { | ||
528 | + model.countries.remove('Brazilll'); | ||
529 | + } | ||
530 | + }); | ||
531 | + | ||
532 | + httpClient.addAuthenticator((request) async { | ||
533 | + final response = await get("http://yourapi/token"); | ||
534 | + final token = response.body['token']; | ||
535 | + // Set the header | ||
536 | + request.headers['Authorization'] = "$token"; | ||
537 | + return request; | ||
538 | + }); | ||
539 | + | ||
540 | + //Autenticator will be called 3 times if HttpStatus is | ||
541 | + //HttpStatus.unauthorized | ||
542 | + httpClient.maxAuthRetries = 3; | ||
543 | + } | ||
544 | + } | ||
545 | + | ||
546 | + @override | ||
547 | + Future<Response<CasesModel>> getCases(String path) => get(path); | ||
548 | +} | ||
549 | +``` | ||
550 | + | ||
551 | +## GetPage Middleware | ||
552 | + | ||
553 | +The GetPage has now new property that takes a list of GetMiddleWare and run them in the specific order. | ||
554 | + | ||
555 | +**Note**: When GetPage has a Middlewares, all the children of this page will have the same middlewares automatically. | ||
556 | + | ||
557 | +### Priority | ||
558 | + | ||
559 | +The Order of the Middlewares to run can be set by the priority in the GetMiddleware. | ||
560 | + | ||
561 | +```dart | ||
562 | +final middlewares = [ | ||
563 | + GetMiddleware(priority: 2), | ||
564 | + GetMiddleware(priority: 5), | ||
565 | + GetMiddleware(priority: 4), | ||
566 | + GetMiddleware(priority: -8), | ||
567 | +]; | ||
568 | +``` | ||
569 | + | ||
570 | +those middlewares will be run in this order **-8 => 2 => 4 => 5** | ||
571 | + | ||
572 | +### Redirect | ||
573 | + | ||
574 | +This function will be called when the page of the called route is being searched for. It takes RouteSettings as a result to redirect to. Or give it null and there will be no redirecting. | ||
575 | + | ||
576 | +```dart | ||
577 | +RouteSettings redirect(String route) { | ||
578 | + final authService = Get.find<AuthService>(); | ||
579 | + return authService.authed.value ? null : RouteSettings(name: '/login') | ||
580 | +} | ||
581 | +``` | ||
582 | + | ||
583 | +### onPageCalled | ||
584 | + | ||
585 | +This function will be called when this Page is called before anything created | ||
586 | +you can use it to change something about the page or give it new page | ||
587 | + | ||
588 | +```dart | ||
589 | +GetPage onPageCalled(GetPage page) { | ||
590 | + final authService = Get.find<AuthService>(); | ||
591 | + return page.copyWith(title: 'Welcome ${authService.UserName}'); | ||
592 | +} | ||
593 | +``` | ||
594 | + | ||
595 | +### OnBindingsStart | ||
596 | + | ||
597 | +This function will be called right before the Bindings are initialize. | ||
598 | +Here you can change Bindings for this page. | ||
599 | + | ||
600 | +```dart | ||
601 | +List<Bindings> onBindingsStart(List<Bindings> bindings) { | ||
602 | + final authService = Get.find<AuthService>(); | ||
603 | + if (authService.isAdmin) { | ||
604 | + bindings.add(AdminBinding()); | ||
605 | + } | ||
606 | + return bindings; | ||
607 | +} | ||
608 | +``` | ||
609 | + | ||
610 | +### OnPageBuildStart | ||
611 | + | ||
612 | +This function will be called right after the Bindings are initialize. | ||
613 | +Here you can do something after that you created the bindings and before creating the page widget. | ||
614 | + | ||
615 | +```dart | ||
616 | +GetPageBuilder onPageBuildStart(GetPageBuilder page) { | ||
617 | + print('bindings are ready'); | ||
618 | + return page; | ||
619 | +} | ||
620 | +``` | ||
621 | + | ||
622 | +### OnPageBuilt | ||
623 | + | ||
624 | +This function will be called right after the GetPage.page function is called and will give you the result of the function. and take the widget that will be showed. | ||
625 | + | ||
626 | +### OnPageDispose | ||
627 | + | ||
628 | +This function will be called right after disposing all the related objects (Controllers, views, ...) of the page. | ||
629 | + | ||
630 | +## Other Advanced APIs | ||
631 | + | ||
632 | +```dart | ||
633 | +// give the current args from currentScreen | ||
634 | +Get.arguments | ||
635 | + | ||
636 | +// give name of previous route | ||
637 | +Get.previousRoute | ||
638 | + | ||
639 | +// give the raw route to access for example, rawRoute.isFirst() | ||
640 | +Get.rawRoute | ||
641 | + | ||
642 | +// give access to Routing API from GetObserver | ||
643 | +Get.routing | ||
644 | + | ||
645 | +// check if snackbar is open | ||
646 | +Get.isSnackbarOpen | ||
647 | + | ||
648 | +// check if dialog is open | ||
649 | +Get.isDialogOpen | ||
650 | + | ||
651 | +// check if bottomsheet is open | ||
652 | +Get.isBottomSheetOpen | ||
653 | + | ||
654 | +// remove one route. | ||
655 | +Get.removeRoute() | ||
656 | + | ||
657 | +// back repeatedly until the predicate returns true. | ||
658 | +Get.until() | ||
659 | + | ||
660 | +// go to next route and remove all the previous routes until the predicate returns true. | ||
661 | +Get.offUntil() | ||
662 | + | ||
663 | +// go to next named route and remove all the previous routes until the predicate returns true. | ||
664 | +Get.offNamedUntil() | ||
665 | + | ||
666 | +//Check in what platform the app is running | ||
667 | +GetPlatform.isAndroid | ||
668 | +GetPlatform.isIOS | ||
669 | +GetPlatform.isMacOS | ||
670 | +GetPlatform.isWindows | ||
671 | +GetPlatform.isLinux | ||
672 | +GetPlatform.isFuchsia | ||
673 | + | ||
674 | +//Check the device type | ||
675 | +GetPlatform.isMobile | ||
676 | +GetPlatform.isDesktop | ||
677 | +//All platforms are supported independently in web! | ||
678 | +//You can tell if you are running inside a browser | ||
679 | +//on Windows, iOS, OSX, Android, etc. | ||
680 | +GetPlatform.isWeb | ||
681 | + | ||
682 | + | ||
683 | +// Equivalent to : MediaQuery.of(context).size.height, | ||
684 | +// but immutable. | ||
685 | +Get.height | ||
686 | +Get.width | ||
687 | + | ||
688 | +// Gives the current context of the Navigator. | ||
689 | +Get.context | ||
690 | + | ||
691 | +// Gives the context of the snackbar/dialog/bottomsheet in the foreground, anywhere in your code. | ||
692 | +Get.contextOverlay | ||
693 | + | ||
694 | +// Note: the following methods are extensions on context. Since you | ||
695 | +// have access to context in any place of your UI, you can use it anywhere in the UI code | ||
696 | + | ||
697 | +// If you need a changeable height/width (like Desktop or browser windows that can be scaled) you will need to use context. | ||
698 | +context.width | ||
699 | +context.height | ||
700 | + | ||
701 | +// Gives you the power to define half the screen, a third of it and so on. | ||
702 | +// Useful for responsive applications. | ||
703 | +// param dividedBy (double) optional - default: 1 | ||
704 | +// param reducedBy (double) optional - default: 0 | ||
705 | +context.heightTransformer() | ||
706 | +context.widthTransformer() | ||
707 | + | ||
708 | +/// Similar to MediaQuery.of(context).size | ||
709 | +context.mediaQuerySize() | ||
710 | + | ||
711 | +/// Similar to MediaQuery.of(context).padding | ||
712 | +context.mediaQueryPadding() | ||
713 | + | ||
714 | +/// Similar to MediaQuery.of(context).viewPadding | ||
715 | +context.mediaQueryViewPadding() | ||
716 | + | ||
717 | +/// Similar to MediaQuery.of(context).viewInsets; | ||
718 | +context.mediaQueryViewInsets() | ||
719 | + | ||
720 | +/// Similar to MediaQuery.of(context).orientation; | ||
721 | +context.orientation() | ||
722 | + | ||
723 | +/// Check if device is on landscape mode | ||
724 | +context.isLandscape() | ||
725 | + | ||
726 | +/// Check if device is on portrait mode | ||
727 | +context.isPortrait() | ||
728 | + | ||
729 | +/// Similar to MediaQuery.of(context).devicePixelRatio; | ||
730 | +context.devicePixelRatio() | ||
731 | + | ||
732 | +/// Similar to MediaQuery.of(context).textScaleFactor; | ||
733 | +context.textScaleFactor() | ||
734 | + | ||
735 | +/// Get the shortestSide from screen | ||
736 | +context.mediaQueryShortestSide() | ||
737 | + | ||
738 | +/// True if width be larger than 800 | ||
739 | +context.showNavbar() | ||
740 | + | ||
741 | +/// True if the shortestSide is smaller than 600p | ||
742 | +context.isPhone() | ||
743 | + | ||
744 | +/// True if the shortestSide is largest than 600p | ||
745 | +context.isSmallTablet() | ||
746 | + | ||
747 | +/// True if the shortestSide is largest than 720p | ||
748 | +context.isLargeTablet() | ||
749 | + | ||
750 | +/// True if the current device is Tablet | ||
751 | +context.isTablet() | ||
752 | + | ||
753 | +/// Returns a value<T> according to the screen size | ||
754 | +/// can give value for: | ||
755 | +/// watch: if the shortestSide is smaller than 300 | ||
756 | +/// mobile: if the shortestSide is smaller than 600 | ||
757 | +/// tablet: if the shortestSide is smaller than 1200 | ||
758 | +/// desktop: if width is largest than 1200 | ||
759 | +context.responsiveValue<T>() | ||
760 | +``` | ||
761 | + | ||
762 | +### Optional Global Settings and Manual configurations | ||
763 | + | ||
764 | +GetMaterialApp configures everything for you, but if you want to configure Get manually. | ||
765 | + | ||
766 | +```dart | ||
767 | +MaterialApp( | ||
768 | + navigatorKey: Get.key, | ||
769 | + navigatorObservers: [GetObserver()], | ||
770 | +); | ||
771 | +``` | ||
772 | + | ||
773 | +You will also be able to use your own Middleware within `GetObserver`, this will not influence anything. | ||
774 | + | ||
775 | +```dart | ||
776 | +MaterialApp( | ||
777 | + navigatorKey: Get.key, | ||
778 | + navigatorObservers: [ | ||
779 | + GetObserver(MiddleWare.observer) // Here | ||
780 | + ], | ||
781 | +); | ||
782 | +``` | ||
783 | + | ||
784 | +You can create _Global Settings_ for `Get`. Just add `Get.config` to your code before pushing any route. | ||
785 | +Or do it directly in your `GetMaterialApp` | ||
786 | + | ||
787 | +```dart | ||
788 | +GetMaterialApp( | ||
789 | + enableLog: true, | ||
790 | + defaultTransition: Transition.fade, | ||
791 | + opaqueRoute: Get.isOpaqueRouteDefault, | ||
792 | + popGesture: Get.isPopGestureEnable, | ||
793 | + transitionDuration: Get.defaultDurationTransition, | ||
794 | + defaultGlobalState: Get.defaultGlobalState, | ||
795 | +); | ||
796 | + | ||
797 | +Get.config( | ||
798 | + enableLog = true, | ||
799 | + defaultPopGesture = true, | ||
800 | + defaultTransition = Transitions.cupertino | ||
801 | +) | ||
802 | +``` | ||
803 | + | ||
804 | +You can optionally redirect all the logging messages from `Get`. | ||
805 | +If you want to use your own, favourite logging package, | ||
806 | +and want to capture the logs there: | ||
807 | + | ||
808 | +```dart | ||
809 | +GetMaterialApp( | ||
810 | + enableLog: true, | ||
811 | + logWriterCallback: localLogWriter, | ||
812 | +); | ||
813 | + | ||
814 | +void localLogWriter(String text, {bool isError = false}) { | ||
815 | + // pass the message to your favourite logging package here | ||
816 | + // please note that even if enableLog: false log messages will be pushed in this callback | ||
817 | + // you get check the flag if you want through GetConfig.isLogEnable | ||
818 | +} | ||
819 | + | ||
820 | +``` | ||
821 | + | ||
822 | +### Local State Widgets | ||
823 | + | ||
824 | +These Widgets allows you to manage a single value, and keep the state ephemeral and locally. | ||
825 | +We have flavours for Reactive and Simple. | ||
826 | +For instance, you might use them to toggle obscureText in a `TextField`, maybe create a custom | ||
827 | +Expandable Panel, or maybe modify the current index in `BottomNavigationBar` while changing the content | ||
828 | +of the body in a `Scaffold`. | ||
829 | + | ||
830 | +#### ValueBuilder | ||
831 | + | ||
832 | +A simplification of `StatefulWidget` that works with a `.setState` callback that takes the updated value. | ||
833 | + | ||
834 | +```dart | ||
835 | +ValueBuilder<bool>( | ||
836 | + initialValue: false, | ||
837 | + builder: (value, updateFn) => Switch( | ||
838 | + value: value, | ||
839 | + onChanged: updateFn, // same signature! you could use ( newValue ) => updateFn( newValue ) | ||
840 | + ), | ||
841 | + // if you need to call something outside the builder method. | ||
842 | + onUpdate: (value) => print("Value updated: $value"), | ||
843 | + onDispose: () => print("Widget unmounted"), | ||
844 | +), | ||
845 | +``` | ||
846 | + | ||
847 | +#### ObxValue | ||
848 | + | ||
849 | +Similar to [`ValueBuilder`](#valuebuilder), but this is the Reactive version, you pass a Rx instance (remember the magical .obs?) and | ||
850 | +updates automatically... isn't it awesome? | ||
851 | + | ||
852 | +```dart | ||
853 | +ObxValue((data) => Switch( | ||
854 | + value: data.value, | ||
855 | + onChanged: data, // Rx has a _callable_ function! You could use (flag) => data.value = flag, | ||
856 | + ), | ||
857 | + false.obs, | ||
858 | +), | ||
859 | +``` | ||
860 | + | ||
861 | +## Useful tips | ||
862 | + | ||
863 | +`.obs`ervables (also known as _Rx_ Types) have a wide variety of internal methods and operators. | ||
864 | + | ||
865 | +> Is very common to _believe_ that a property with `.obs` **IS** the actual value... but make no mistake! | ||
866 | +> We avoid the Type declaration of the variable, because Dart's compiler is smart enough, and the code | ||
867 | +> looks cleaner, but: | ||
868 | + | ||
869 | +```dart | ||
870 | +var message = 'Hello world'.obs; | ||
871 | +print( 'Message "$message" has Type ${message.runtimeType}'); | ||
872 | +``` | ||
873 | + | ||
874 | +Even if `message` _prints_ the actual String value, the Type is **RxString**! | ||
875 | + | ||
876 | +So, you can't do `message.substring( 0, 4 )`. | ||
877 | +You have to access the real `value` inside the _observable_: | ||
878 | +The most "used way" is `.value`, but, did you know that you can also use... | ||
879 | + | ||
880 | +```dart | ||
881 | +final name = 'GetX'.obs; | ||
882 | +// only "updates" the stream, if the value is different from the current one. | ||
883 | +name.value = 'Hey'; | ||
884 | + | ||
885 | +// All Rx properties are "callable" and returns the new value. | ||
886 | +// but this approach does not accepts `null`, the UI will not rebuild. | ||
887 | +name('Hello'); | ||
888 | + | ||
889 | +// is like a getter, prints 'Hello'. | ||
890 | +name() ; | ||
891 | + | ||
892 | +/// numbers: | ||
893 | + | ||
894 | +final count = 0.obs; | ||
895 | + | ||
896 | +// You can use all non mutable operations from num primitives! | ||
897 | +count + 1; | ||
898 | + | ||
899 | +// Watch out! this is only valid if `count` is not final, but var | ||
900 | +count += 1; | ||
901 | + | ||
902 | +// You can also compare against values: | ||
903 | +count > 2; | ||
904 | + | ||
905 | +/// booleans: | ||
906 | + | ||
907 | +final flag = false.obs; | ||
908 | + | ||
909 | +// switches the value between true/false | ||
910 | +flag.toggle(); | ||
911 | + | ||
912 | + | ||
913 | +/// all types: | ||
914 | + | ||
915 | +// Sets the `value` to null. | ||
916 | +flag.nil(); | ||
917 | + | ||
918 | +// All toString(), toJson() operations are passed down to the `value` | ||
919 | +print( count ); // calls `toString()` inside for RxInt | ||
920 | + | ||
921 | +final abc = [0,1,2].obs; | ||
922 | +// Converts the value to a json Array, prints RxList | ||
923 | +// Json is supported by all Rx types! | ||
924 | +print('json: ${jsonEncode(abc)}, type: ${abc.runtimeType}'); | ||
925 | + | ||
926 | +// RxMap, RxList and RxSet are special Rx types, that extends their native types. | ||
927 | +// but you can work with a List as a regular list, although is reactive! | ||
928 | +abc.add(12); // pushes 12 to the list, and UPDATES the stream. | ||
929 | +abc[3]; // like Lists, reads the index 3. | ||
930 | + | ||
931 | + | ||
932 | +// equality works with the Rx and the value, but hashCode is always taken from the value | ||
933 | +final number = 12.obs; | ||
934 | +print( number == 12 ); // prints > true | ||
935 | + | ||
936 | +/// Custom Rx Models: | ||
937 | + | ||
938 | +// toJson(), toString() are deferred to the child, so you can implement override on them, and print() the observable directly. | ||
939 | + | ||
940 | +class User { | ||
941 | + String name, last; | ||
942 | + int age; | ||
943 | + User({this.name, this.last, this.age}); | ||
944 | + | ||
945 | + @override | ||
946 | + String toString() => '$name $last, $age years old'; | ||
947 | +} | ||
948 | + | ||
949 | +final user = User(name: 'John', last: 'Doe', age: 33).obs; | ||
950 | + | ||
951 | +// `user` is "reactive", but the properties inside ARE NOT! | ||
952 | +// So, if we change some variable inside of it... | ||
953 | +user.value.name = 'Roi'; | ||
954 | +// The widget will not rebuild!, | ||
955 | +// `Rx` don't have any clue when you change something inside user. | ||
956 | +// So, for custom classes, we need to manually "notify" the change. | ||
957 | +user.refresh(); | ||
958 | + | ||
959 | +// or we can use the `update()` method! | ||
960 | +user.update((value){ | ||
961 | + value.name='Roi'; | ||
962 | +}); | ||
963 | + | ||
964 | +print( user ); | ||
965 | +``` | ||
966 | + | ||
967 | +## StateMixin | ||
968 | + | ||
969 | +Another way to handle your `UI` state is use the `StateMixin<T>` . | ||
970 | +To implement it, use the `with` to add the `StateMixin<T>` | ||
971 | +to your controller which allows a T model. | ||
972 | + | ||
973 | +```dart | ||
974 | +class Controller extends GetController with StateMixin<User>{} | ||
975 | +``` | ||
976 | + | ||
977 | +The `change()` method change the State whenever we want. | ||
978 | +Just pass the data and the status in this way: | ||
979 | + | ||
980 | +```dart | ||
981 | +change(data, status: RxStatus.success()); | ||
982 | +``` | ||
983 | + | ||
984 | +RxStatus allow these status: | ||
985 | + | ||
986 | +```dart | ||
987 | +RxStatus.loading(); | ||
988 | +RxStatus.success(); | ||
989 | +RxStatus.empty(); | ||
990 | +RxStatus.error('message'); | ||
991 | +``` | ||
992 | + | ||
993 | +To represent it in the UI, use: | ||
994 | + | ||
995 | +```dart | ||
996 | +class OtherClass extends GetView<Controller> { | ||
997 | + @override | ||
998 | + Widget build(BuildContext context) { | ||
999 | + return Scaffold( | ||
1000 | + | ||
1001 | + body: controller.obx( | ||
1002 | + (state)=>Text(state.name), | ||
1003 | + | ||
1004 | + // here you can put your custom loading indicator, but | ||
1005 | + // by default would be Center(child:CircularProgressIndicator()) | ||
1006 | + onLoading: CustomLoadingIndicator(), | ||
1007 | + onEmpty: Text('No data found'), | ||
1008 | + | ||
1009 | + // here also you can set your own error widget, but by | ||
1010 | + // default will be an Center(child:Text(error)) | ||
1011 | + onError: (error)=>Text(error), | ||
1012 | + ), | ||
1013 | + ); | ||
1014 | +} | ||
1015 | +``` | ||
1016 | + | ||
1017 | +#### GetView | ||
1018 | + | ||
1019 | +I love this Widget, is so simple, yet, so useful! | ||
1020 | + | ||
1021 | +Is a `const Stateless` Widget that has a getter `controller` for a registered `Controller`, that's all. | ||
1022 | + | ||
1023 | +```dart | ||
1024 | + class AwesomeController extends GetController { | ||
1025 | + final String title = 'My Awesome View'; | ||
1026 | + } | ||
1027 | + | ||
1028 | + // ALWAYS remember to pass the `Type` you used to register your controller! | ||
1029 | + class AwesomeView extends GetView<AwesomeController> { | ||
1030 | + @override | ||
1031 | + Widget build(BuildContext context) { | ||
1032 | + return Container( | ||
1033 | + padding: EdgeInsets.all(20), | ||
1034 | + child: Text(controller.title), // just call `controller.something` | ||
1035 | + ); | ||
1036 | + } | ||
1037 | + } | ||
1038 | +``` | ||
1039 | + | ||
1040 | +#### GetResponsiveView | ||
1041 | + | ||
1042 | +Extend this widget to build responsive view. | ||
1043 | +this widget contains the `screen` property that have all | ||
1044 | +information about the screen size and type. | ||
1045 | + | ||
1046 | +##### How to use it | ||
1047 | + | ||
1048 | +You have two options to build it. | ||
1049 | + | ||
1050 | +- with `builder` method you return the widget to build. | ||
1051 | +- with methods `desktop`, `tablet`,`phone`, `watch`. the specific | ||
1052 | + method will be built when the screen type matches the method | ||
1053 | + when the screen is [ScreenType.Tablet] the `tablet` method | ||
1054 | + will be exuded and so on. | ||
1055 | + **Note:** If you use this method please set the property `alwaysUseBuilder` to `false` | ||
1056 | + | ||
1057 | +With `settings` property you can set the width limit for the screen types. | ||
1058 | + | ||
1059 | + | ||
1060 | +Code to this screen | ||
1061 | +[code](https://github.com/SchabanBo/get_page_example/blob/master/lib/pages/responsive_example/responsive_view.dart) | ||
1062 | + | ||
1063 | +#### GetWidget | ||
1064 | + | ||
1065 | +Most people have no idea about this Widget, or totally confuse the usage of it. | ||
1066 | +The use case is very rare, but very specific: It `caches` a Controller. | ||
1067 | +Because of the _cache_, can't be a `const Stateless`. | ||
1068 | + | ||
1069 | +> So, when do you need to "cache" a Controller? | ||
1070 | + | ||
1071 | +If you use, another "not so common" feature of **GetX**: `Get.create()`. | ||
1072 | + | ||
1073 | +`Get.create(()=>Controller())` will generate a new `Controller` each time you call | ||
1074 | +`Get.find<Controller>()`, | ||
1075 | + | ||
1076 | +That's where `GetWidget` shines... as you can use it, for example, | ||
1077 | +to keep a list of Todo items. So, if the widget gets "rebuilt", it will keep the same controller instance. | ||
1078 | + | ||
1079 | +#### GetxService | ||
1080 | + | ||
1081 | +This class is like a `GetxController`, it shares the same lifecycle ( `onInit()`, `onReady()`, `onClose()`). | ||
1082 | +But has no "logic" inside of it. It just notifies **GetX** Dependency Injection system, that this subclass | ||
1083 | +**can not** be removed from memory. | ||
1084 | + | ||
1085 | +So is super useful to keep your "Services" always reachable and active with `Get.find()`. Like: | ||
1086 | +`ApiService`, `StorageService`, `CacheService`. | ||
1087 | + | ||
1088 | +```dart | ||
1089 | +Future<void> main() async { | ||
1090 | + await initServices(); /// AWAIT SERVICES INITIALIZATION. | ||
1091 | + runApp(SomeApp()); | ||
1092 | +} | ||
1093 | + | ||
1094 | +/// Is a smart move to make your Services intiialize before you run the Flutter app. | ||
1095 | +/// as you can control the execution flow (maybe you need to load some Theme configuration, | ||
1096 | +/// apiKey, language defined by the User... so load SettingService before running ApiService. | ||
1097 | +/// so GetMaterialApp() doesnt have to rebuild, and takes the values directly. | ||
1098 | +void initServices() async { | ||
1099 | + print('starting services ...'); | ||
1100 | + /// Here is where you put get_storage, hive, shared_pref initialization. | ||
1101 | + /// or moor connection, or whatever that's async. | ||
1102 | + await Get.putAsync(() => DbService().init()); | ||
1103 | + await Get.putAsync(SettingsService()).init(); | ||
1104 | + print('All services started...'); | ||
1105 | +} | ||
1106 | + | ||
1107 | +class DbService extends GetxService { | ||
1108 | + Future<DbService> init() async { | ||
1109 | + print('$runtimeType delays 2 sec'); | ||
1110 | + await 2.delay(); | ||
1111 | + print('$runtimeType ready!'); | ||
1112 | + return this; | ||
1113 | + } | ||
1114 | +} | ||
1115 | + | ||
1116 | +class SettingsService extends GetxService { | ||
1117 | + void init() async { | ||
1118 | + print('$runtimeType delays 1 sec'); | ||
1119 | + await 1.delay(); | ||
1120 | + print('$runtimeType ready!'); | ||
1121 | + } | ||
1122 | +} | ||
1123 | + | ||
1124 | +``` | ||
1125 | + | ||
1126 | +The only way to actually delete a `GetxService`, is with `Get.reset()` which is like a | ||
1127 | +"Hot Reboot" of your app. So remember, if you need absolute persistence of a class instance during the | ||
1128 | +lifetime of your app, use `GetxService`. | ||
1129 | + | ||
1130 | +### Tests | ||
1131 | + | ||
1132 | +You can test your controllers like any other class, including their lifecycles: | ||
1133 | + | ||
1134 | +```dart | ||
1135 | +class Controller extends GetxController { | ||
1136 | + @override | ||
1137 | + void onInit() { | ||
1138 | + super.onInit(); | ||
1139 | + //Change value to name2 | ||
1140 | + name.value = 'name2'; | ||
1141 | + } | ||
1142 | + | ||
1143 | + @override | ||
1144 | + void onClose() { | ||
1145 | + name.value = ''; | ||
1146 | + super.onClose(); | ||
1147 | + } | ||
1148 | + | ||
1149 | + final name = 'name1'.obs; | ||
1150 | + | ||
1151 | + void changeName() => name.value = 'name3'; | ||
1152 | +} | ||
1153 | + | ||
1154 | +void main() { | ||
1155 | + test(''' | ||
1156 | +Test the state of the reactive variable "name" across all of its lifecycles''', | ||
1157 | + () { | ||
1158 | + /// You can test the controller without the lifecycle, | ||
1159 | + /// but it's not recommended unless you're not using | ||
1160 | + /// GetX dependency injection | ||
1161 | + final controller = Controller(); | ||
1162 | + expect(controller.name.value, 'name1'); | ||
1163 | + | ||
1164 | + /// If you are using it, you can test everything, | ||
1165 | + /// including the state of the application after each lifecycle. | ||
1166 | + Get.put(controller); // onInit was called | ||
1167 | + expect(controller.name.value, 'name2'); | ||
1168 | + | ||
1169 | + /// Test your functions | ||
1170 | + controller.changeName(); | ||
1171 | + expect(controller.name.value, 'name3'); | ||
1172 | + | ||
1173 | + /// onClose was called | ||
1174 | + Get.delete<Controller>(); | ||
1175 | + | ||
1176 | + expect(controller.name.value, ''); | ||
1177 | + }); | ||
1178 | +} | ||
1179 | +``` | ||
1180 | + | ||
1181 | +#### Tips | ||
1182 | + | ||
1183 | +##### Mockito or mocktail | ||
1184 | + | ||
1185 | +If you need to mock your GetxController/GetxService, you should extend GetxController, and mixin it with Mock, that way | ||
1186 | + | ||
1187 | +```dart | ||
1188 | +class NotificationServiceMock extends GetxService with Mock implements NotificationService {} | ||
1189 | +``` | ||
1190 | + | ||
1191 | +##### Using Get.reset() | ||
1192 | + | ||
1193 | +If you are testing widgets, or test groups, use Get.reset at the end of your test or in tearDown to reset all settings from your previous test. | ||
1194 | + | ||
1195 | +##### Get.testMode | ||
1196 | + | ||
1197 | +if you are using your navigation in your controllers, use `Get.testMode = true` at the beginning of your main. | ||
1198 | + | ||
1199 | +# Breaking changes from 2.0 | ||
1200 | + | ||
1201 | +1- Rx types: | ||
1202 | + | ||
1203 | +| Before | After | | ||
1204 | +| ------- | ---------- | | ||
1205 | +| StringX | `RxString` | | ||
1206 | +| IntX | `RxInt` | | ||
1207 | +| MapX | `RxMap` | | ||
1208 | +| ListX | `RxList` | | ||
1209 | +| NumX | `RxNum` | | ||
1210 | +| DoubleX | `RxDouble` | | ||
1211 | + | ||
1212 | +RxController and GetBuilder now have merged, you no longer need to memorize which controller you want to use, just use GetxController, it will work for simple state management and for reactive as well. | ||
1213 | + | ||
1214 | +2- NamedRoutes | ||
1215 | +Before: | ||
1216 | + | ||
1217 | +```dart | ||
1218 | +GetMaterialApp( | ||
1219 | + namedRoutes: { | ||
1220 | + '/': GetRoute(page: Home()), | ||
1221 | + } | ||
1222 | +) | ||
1223 | +``` | ||
1224 | + | ||
1225 | +Now: | ||
1226 | + | ||
1227 | +```dart | ||
1228 | +GetMaterialApp( | ||
1229 | + getPages: [ | ||
1230 | + GetPage(name: '/', page: () => Home()), | ||
1231 | + ] | ||
1232 | +) | ||
1233 | +``` | ||
1234 | + | ||
1235 | +Why this change? | ||
1236 | +Often, it may be necessary to decide which page will be displayed from a parameter, or a login token, the previous approach was inflexible, as it did not allow this. | ||
1237 | +Inserting the page into a function has significantly reduced the RAM consumption, since the routes will not be allocated in memory since the app was started, and it also allowed to do this type of approach: | ||
1238 | + | ||
1239 | +```dart | ||
1240 | + | ||
1241 | +GetStorage box = GetStorage(); | ||
1242 | + | ||
1243 | +GetMaterialApp( | ||
1244 | + getPages: [ | ||
1245 | + GetPage(name: '/', page:(){ | ||
1246 | + return box.hasData('token') ? Home() : Login(); | ||
1247 | + }) | ||
1248 | + ] | ||
1249 | +) | ||
1250 | +``` | ||
1251 | + | ||
1252 | +# Why Getx? | ||
1253 | + | ||
1254 | +1- Many times after a Flutter update, many of your packages will break. Sometimes compilation errors happen, errors often appear that there are still no answers about, and the developer needs to know where the error came from, track the error, only then try to open an issue in the corresponding repository, and see its problem solved. Get centralizes the main resources for development (State, dependency and route management), allowing you to add a single package to your pubspec, and start working. After a Flutter update, the only thing you need to do is update the Get dependency, and get to work. Get also resolves compatibility issues. How many times a version of a package is not compatible with the version of another, because one uses a dependency in one version, and the other in another version? This is also not a concern using Get, as everything is in the same package and is fully compatible. | ||
1255 | + | ||
1256 | +2- Flutter is easy, Flutter is incredible, but Flutter still has some boilerplate that may be unwanted for most developers, such as `Navigator.of(context).push (context, builder [...]`. Get simplifies development. Instead of writing 8 lines of code to just call a route, you can just do it: `Get.to(Home())` and you're done, you'll go to the next page. Dynamic web urls are a really painful thing to do with Flutter currently, and that with GetX is stupidly simple. Managing states in Flutter, and managing dependencies is also something that generates a lot of discussion, as there are hundreds of patterns in the pub. But there is nothing as easy as adding a ".obs" at the end of your variable, and place your widget inside an Obx, and that's it, all updates to that variable will be automatically updated on the screen. | ||
1257 | + | ||
1258 | +3- Ease without worrying about performance. Flutter's performance is already amazing, but imagine that you use a state manager, and a locator to distribute your blocs/stores/controllers/ etc. classes. You will have to manually call the exclusion of that dependency when you don't need it. But have you ever thought of simply using your controller, and when it was no longer being used by anyone, it would simply be deleted from memory? That's what GetX does. With SmartManagement, everything that is not being used is deleted from memory, and you shouldn't have to worry about anything but programming. You will be assured that you are consuming the minimum necessary resources, without even having created a logic for this. | ||
1259 | + | ||
1260 | +4- Actual decoupling. You may have heard the concept "separate the view from the business logic". This is not a peculiarity of BLoC, MVC, MVVM, and any other standard on the market has this concept. However, this concept can often be mitigated in Flutter due to the use of context. | ||
1261 | +If you need context to find an InheritedWidget, you need it in the view, or pass the context by parameter. I particularly find this solution very ugly, and to work in teams we will always have a dependence on View's business logic. Getx is unorthodox with the standard approach, and while it does not completely ban the use of StatefulWidgets, InitState, etc., it always has a similar approach that can be cleaner. Controllers have life cycles, and when you need to make an APIREST request for example, you don't depend on anything in the view. You can use onInit to initiate the http call, and when the data arrives, the variables will be populated. As GetX is fully reactive (really, and works under streams), once the items are filled, all widgets that use that variable will be automatically updated in the view. This allows people with UI expertise to work only with widgets, and not have to send anything to business logic other than user events (like clicking a button), while people working with business logic will be free to create and test the business logic separately. | ||
1262 | + | ||
1263 | +This library will always be updated and implementing new features. Feel free to offer PRs and contribute to them. | ||
1264 | + | ||
1265 | +# Community | ||
1266 | + | ||
1267 | +## Community channels | ||
1268 | + | ||
1269 | +GetX has a highly active and helpful community. If you have questions, or would like any assistance regarding the use of this framework, please join our community channels, your question will be answered more quickly, and it will be the most suitable place. This repository is exclusive for opening issues, and requesting resources, but feel free to be part of GetX Community. | ||
1270 | + | ||
1271 | +| **Slack** | **Discord** | **Telegram** | | ||
1272 | +| :-------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------- | | ||
1273 | +| [](https://communityinviter.com/apps/getxworkspace/getx) | [](https://discord.com/invite/9Hpt99N) | [](https://t.me/joinchat/PhdbJRmsZNpAqSLJL6bH7g) | | ||
1274 | + | ||
1275 | +## How to contribute | ||
1276 | + | ||
1277 | +_Want to contribute to the project? We will be proud to highlight you as one of our collaborators. Here are some points where you can contribute and make Get (and Flutter) even better._ | ||
1278 | + | ||
1279 | +- Helping to translate the readme into other languages. | ||
1280 | +- Adding documentation to the readme (a lot of Get's functions haven't been documented yet). | ||
1281 | +- Write articles or make videos teaching how to use Get (they will be inserted in the Readme and in the future in our Wiki). | ||
1282 | +- Offering PRs for code/tests. | ||
1283 | +- Including new functions. | ||
1284 | + | ||
1285 | +Any contribution is welcome! | ||
1286 | + | ||
1287 | +## Articles and videos | ||
1288 | + | ||
1289 | +- [Flutter Getx EcoSystem package for arabic people](https://www.youtube.com/playlist?list=PLV1fXIAyjeuZ6M8m56zajMUwu4uE3-SL0) - Tutorial by [Pesa Coder](https://github.com/UsamaElgendy). | ||
1290 | +- [Dynamic Themes in 3 lines using GetX™](https://medium.com/swlh/flutter-dynamic-themes-in-3-lines-c3b375f292e3) - Tutorial by [Rod Brown](https://github.com/RodBr). | ||
1291 | +- [Complete GetX™ Navigation](https://www.youtube.com/watch?v=RaqPIoJSTtI) - Route management video by Amateur Coder. | ||
1292 | +- [Complete GetX State Management](https://www.youtube.com/watch?v=CNpXbeI_slw) - State management video by Amateur Coder. | ||
1293 | +- [GetX™ Other Features](https://youtu.be/ttQtlX_Q0eU) - Utils, storage, bindings and other features video by Amateur Coder. | ||
1294 | +- [Firestore User with GetX | Todo App](https://www.youtube.com/watch?v=BiV0DcXgk58) - Video by Amateur Coder. | ||
1295 | +- [Firebase Auth with GetX | Todo App](https://www.youtube.com/watch?v=-H-T_BSgfOE) - Video by Amateur Coder. | ||
1296 | +- [The Flutter GetX™ Ecosystem ~ State Management](https://medium.com/flutter-community/the-flutter-getx-ecosystem-state-management-881c7235511d) - State management by [Aachman Garg](https://github.com/imaachman). | ||
1297 | +- [The Flutter GetX™ Ecosystem ~ Dependency Injection](https://medium.com/flutter-community/the-flutter-getx-ecosystem-dependency-injection-8e763d0ec6b9) - Dependency Injection by [Aachman Garg](https://github.com/imaachman). | ||
1298 | +- [GetX, the all-in-one Flutter package](https://www.youtube.com/watch?v=IYQgtu9TM74) - A brief tutorial covering State Management and Navigation by Thad Carnevalli. | ||
1299 | +- [Build a To-do List App from scratch using Flutter and GetX](https://www.youtube.com/watch?v=EcnqFasHf18) - UI + State Management + Storage video by Thad Carnevalli. | ||
1300 | +- [GetX Flutter Firebase Auth Example](https://medium.com/@jeffmcmorris/getx-flutter-firebase-auth-example-b383c1dd1de2) - Article by Jeff McMorris. | ||
1301 | +- [Flutter State Management with GetX – Complete App](https://www.appwithflutter.com/flutter-state-management-with-getx/) - by App With Flutter. | ||
1302 | +- [Flutter Routing with Animation using Get Package](https://www.appwithflutter.com/flutter-routing-using-get-package/) - by App With Flutter. | ||
1303 | +- [A minimal example on dartpad](https://dartpad.dev/2b3d0d6f9d4e312c5fdbefc414c1727e?) - by [Roi Peker](https://github.com/roipeker) | ||
1304 | + | ||
1305 | +</div> |
README.ja-JP.md
0 → 100644
1 | + | ||
2 | + | ||
3 | +[](https://pub.dev/packages/get) | ||
4 | +[](https://pub.dev/packages/get/score) | ||
5 | + | ||
6 | +[](https://pub.dev/packages/effective_dart) | ||
7 | +[](https://discord.com/invite/9Hpt99N) | ||
8 | +[](https://communityinviter.com/apps/getxworkspace/getx) | ||
9 | +[](https://t.me/joinchat/PhdbJRmsZNpAqSLJL6bH7g) | ||
10 | +<a href="https://github.com/Solido/awesome-flutter"> | ||
11 | +<img alt="Awesome Flutter" src="https://img.shields.io/badge/Awesome-Flutter-blue.svg?longCache=true&style=flat-square" /> | ||
12 | +</a> | ||
13 | +<a href="https://www.buymeacoffee.com/jonataslaw" target="_blank"><img src="https://i.imgur.com/aV6DDA7.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important; box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" > </a> | ||
14 | + | ||
15 | + | ||
16 | + | ||
17 | + | ||
18 | +<div align="center"> | ||
19 | + | ||
20 | +**言語** | ||
21 | + | ||
22 | + | ||
23 | +[](README.md) | ||
24 | +[](README-vi.md) | ||
25 | +[](README.id-ID.md) | ||
26 | +[](README.ur-PK.md) | ||
27 | +[](README.zh-cn.md) | ||
28 | +[](README.pt-br.md) | ||
29 | +[](README-es.md) | ||
30 | +[](README.ru.md) | ||
31 | +[](README.pl.md) | ||
32 | +[](README.ko-kr.md) | ||
33 | +[](README-fr.md) | ||
34 | +[](README-ja.md) | ||
35 | + | ||
36 | +</div> | ||
37 | + | ||
38 | +- [Getとは](#Getとは) | ||
39 | +- [インストール方法](#インストール方法) | ||
40 | +- [GetXによるカウンターアプリ](#GetXによるカウンターアプリ) | ||
41 | +- [三本柱](#三本柱) | ||
42 | + - [状態管理](#状態管理) | ||
43 | + - [リアクティブな状態管理](#リアクティブな状態管理) | ||
44 | + - [状態管理に関する詳細ドキュメント](#状態管理に関する詳細ドキュメント) | ||
45 | + - [Route管理](#Route管理) | ||
46 | + - [Route管理に関する詳細ドキュメント](#Route管理に関する詳細ドキュメント) | ||
47 | + - [依存オブジェクト管理](#依存オブジェクト管理) | ||
48 | + - [依存オブジェクト管理に関する詳細ドキュメント](#依存オブジェクト管理に関する詳細ドキュメント) | ||
49 | +- [ユーティリティ](#ユーティリティ) | ||
50 | + - [多言語対応](#多言語対応) | ||
51 | + - [翻訳ファイル](#翻訳ファイル) | ||
52 | + - [翻訳ファイルの利用](#翻訳ファイルの利用) | ||
53 | + - [ロケール](#ロケール) | ||
54 | + - [ロケールの変更](#ロケールの変更) | ||
55 | + - [システムのロケールを読み込む](#システムのロケールを読み込む) | ||
56 | + - [Themeの変更](#Themeの変更) | ||
57 | + - [GetConnect](#getconnect) | ||
58 | + - [デフォルト設定](#デフォルト設定) | ||
59 | + - [カスタム設定](#カスタム設定) | ||
60 | + - [GetPageにミドルウェアを設定](#GetPageにミドルウェアを設定) | ||
61 | + - [実行優先度](#実行優先度) | ||
62 | + - [redirect](#redirect) | ||
63 | + - [onPageCalled](#onpagecalled) | ||
64 | + - [onBindingsStart](#onbindingsstart) | ||
65 | + - [onPageBuildStart](#onpagebuildstart) | ||
66 | + - [onPageBuilt](#onpagebuilt) | ||
67 | + - [onPageDispose](#onpagedispose) | ||
68 | + - [その他API](#その他API) | ||
69 | + - [オプションのグローバル設定と手動設定](#オプションのグローバル設定と手動設定) | ||
70 | + - [ローカルステートWidget](#ローカルステートWidget) | ||
71 | + - [ValueBuilder](#valuebuilder) | ||
72 | + - [ObxValue](#obxvalue) | ||
73 | + - [お役立ちTIPS](#お役立ちTIPS) | ||
74 | + - [StateMixin](#statemixin) | ||
75 | + - [GetView](#getview) | ||
76 | + - [GetResponsiveView](#getresponsiveview) | ||
77 | + - [使い方](#使い方]) | ||
78 | + - [GetWidget](#getwidget) | ||
79 | + - [GetxService](#getxservice) | ||
80 | + - [テストの実行](#テストの実行) | ||
81 | + - [mockitoやmocktailを使う場合](#mockitoやmocktailを使う場合) | ||
82 | + - [Get.reset()](#Get.reset()) | ||
83 | + - [Get.testMode](#Get.testMode) | ||
84 | +- [バージョン2.0からの破壊的変更](#バージョン2.0からの破壊的変更) | ||
85 | +- [なぜGetXなのか](#なぜGetXなのか) | ||
86 | +- [コミュニティ](#コミュニティ) | ||
87 | + - [コミュニティチャンネル](#コミュニティチャンネル) | ||
88 | + - [コントリビュート方法](#コントリビュート方法) | ||
89 | + - [GetXに関する記事と動画](#GetXに関する記事と動画) | ||
90 | + | ||
91 | +# Getとは | ||
92 | + | ||
93 | +- GetXはFlutterのための超軽量でパワフルなソリューションです。高パフォーマンスな状態管理機能、インテリジェントな依存オブジェクト管理機能、そしてRoute管理機能の三本柱を軽量かつ実用的な形で組み合わせています。 | ||
94 | + | ||
95 | +- GetXは3つの基本原則を念頭に開発されています。 **【生産性、パフォーマンス、コードの分離性】** これらはライブラリ内のすべてのリソースに優先適用されている原則です。 | ||
96 | + | ||
97 | + - **パフォーマンス:** GetXは高いパフォーマンスと最小限のリソース消費を目標にしています。GetXはでは Stream および ChangeNotifier を利用しなくて済みます。 | ||
98 | + | ||
99 | + - **生産性:** GetXはシンプルで使い心地のいいシンタックスを採用しています。あなたの実現したい機能がどんなものであれ、GetXを使えばより簡単に実現できる方法が見つかるでしょう。開発にかかる時間を短縮し、あなたのアプリケーションのパフォーマンスを最大限引き出してくれます。 | ||
100 | + | ||
101 | + 開発者はメモリリソースの管理に気を配るのが常です。しかしGetXでは、リソースが使用されていないときはメモリから削除されるのがデフォルト動作のため、過度に気にかける必要はありません。(逆にメモリに残しておきたい場合は、依存オブジェクトをインスタンス化するメソッドを使う際に「permanent: true」と宣言してください)これにより時間が節約できますし、不要な依存オブジェクトがメモリ上に残るリスクも少なくなります。メモリへの読み込みについてもデフォルトは遅延読み込みであり、使用するときに初めてメモリ上に読み込まれます。 | ||
102 | + | ||
103 | + - **コードの分離性:** GetXを使うと、ビュー、プレゼンテーションロジック、ビジネスロジック、依存オブジェクトの注入、およびナビゲーション周りのコードを書き分けやすくなります。Routeのナビゲーションにはcontextを必要としないため、Widgetツリーに依存することはありません。ロジックについてもInheritedWidget経由でController/BLoCにアクセスする際のcontextは必要ありません。プレゼンテーションロジックとビジネスロジックをUIクラスから完全に切り離すことができます。また、Controller/モデル/BLoCのクラスを、`MultiProvider`を使ってWidgetツリーに注入する必要もありません。GetXでは独自の依存オブジェクト注入機能を使用し、ビュークラスからビューとは無関係なコードをなくすことができるのです。 | ||
104 | + | ||
105 | + GetXを使うことでアプリケーションの各機能がどこにあるのかがわかりやすくなり、自然と見やすいコードになります。メンテナンスが容易になるだけでなく、それまでのFlutterでは考えられなかったモジュール共有が簡単に実現できるようになりました。 | ||
106 | + BLoCはこの分野におけるFlutterの出発点と言えるものでしたが、GetXはこれを正統進化させており、ビジネスロジックのみならずプレゼンテーションロジックも分離することができます。そのほかデータレイヤーはもちろん、依存オブジェクトやRouteの注入に関するコードも。どこに何が配置されているのか全体の見通しがしやすくなり、Hello Worldを表示させるかのように簡単にアプリの機能を利用できるようになるでしょう。 | ||
107 | + Flutterアプリを作るならGetXは最も簡単で実用的、かつスケーラブルなソリューションです。強力なエコシステムも存在があるため、初心者にはわかりやすさ、プロには正確性を提供することができます。そしてFlutter SDKにはない幅広い種類のAPIを提供し、セキュアで安定的な環境を構築します。 | ||
108 | + | ||
109 | +- GetXは肥大化したライブラリではありません。何も気にせずすぐに開発を始められるよう多数の機能を標準で備えていますが、それぞれの機能は個別にコンテナに入っており、使用してはじめて起動します。状態管理機能しか利用していない場合はその機能だけがコンパイルされます。Route管理機能だけを利用していれば、状態管理機能がコンパイルされることはありません。 | ||
110 | + | ||
111 | +- GetXには巨大なエコシステム、コミュニティ、コラボレーターの存在があるため、Flutterが存在する限りメンテナンスされ続けます。またGetXもFlutterと同様にAndroid、iOS、Web、Mac、Linux、Windows、そしてあなたのサーバー上で、単一のコードから実行することができます。 | ||
112 | + | ||
113 | +**[Get Server](https://github.com/jonataslaw/get_server)を使うことで、フロントエンドで作成したコードをバックエンドで再利用することが可能です。** | ||
114 | + | ||
115 | +**さらに、[Get CLI](https://github.com/jonataslaw/get_cli)を使えば、サーバー側でもフロントエンド側でも開発プロセス全体を自動化することができます。** | ||
116 | + | ||
117 | +**また、生産性をさらに高めるためのツールとして、[VSCode用の拡張機能](https://marketplace.visualstudio.com/items?itemName=get-snippets.get-snippets) と [Android Studio/Intellij用の拡張機能](https://plugins.jetbrains.com/plugin/14975-getx-snippets)があります。** | ||
118 | + | ||
119 | +# インストール方法 | ||
120 | + | ||
121 | +Getパッケージを pubspec.yaml に追加します: | ||
122 | + | ||
123 | +```yaml | ||
124 | +dependencies: | ||
125 | + get: | ||
126 | +``` | ||
127 | + | ||
128 | +使用するときはこのようにインポートしてください: | ||
129 | + | ||
130 | +```dart | ||
131 | +import 'package:get/get.dart'; | ||
132 | +``` | ||
133 | + | ||
134 | +# GetXによるカウンターアプリ | ||
135 | + | ||
136 | +Flutterで新規プロジェクトを作成する際に表示されるカウンターアプリは、コメントを含めると100行以上あります。Getの実力を示すため、このカウンターアプリを可読性を重視した形で、コメントを含めてわずか26行のコードで作成する方法を紹介します。 | ||
137 | + | ||
138 | +- ステップ1: | ||
139 | + MaterialAppの前に「Get」を足して、GetMaterialAppにします。 | ||
140 | + | ||
141 | +```dart | ||
142 | +void main() => runApp(GetMaterialApp(home: Home())); | ||
143 | +``` | ||
144 | + | ||
145 | +- 注1: GetMaterialAppはFlutterのMaterialAppに手を加えたものではありません。MaterialAppをchildに持ち、諸々の追加設定をしてくれるWidgetに過ぎません。この設定は手動でも可能ですが、その必要はありません。GetMaterialAppは、Routeの作成・注入、言語翻訳の注入など、ナビゲーションに必要なものをすべて注入してくれます。Getを状態管理や依存オブジェクト管理に限定して使用する場合は、GetMaterialAppを使用する必要はありません。GetMaterialAppは、Route、SnackBar、多言語対応、BottomSheet、Dialog、contextなしの高レベルAPIを利用する場合に必要です。 | ||
146 | +- 注2: このステップは、Route管理機能(`Get.to()`や`Get.back()`など)を使用しない場合は、必要ありません。 | ||
147 | + | ||
148 | +- ステップ2: | ||
149 | + ビジネスロジッククラスを作成し、そこに必要な変数、メソッド、コントローラをすべて配置します。 | ||
150 | + 変数に ".obs" を付け足すことで、その変数の値の変化を監視することが可能になります。 | ||
151 | + | ||
152 | +```dart | ||
153 | +class Controller extends GetxController{ | ||
154 | + var count = 0.obs; | ||
155 | + increment() => count++; | ||
156 | +} | ||
157 | +``` | ||
158 | + | ||
159 | +- ステップ3: | ||
160 | + ビューを作成します。StatelessWidgetを使用することでRAMが節約できます。GetではStatefulWidgetを使用する必要がなくなるかもしれません。 | ||
161 | + | ||
162 | +```dart | ||
163 | +class Home extends StatelessWidget { | ||
164 | + | ||
165 | + @override | ||
166 | + Widget build(context) { | ||
167 | + | ||
168 | + // Get.put()を使ってクラスをインスタンス化することですべての子Routeで利用できるようになります。 | ||
169 | + final Controller c = Get.put(Controller()); | ||
170 | + | ||
171 | + return Scaffold( | ||
172 | + // countが変わるたびにTextを更新するにはObx(()=>)を使ってください。 | ||
173 | + appBar: AppBar(title: Obx(() => Text("Clicks: ${c.count}"))), | ||
174 | + | ||
175 | + // 8行使っていたNavigator.pushの代わりに短い Get.to()を使ってください。context不要です。 | ||
176 | + body: Center(child: ElevatedButton( | ||
177 | + child: Text("Go to Other"), onPressed: () => Get.to(Other()))), | ||
178 | + floatingActionButton: | ||
179 | + FloatingActionButton(child: Icon(Icons.add), onPressed: c.increment)); | ||
180 | + } | ||
181 | +} | ||
182 | + | ||
183 | +class Other extends StatelessWidget { | ||
184 | + // 他のページで使われているコントローラーを見つけてきてくれます。 | ||
185 | + final Controller c = Get.find(); | ||
186 | + | ||
187 | + @override | ||
188 | + Widget build(context){ | ||
189 | + // 最新のcount変数の値にアクセス | ||
190 | + return Scaffold(body: Center(child: Text("${c.count}"))); | ||
191 | + } | ||
192 | +} | ||
193 | +``` | ||
194 | + | ||
195 | +Result: | ||
196 | + | ||
197 | + | ||
198 | + | ||
199 | +これはシンプルな例ですが、すでにGetがいかに強力であるかがわかると思います。プロジェクトが大きければ大きいほど、この差はもっと開くでしょう。 | ||
200 | + | ||
201 | +Getはチームでの作業を想定して設計されていますが、個人開発者の仕事もシンプルにしてくれます。 | ||
202 | + | ||
203 | +パフォーマンスを落とさず納期までにすべて納品。Getはすべての人に向いているわけではありませんが、このフレーズにぴんと来た人には確実に向いています! | ||
204 | + | ||
205 | +# 三本柱 | ||
206 | + | ||
207 | +## 状態管理 | ||
208 | + | ||
209 | +Getの状態管理には、非リアクティブ(GetBuilder)と、リアクティブ(GetX/Obx)の2つのアプローチがあります。 | ||
210 | + | ||
211 | +### リアクティブな状態管理 | ||
212 | + | ||
213 | +リアクティブプログラミングは複雑であると言われ、多くの人に敬遠されています。GetXは、リアクティブプログラミングをシンプルなものに変えます: | ||
214 | + | ||
215 | +* StreamControllerを作る必要はありません。 | ||
216 | +* 変数ごとにStreamBuilderをセットする必要はありません。 | ||
217 | +* 状態ごとにクラスを作る必要はありません。 | ||
218 | +* 初期値のためにgetを準備する必要はありません。 | ||
219 | +- コードの自動生成をする必要がありません。 | ||
220 | + | ||
221 | +GetにおけるリアクティブプログラミングはsetStateと同じように簡単です。 | ||
222 | + | ||
223 | +例えば、名前の変数があって、それを変更するたびに、その名前を使っているすべてのWidgetを自動で更新したい場合。 | ||
224 | + | ||
225 | +```dart | ||
226 | +var name = 'Jonatas Borges'; | ||
227 | +``` | ||
228 | + | ||
229 | +このnameをObservable(監視可能)にするには, ".obs"を値の末尾に付けるだけです。 | ||
230 | + | ||
231 | +```dart | ||
232 | +var name = 'Jonatas Borges'.obs; | ||
233 | +``` | ||
234 | + | ||
235 | +UIでその値を表示し、値が変わるたびに内容を更新したい場合は次のようにします。 | ||
236 | + | ||
237 | +```dart | ||
238 | +Obx(() => Text("${controller.name}")); | ||
239 | +``` | ||
240 | + | ||
241 | +以上です。こんなに簡単なんですよ。 | ||
242 | + | ||
243 | +### 状態管理に関する詳細ドキュメント | ||
244 | + | ||
245 | +**状態管理に関するより詳細な説明を知りたい方は[こちらの日本語ドキュメント](./documentation/ja_JP/state_management.md)をご覧ください。多くの事例や、非リアクティブな状態管理とリアクティブな状態管理の違いについても説明されています。** | ||
246 | + | ||
247 | +GetXパワーがもたらす利点をより理解していただけると思います。 | ||
248 | + | ||
249 | +## Route管理 | ||
250 | + | ||
251 | +GetXはcontextなしでRoute/SnackBar/Dialog/BottomSheetを使用することができます。具体的に見ていきましょう。 | ||
252 | + | ||
253 | +いつものMaterialAppの前に「Get」を付け足して、GetMaterialAppにしましょう。 | ||
254 | + | ||
255 | +```dart | ||
256 | +GetMaterialApp( // MaterialApp の前に Get | ||
257 | + home: MyHome(), | ||
258 | +) | ||
259 | +``` | ||
260 | + | ||
261 | +新しいRouteに画面遷移するにはこのシンタックス。 | ||
262 | + | ||
263 | +```dart | ||
264 | +Get.to(NextScreen()); | ||
265 | +``` | ||
266 | + | ||
267 | +名前付きRouteに画面遷移するにはこのシンタックス。名前付きRouteの詳細は[こちらの日本語ドキュメント](./documentation/ja_JP/route_management.md#navigation-with-named-routes) | ||
268 | + | ||
269 | +```dart | ||
270 | +Get.toNamed('/details'); | ||
271 | +``` | ||
272 | + | ||
273 | +SnackBar、Dialog、BottomSheetなど、Navigator.pop(context)で閉じられるRouteはこれで閉じます。 | ||
274 | + | ||
275 | +```dart | ||
276 | +Get.back(); | ||
277 | +``` | ||
278 | + | ||
279 | +次の画面に移動した後、前の画面に戻れないようにする場合(スプラッシュスクリーンやログイン画面など)はこちら。 | ||
280 | + | ||
281 | +```dart | ||
282 | +Get.off(NextScreen()); | ||
283 | +``` | ||
284 | + | ||
285 | +次の画面に進み、前のRouteをすべてキャンセルする場合(ショッピングカート、アンケート、テストなど)はこちら。 | ||
286 | + | ||
287 | +```dart | ||
288 | +Get.offAll(NextScreen()); | ||
289 | +``` | ||
290 | + | ||
291 | +以上、contextを一度も使わなかったことに気付きましたか?これがGetでRoute管理を行う最大のメリットのひとつです。contextを使わないので、たとえばcontrollerクラスの中でも、これらのメソッドを実行することができます。 | ||
292 | + | ||
293 | +### Route管理に関する詳細ドキュメント | ||
294 | + | ||
295 | +**Getは名前付きRouteでも動作し、Routeの下位レベルの制御も可能です。詳細なドキュメントは[こちらの日本語ドキュメント](./documentation/ja_JP/route_management.md)にあります。** | ||
296 | + | ||
297 | +## 依存オブジェクト管理 | ||
298 | + | ||
299 | +Getにはシンプルで強力な依存オブジェクト注入機能があります。わずか1行のコードで、Provider contextやinheritedWidgetも使わず、BLoCやControllerのようなクラスのインスタンスを取得することができます。 | ||
300 | + | ||
301 | +```dart | ||
302 | +Controller controller = Get.put(Controller()); // controller = Controller() とする代わりに | ||
303 | +``` | ||
304 | + | ||
305 | +- 注: Getの状態管理機能を使用している場合は、Bindings APIにもご注目を。BindingsはビューとControllerを結びつけるのをより便利にしてくれます。 | ||
306 | + | ||
307 | +一つのクラスの中でControllerクラスをインスタンス化するのではなく、Getインスタンスの中でインスタンス化することで、アプリ全体でControllerが利用できるようになります。 | ||
308 | + | ||
309 | +**ヒント:** Getの依存オブジェクト注入機能の部分は、パッケージ全体の中でも他の部分と切り離されているので、たとえば、あなたのアプリがすでに状態管理機能を一部で使用していたとしても、それらを書き直す必要はなく、この依存オブジェクト注入機能をそのまま使用することができます。 | ||
310 | + | ||
311 | +```dart | ||
312 | +controller.fetchApi(); | ||
313 | +``` | ||
314 | + | ||
315 | +色々なRouteを行き来した後に、あるControllerクラスのデータにアクセスする必要が生じたとしましょう。ProviderやGet_itなら再びそのクラスに依存オブジェクトを注入する必要がありますよね?Getの場合は違います。Getでは「find」と依頼するだけで、追加の依存オブジェクトの注入は必要ありません。 | ||
316 | + | ||
317 | +```dart | ||
318 | +Controller controller = Get.find(); | ||
319 | +//マジックみたいですね。Getは正しいcontrollerをきちんと探してきてくれますよ。100万のcontrollerのインスタンスがあっても、Getは必ず正しいcontrollerを探し当てます。 | ||
320 | +``` | ||
321 | + | ||
322 | +そして、findで取得したコントローラーのデータをこのように呼び出すことができます。 | ||
323 | + | ||
324 | +```dart | ||
325 | +Text(controller.textFromApi); | ||
326 | +``` | ||
327 | + | ||
328 | +### 依存オブジェクト管理に関する詳細ドキュメント | ||
329 | + | ||
330 | +**依存オブジェクト管理に関するより詳細な説明は[こちらの日本語ドキュメント](./documentation/ja_JP/dependency_management.md)をご覧ください。** | ||
331 | + | ||
332 | +# ユーティリティ | ||
333 | + | ||
334 | +## 多言語対応 | ||
335 | + | ||
336 | +### 翻訳ファイル | ||
337 | + | ||
338 | +翻訳ファイルはシンプルなキーと値のMapとして保持されます。 | ||
339 | +翻訳を追加するには、クラスを作成して `Translations` を継承します。 | ||
340 | + | ||
341 | +```dart | ||
342 | +import 'package:get/get.dart'; | ||
343 | + | ||
344 | +class Messages extends Translations { | ||
345 | + @override | ||
346 | + Map<String, Map<String, String>> get keys => { | ||
347 | + 'en_US': { | ||
348 | + 'hello': 'Hello World', | ||
349 | + }, | ||
350 | + 'de_DE': { | ||
351 | + 'hello': 'Hallo Welt', | ||
352 | + } | ||
353 | + }; | ||
354 | +} | ||
355 | +``` | ||
356 | + | ||
357 | +#### 翻訳ファイルの利用 | ||
358 | + | ||
359 | +指定されたキーに `.tr` (translateのtr)を追加するだけで、`Get.locale` と `Get.fallbackLocale` の現在の値をに沿って適切な言語に翻訳されます。 | ||
360 | + | ||
361 | +```dart | ||
362 | +Text('title'.tr); | ||
363 | +``` | ||
364 | + | ||
365 | +#### 単数系と複数形に対応 | ||
366 | + | ||
367 | +```dart | ||
368 | +var products = []; | ||
369 | +Text('singularKey'.trPlural('pluralKey', products.length, Args)); | ||
370 | +``` | ||
371 | + | ||
372 | +#### パラメーターに対応 | ||
373 | + | ||
374 | +```dart | ||
375 | +import 'package:get/get.dart'; | ||
376 | + | ||
377 | + | ||
378 | +Map<String, Map<String, String>> get keys => { | ||
379 | + 'en_US': { | ||
380 | + 'logged_in': 'logged in as @name with email @email', | ||
381 | + }, | ||
382 | + 'es_ES': { | ||
383 | + 'logged_in': 'iniciado sesión como @name con e-mail @email', | ||
384 | + } | ||
385 | +}; | ||
386 | + | ||
387 | +Text('logged_in'.trParams({ | ||
388 | + 'name': 'Jhon', | ||
389 | + 'email': 'jhon@example.com' | ||
390 | + })); | ||
391 | +``` | ||
392 | + | ||
393 | +### ロケール | ||
394 | + | ||
395 | +ロケールと翻訳を定義するため、`GetMaterialApp`にパラメータを渡します。 | ||
396 | + | ||
397 | +```dart | ||
398 | +return GetMaterialApp( | ||
399 | + translations: Messages(), // Translationsを継承したクラスのインスタンス | ||
400 | + locale: Locale('en', 'US'), // このロケール設定に沿って翻訳が表示される | ||
401 | + fallbackLocale: Locale('en', 'UK'), // 無効なロケールだったときのフォールバックを指定 | ||
402 | +); | ||
403 | +``` | ||
404 | + | ||
405 | +#### ロケールの変更 | ||
406 | + | ||
407 | +ロケールを変更するには、`Get.updateLocale(locale)`を呼び出します。翻訳は新しいロケールに沿ってなされます。 | ||
408 | + | ||
409 | +```dart | ||
410 | +var locale = Locale('en', 'US'); | ||
411 | +Get.updateLocale(locale); | ||
412 | +``` | ||
413 | + | ||
414 | +#### システムのロケールを読み込む | ||
415 | + | ||
416 | +システムのロケールを読み込むには、`Get.deviceLocale`を使用します。 | ||
417 | + | ||
418 | +```dart | ||
419 | +return GetMaterialApp( | ||
420 | + locale: Get.deviceLocale, | ||
421 | +); | ||
422 | +``` | ||
423 | + | ||
424 | +## Themeの変更 | ||
425 | + | ||
426 | +`GetMaterialApp`より上位のWidgetを使ってThemeを変更しないでください。Keyの重複を引き起こす可能性があります。アプリのThemeを変更するためには「ThemeProvider」Widgetを作成するという前時代的なアプローチが採られることが多いですが、**GetX™**ではこのようなことは必要ありません。 | ||
427 | + | ||
428 | +カスタムのThemeDataを作成したら、それを`Get.changeTheme`内に追加するだけです。 | ||
429 | + | ||
430 | +```dart | ||
431 | +Get.changeTheme(ThemeData.light()); | ||
432 | +``` | ||
433 | + | ||
434 | +もし、`onTap`でThemeを変更するボタンを作りたいのであれば、以下の2つの**GetX™** APIを組み合わせることができます。 | ||
435 | + | ||
436 | +- Dark Theme が使われているかどうかをチェックするAPI | ||
437 | +- Theme を変えるAPI(ボタンの`onPressed`の中に設置できます) | ||
438 | + | ||
439 | +```dart | ||
440 | +Get.changeTheme(Get.isDarkMode? ThemeData.light(): ThemeData.dark()); | ||
441 | +``` | ||
442 | + | ||
443 | +Darkモードが有効であれば、_light theme_に切り替わり、Lightモードが有効なら、_dark theme_に切り替わります。 | ||
444 | + | ||
445 | +## GetConnect | ||
446 | + | ||
447 | +GetConnect は、http または websocket を使用してバックエンドとフロントエンド間の通信を行う機能です。 | ||
448 | + | ||
449 | +### デフォルト設定 | ||
450 | + | ||
451 | +GetConnectを拡張することで、GET/POST/PUT/DELETE/SOCKETメソッドを使用して、Rest APIやウェブソケットと通信することができます。 | ||
452 | + | ||
453 | +```dart | ||
454 | +class UserProvider extends GetConnect { | ||
455 | + // Get リクエスト | ||
456 | + Future<Response> getUser(int id) => get('http://youapi/users/$id'); | ||
457 | + // Post リクエスト | ||
458 | + Future<Response> postUser(Map data) => post('http://youapi/users', body: data); | ||
459 | + // File付き Post リクエスト | ||
460 | + Future<Response<CasesModel>> postCases(List<int> image) { | ||
461 | + final form = FormData({ | ||
462 | + 'file': MultipartFile(image, filename: 'avatar.png'), | ||
463 | + 'otherFile': MultipartFile(image, filename: 'cover.png'), | ||
464 | + }); | ||
465 | + return post('http://youapi/users/upload', form); | ||
466 | + } | ||
467 | + | ||
468 | + GetSocket userMessages() { | ||
469 | + return socket('https://yourapi/users/socket'); | ||
470 | + } | ||
471 | +} | ||
472 | +``` | ||
473 | + | ||
474 | +### カスタム設定 | ||
475 | + | ||
476 | +GetConnect は高度なカスタマイズが可能です。ベースUrlの定義はもちろん、リクエストヘッダーを足したり、レスポンスボディに変更を加えたり、認証情報を追加したり、認証回数の制限を設けたりすることができるほか、リクエストをModelに変換するデコーダを定義することもできます。 | ||
477 | + | ||
478 | +```dart | ||
479 | +class HomeProvider extends GetConnect { | ||
480 | + @override | ||
481 | + void onInit() { | ||
482 | + // デフォルトデコーダーをセット | ||
483 | + httpClient.defaultDecoder = CasesModel.fromJson; | ||
484 | + httpClient.baseUrl = 'https://api.covid19api.com'; | ||
485 | + // baseUrlをセット | ||
486 | + | ||
487 | + // リクエストヘッダーに 'apikey' プロパティを付け足しています。 | ||
488 | + httpClient.addRequestModifier((request) { | ||
489 | + request.headers['apikey'] = '12345678'; | ||
490 | + return request; | ||
491 | + }); | ||
492 | + | ||
493 | + // サーバーが"Brazil"を含むデータを送ってきてもユーザーに表示されることはありません。 | ||
494 | + // レスポンスがUIレイヤーに届けられる前にデータが取り除かれているからです。 | ||
495 | + httpClient.addResponseModifier<CasesModel>((request, response) { | ||
496 | + CasesModel model = response.body; | ||
497 | + if (model.countries.contains('Brazil')) { | ||
498 | + model.countries.remove('Brazilll'); | ||
499 | + } | ||
500 | + }); | ||
501 | + | ||
502 | + httpClient.addAuthenticator((request) async { | ||
503 | + final response = await get("http://yourapi/token"); | ||
504 | + final token = response.body['token']; | ||
505 | + // ヘッダーをセット | ||
506 | + request.headers['Authorization'] = "$token"; | ||
507 | + return request; | ||
508 | + }); | ||
509 | + | ||
510 | + // HttpStatus が HttpStatus.unauthorized である限り、 | ||
511 | + // 3回まで認証が試みられます。 | ||
512 | + httpClient.maxAuthRetries = 3; | ||
513 | + } | ||
514 | + } | ||
515 | + | ||
516 | + @override | ||
517 | + Future<Response<CasesModel>> getCases(String path) => get(path); | ||
518 | +} | ||
519 | +``` | ||
520 | + | ||
521 | +## GetPageにミドルウェアを設定 | ||
522 | + | ||
523 | +GetPageに新しいプロパティが追加され、GetMiddleWareのListを設定することができるようになりました。GetMiddleWareは設定した任意の順序で実行されます。 | ||
524 | + | ||
525 | +**注**: GetPageにミドルウェアを設定すると、そのページの子ページはすべて同じミドルウェアを自動的に持つことになります。 | ||
526 | + | ||
527 | +### 実行優先度 | ||
528 | + | ||
529 | +GetMiddlewareに設定したpriority(優先度)の若い順にミドルウェアが実行されます。 | ||
530 | + | ||
531 | +```dart | ||
532 | +final middlewares = [ | ||
533 | + GetMiddleware(priority: 2), | ||
534 | + GetMiddleware(priority: 5), | ||
535 | + GetMiddleware(priority: 4), | ||
536 | + GetMiddleware(priority: -8), | ||
537 | +]; | ||
538 | +``` | ||
539 | + | ||
540 | +この場合の実行順序は **-8 => 2 => 4 => 5** | ||
541 | + | ||
542 | +### redirect | ||
543 | + | ||
544 | +redirect関数は、Routeを呼び出してページが検索されると実行されます。リダイレクト先のRouteSettingsが戻り値となります。もしくはnullを与えれば、リダイレクトは行われません。 | ||
545 | + | ||
546 | +```dart | ||
547 | +RouteSettings redirect(String route) { | ||
548 | + final authService = Get.find<AuthService>(); | ||
549 | + return authService.authed.value ? null : RouteSettings(name: '/login') | ||
550 | +} | ||
551 | +``` | ||
552 | + | ||
553 | +### onPageCalled | ||
554 | + | ||
555 | +onPageCalled関数は、ページが呼び出された直後に実行されます。 | ||
556 | +この関数を使ってページの内容を変更したり、新しいページを作成したりすることができます。 | ||
557 | + | ||
558 | +```dart | ||
559 | +GetPage onPageCalled(GetPage page) { | ||
560 | + final authService = Get.find<AuthService>(); | ||
561 | + return page.copyWith(title: 'Welcome ${authService.UserName}'); | ||
562 | +} | ||
563 | +``` | ||
564 | + | ||
565 | +### onBindingsStart | ||
566 | + | ||
567 | +onBindingsStart関数は、Bindingsが初期化される直前に実行されます。 | ||
568 | +たとえば、ページのBindingsを変更することもできます。 | ||
569 | + | ||
570 | +```dart | ||
571 | +List<Bindings> onBindingsStart(List<Bindings> bindings) { | ||
572 | + final authService = Get.find<AuthService>(); | ||
573 | + if (authService.isAdmin) { | ||
574 | + bindings.add(AdminBinding()); | ||
575 | + } | ||
576 | + return bindings; | ||
577 | +} | ||
578 | +``` | ||
579 | + | ||
580 | +### onPageBuildStart | ||
581 | + | ||
582 | +onPageBuildStart関数は、Bindingsが初期化された直後、ページWidgetが作成される前に実行されます。 | ||
583 | + | ||
584 | +```dart | ||
585 | +GetPageBuilder onPageBuildStart(GetPageBuilder page) { | ||
586 | + print('bindings are ready'); | ||
587 | + return page; | ||
588 | +} | ||
589 | +``` | ||
590 | + | ||
591 | +### onPageBuilt | ||
592 | + | ||
593 | +onPageBuilt関数は、GetPage.page(ページのビルダー)が呼び出された直後に実行され、表示されるWidgetを結果として受け取ることができます。 | ||
594 | + | ||
595 | +### onPageDispose | ||
596 | + | ||
597 | +onPageDispose関数は、ページに関するすべてのオブジェクト(Controller、ビューなど)が破棄された直後に実行されます。 | ||
598 | + | ||
599 | +## その他API | ||
600 | + | ||
601 | +```dart | ||
602 | +// 現在の画面に渡されているargs(引数)を取得 | ||
603 | +Get.arguments | ||
604 | + | ||
605 | +// 直前のRouteの名前("/" など)を取得 | ||
606 | +Get.previousRoute | ||
607 | + | ||
608 | +// 現在のRouteオブジェクトを取得 | ||
609 | +Get.rawRoute | ||
610 | + | ||
611 | +// GetObserverからRoutingを取得 | ||
612 | +Get.routing | ||
613 | + | ||
614 | +// SnackBarが開いているかチェック | ||
615 | +Get.isSnackbarOpen | ||
616 | + | ||
617 | +// Dialogが開いているかチェック | ||
618 | +Get.isDialogOpen | ||
619 | + | ||
620 | +// BottomSheetが開いているかチェック | ||
621 | +Get.isBottomSheetOpen | ||
622 | + | ||
623 | +// Routeを削除 | ||
624 | +Get.removeRoute() | ||
625 | + | ||
626 | +// 引数のRoutePredicateがtrueを返すまで画面を戻る | ||
627 | +Get.until() | ||
628 | + | ||
629 | +// 引数で指定したRouteに進み、RoutePredicateがtrueを返すまで画面を戻る | ||
630 | +Get.offUntil() | ||
631 | + | ||
632 | +// 引数で指定した名前付きRouteに進み、RoutePredicateがtrueを返すまで画面を戻る | ||
633 | +Get.offNamedUntil() | ||
634 | + | ||
635 | +// アプリがどのプラットフォームで実行されているかのチェック | ||
636 | +GetPlatform.isAndroid | ||
637 | +GetPlatform.isIOS | ||
638 | +GetPlatform.isMacOS | ||
639 | +GetPlatform.isWindows | ||
640 | +GetPlatform.isLinux | ||
641 | +GetPlatform.isFuchsia | ||
642 | + | ||
643 | +// アプリがどのデバイスで実行されているかのチェック | ||
644 | +GetPlatform.isMobile | ||
645 | +GetPlatform.isDesktop | ||
646 | +// プラットフォームとデバイスのチェックは独立 | ||
647 | +// 同じOSでもウェブで実行されているのか、ネイティブで実行されているのか区別 | ||
648 | +GetPlatform.isWeb | ||
649 | + | ||
650 | + | ||
651 | +// MediaQuery.of(context).size.height と同じ | ||
652 | +// ただしimmutable | ||
653 | +Get.height | ||
654 | +Get.width | ||
655 | + | ||
656 | +// Navigatorの現在のcontextを取得 | ||
657 | +Get.context | ||
658 | + | ||
659 | +// SnackBar/Dialog/BottomSheet などフォアグラウンドのcontextを取得 | ||
660 | +Get.overlayContext | ||
661 | + | ||
662 | +// 注: 以降のメソッドはcontextの拡張メソッドです。 | ||
663 | +// contextと同じくUIのどこからでもアクセスできます。 | ||
664 | + | ||
665 | +// ウィンドウサイズの変更などに合わせて変わる height/width を取得 | ||
666 | +context.width | ||
667 | +context.height | ||
668 | + | ||
669 | +// 画面の半分のサイズ,1/3のサイズなどを取得 | ||
670 | +// レスポンシブなデザインの場合に便利 | ||
671 | +// オプションのパラメーター dividedBy で割る数を指定 | ||
672 | +// オプションのパラメーター reducedBy でパーセンテージを指定 | ||
673 | +context.heightTransformer() | ||
674 | +context.widthTransformer() | ||
675 | + | ||
676 | +/// MediaQuery.of(context).size とほぼ同じ | ||
677 | +context.mediaQuerySize() | ||
678 | + | ||
679 | +/// MediaQuery.of(context).padding とほぼ同じ | ||
680 | +context.mediaQueryPadding() | ||
681 | + | ||
682 | +/// MediaQuery.of(context).viewPadding とほぼ同じ | ||
683 | +context.mediaQueryViewPadding() | ||
684 | + | ||
685 | +/// MediaQuery.of(context).viewInsets とほぼ同じ | ||
686 | +context.mediaQueryViewInsets() | ||
687 | + | ||
688 | +/// MediaQuery.of(context).orientation とほぼ同じ | ||
689 | +context.orientation() | ||
690 | + | ||
691 | +/// デバイスがランドスケープ(横長)モードかどうかチェック | ||
692 | +context.isLandscape() | ||
693 | + | ||
694 | +/// デバイスがポートレート(縦長)モードかどうかチェック | ||
695 | +context.isPortrait() | ||
696 | + | ||
697 | +/// MediaQuery.of(context).devicePixelRatio とほぼ同じ | ||
698 | +context.devicePixelRatio() | ||
699 | + | ||
700 | +/// MediaQuery.of(context).textScaleFactor とほぼ同じ | ||
701 | +context.textScaleFactor() | ||
702 | + | ||
703 | +/// 画面の短辺の長さを取得 | ||
704 | +context.mediaQueryShortestSide() | ||
705 | + | ||
706 | +/// 画面の横幅が800より大きい場合にtrueを返す | ||
707 | +context.showNavbar() | ||
708 | + | ||
709 | +/// 画面の短辺が600より小さい場合にtrueを返す | ||
710 | +context.isPhone() | ||
711 | + | ||
712 | +/// 画面の短辺が600より小さい場合にtrueを返す | ||
713 | +context.isSmallTablet() | ||
714 | + | ||
715 | +/// 画面の短辺が720より大きい場合にtrueを返す | ||
716 | +context.isLargeTablet() | ||
717 | + | ||
718 | +/// デバイスがタブレットの場合にtrueを返す | ||
719 | +context.isTablet() | ||
720 | + | ||
721 | +/// 画面サイズに合わせて value<T> を返す | ||
722 | +/// たとえば: | ||
723 | +/// 短辺が300より小さい → watchパラメーターの値を返す | ||
724 | +/// 短辺が600より小さい → mobileパラメーターの値を返す | ||
725 | +/// 短辺が1200より小さい → tabletパラメーターの値を返す | ||
726 | +/// 横幅が1200より大きい → desktopパラメーターの値を返す | ||
727 | +context.responsiveValue<T>() | ||
728 | +``` | ||
729 | + | ||
730 | +### オプションのグローバル設定と手動設定 | ||
731 | + | ||
732 | +GetMaterialApp はすべてあなたの代わりに設定してくれますが、手動で設定を施したい場合は MaterialApp の navigatorKey と navigatorObservers の値を指定してください。 | ||
733 | + | ||
734 | +```dart | ||
735 | +MaterialApp( | ||
736 | + navigatorKey: Get.key, | ||
737 | + navigatorObservers: [GetObserver()], | ||
738 | +); | ||
739 | +``` | ||
740 | + | ||
741 | +`GetObserver`内で独自のミドルウェアを使用することもできます。これは他に影響を及ぼすことはありません。 | ||
742 | + | ||
743 | +```dart | ||
744 | +MaterialApp( | ||
745 | + navigatorKey: Get.key, | ||
746 | + navigatorObservers: [ | ||
747 | + GetObserver(MiddleWare.observer) // ここ | ||
748 | + ], | ||
749 | +); | ||
750 | +``` | ||
751 | + | ||
752 | +`Get` クラスに_グローバル設定_を施すことができます。Routeをプッシュする前のコードに `Get.config` を追加するだけです。もしくは、`GetMaterialApp` 内で直接設定することもできます。 | ||
753 | + | ||
754 | +```dart | ||
755 | +GetMaterialApp( | ||
756 | + enableLog: true, | ||
757 | + defaultTransition: Transition.fade, | ||
758 | + opaqueRoute: Get.isOpaqueRouteDefault, | ||
759 | + popGesture: Get.isPopGestureEnable, | ||
760 | + transitionDuration: Get.defaultDurationTransition, | ||
761 | + defaultGlobalState: Get.defaultGlobalState, | ||
762 | +); | ||
763 | + | ||
764 | +Get.config( | ||
765 | + enableLog = true, | ||
766 | + defaultPopGesture = true, | ||
767 | + defaultTransition = Transitions.cupertino | ||
768 | +) | ||
769 | +``` | ||
770 | + | ||
771 | +オプションで、すべてのログメッセージを `Get` からリダイレクトさせることができます。 | ||
772 | +お好みのロギングパッケージを使ってログを取得したい場合はこのようにしてください。 | ||
773 | + | ||
774 | +```dart | ||
775 | +GetMaterialApp( | ||
776 | + enableLog: true, | ||
777 | + logWriterCallback: localLogWriter, | ||
778 | +); | ||
779 | + | ||
780 | +void localLogWriter(String text, {bool isError = false}) { | ||
781 | + // ここでお好みのロギングパッケージにメッセージを渡してください | ||
782 | + // enableLog: false にしても、ログメッセージはこのコールバックでプッシュされる点ご注意を | ||
783 | + // ログが有効かどうかのチェックは Get.isLogEnable で可能 | ||
784 | +} | ||
785 | + | ||
786 | +``` | ||
787 | + | ||
788 | +### ローカルステートWidget | ||
789 | + | ||
790 | +ローカルステートWidgetは、1つの変数の状態を一時的かつローカルに管理したい場合に便利です。 | ||
791 | +シンプルなValueBuilderとリアクティブなObxValueの2種類があります。 | ||
792 | +たとえば、`TextField` Widgetの obscureText プロパティを切り替えたり、折りたたみ可能なパネルをカスタムで作成したり、`BottomNavigation` の現在のインデックス値を変更して内容を変更したりといった用途に最適です。 | ||
793 | + | ||
794 | +#### ValueBuilder | ||
795 | + | ||
796 | +setStateでお馴染みの `StatefulWidget` をシンプルにしたビルダーWidgetです。 | ||
797 | + | ||
798 | +```dart | ||
799 | +ValueBuilder<bool>( | ||
800 | + initialValue: false, | ||
801 | + builder: (value, updateFn) => Switch( | ||
802 | + value: value, | ||
803 | + onChanged: updateFn, // ( newValue ) => updateFn( newValue ) も可 | ||
804 | + ), | ||
805 | + // builderメソッドの外で何か実行する場合 | ||
806 | + onUpdate: (value) => print("Value updated: $value"), | ||
807 | + onDispose: () => print("Widget unmounted"), | ||
808 | +), | ||
809 | +``` | ||
810 | + | ||
811 | +#### ObxValue | ||
812 | + | ||
813 | +[`ValueBuilder`](#valuebuilder)に似ていますが、これはそのリアクティブバージョンです。Rxインスタンス(.obsを付けたときに戻る値です)を渡すと自動で更新されます。すごいでしょ? | ||
814 | + | ||
815 | +```dart | ||
816 | +ObxValue((data) => Switch( | ||
817 | + value: data.value, | ||
818 | + onChanged: data, | ||
819 | + // Rxには_呼び出し可能な_関数が備わっているのでこれだけでOK | ||
820 | + // (flag) => data.value = flag も可能 | ||
821 | + ), | ||
822 | + false.obs, | ||
823 | +), | ||
824 | +``` | ||
825 | + | ||
826 | +## お役立ちTIPS | ||
827 | + | ||
828 | +`.obs`が付いた型(_Rx_型とも呼ばれる)には、さまざまな内部メソッドや演算子が用意されています。 | ||
829 | + | ||
830 | +> `.obs`が付いたプロパティが **実際の値** だと信じてしまいがちですが...間違えないように! | ||
831 | +> 我々がcontrollerにおける変数の型宣言を省略してvarとしているのはDartのコンパイラが賢い上に、 | ||
832 | +> そのほうがコードがすっきる見えるからですが… | ||
833 | + | ||
834 | +```dart | ||
835 | +var message = 'Hello world'.obs; | ||
836 | +print( 'Message "$message" has Type ${message.runtimeType}'); | ||
837 | +``` | ||
838 | + | ||
839 | +`message`を _print_ することで実際の文字列が取り出されはしますが、型は **RxString** です! | ||
840 | + | ||
841 | +そのため `message.substring( 0, 4 )` などといったことはできません。 | ||
842 | +Stringのメソッドにアクセスするには _observable_ の中にある実際の値 `value` にアクセスします。 | ||
843 | +アクセスには `.value`を使うのが通常ですが、他の方法もあるのでご紹介します。 | ||
844 | + | ||
845 | +```dart | ||
846 | +final name = 'GetX'.obs; | ||
847 | +// 新しい値が現在のものと異なる場合のみ Stream が更新されます。 | ||
848 | +name.value = 'Hey'; | ||
849 | + | ||
850 | +// すべてのRxプロパティは「呼び出し可能」で、valueを返してくれます。 | ||
851 | +// ただし `null` は受付不可。nullの場合はUIが更新されない。 | ||
852 | +name('Hello'); | ||
853 | + | ||
854 | +// これはgetterみたいなものです。'Hello' を返します。 | ||
855 | +name() ; | ||
856 | + | ||
857 | +/// num型の場合 | ||
858 | + | ||
859 | +final count = 0.obs; | ||
860 | + | ||
861 | +// num型の非破壊的な演算子はすべて使えます。 | ||
862 | +count + 1; | ||
863 | + | ||
864 | +// 注意! この場合は`count`がfinalなら有効ではないです。varなら有効。 | ||
865 | +count += 1; | ||
866 | + | ||
867 | +// 比較演算子も使用可能 | ||
868 | +count > 2; | ||
869 | + | ||
870 | +/// bool型の場合 | ||
871 | + | ||
872 | +final flag = false.obs; | ||
873 | + | ||
874 | +// true/false を入れ替えることができます。 | ||
875 | +flag.toggle(); | ||
876 | + | ||
877 | + | ||
878 | +/// すべての型 | ||
879 | + | ||
880 | +// `value` を null にセット。 | ||
881 | +flag.nil(); | ||
882 | + | ||
883 | +// toString(), toJson() などの操作はすべて `value` が対象になります。 | ||
884 | +print( count ); // RxIntの `toString()` が呼び出されて数字がprintされる。 | ||
885 | + | ||
886 | +final abc = [0,1,2].obs; | ||
887 | +// json配列に変換した値と、'RxList' がprintされます。 | ||
888 | +// JsonはすべてのRx型でサポートされています! | ||
889 | +print('json: ${jsonEncode(abc)}, type: ${abc.runtimeType}'); | ||
890 | + | ||
891 | +// RxMap、RxList、RxSetはそれぞれの元の型を拡張した特別なRx型です。 | ||
892 | +// たとえばRxListは通常のListとして扱うことができる上にリアクティブです。 | ||
893 | +abc.add(12); // 12をListにプッシュし、Streamを更新してくれます。 | ||
894 | +abc[3]; // Listと同様にインデックス番号3の値を取得してくれます。 | ||
895 | + | ||
896 | + | ||
897 | +// 等価演算子はRx型と元の型でも動作しますが、.hashCode は常にvalueから取得します。 | ||
898 | +final number = 12.obs; | ||
899 | +print( number == 12 ); // true | ||
900 | + | ||
901 | +/// カスタムのRxモデル | ||
902 | + | ||
903 | +// toJson()やtoString()をモデルクラスに設定すれば、.obsからでもprintされるように実装可能。 | ||
904 | + | ||
905 | +class User { | ||
906 | + String name, last; | ||
907 | + int age; | ||
908 | + User({this.name, this.last, this.age}); | ||
909 | + | ||
910 | + @override | ||
911 | + String toString() => '$name $last, $age years old'; | ||
912 | +} | ||
913 | + | ||
914 | +final user = User(name: 'John', last: 'Doe', age: 33).obs; | ||
915 | + | ||
916 | +// `user` 自体はリアクティブですが、その中のプロパティはリアクティブではありません。 | ||
917 | +// そのため、このようにプロパティの値を変更してもWidgetは更新されません。 | ||
918 | +user.value.name = 'Roi'; | ||
919 | +// `Rx` には自ら変更を検知する手段がないからです。 | ||
920 | +// そのため、カスタムクラスの場合はこのようにWidgetに変更を知らせる必要があります。 | ||
921 | +user.refresh(); | ||
922 | + | ||
923 | +// もしくは `update()` メソッドを使用してください。 | ||
924 | +user.update((value){ | ||
925 | + value.name='Roi'; | ||
926 | +}); | ||
927 | + | ||
928 | +print( user ); | ||
929 | +``` | ||
930 | +#### StateMixin | ||
931 | + | ||
932 | +`UI`の状態を管理するもう一つの手法として、`StateMixin<T>`を利用する方法があります。 | ||
933 | +controllerクラスに`with`を使って`StateMixin<T>`を追加することで実装可能です。 | ||
934 | + | ||
935 | +``` dart | ||
936 | +class Controller extends GetController with StateMixin<User>{} | ||
937 | +``` | ||
938 | + | ||
939 | +`change()`メソッドにより好きなタイミングで状態を変更することができます。 | ||
940 | +このようにデータと状態を渡すだけです。 | ||
941 | + | ||
942 | +```dart | ||
943 | +change(data, status: RxStatus.success()); | ||
944 | +``` | ||
945 | + | ||
946 | +RxStatus には以下のステータスが存在します。 | ||
947 | + | ||
948 | +``` dart | ||
949 | +RxStatus.loading(); | ||
950 | +RxStatus.success(); | ||
951 | +RxStatus.empty(); | ||
952 | +RxStatus.error('message'); | ||
953 | +``` | ||
954 | + | ||
955 | +ステータスごとにUIを設定するにはこのようにします。 | ||
956 | + | ||
957 | +```dart | ||
958 | +class OtherClass extends GetView<Controller> { | ||
959 | + @override | ||
960 | + Widget build(BuildContext context) { | ||
961 | + return Scaffold( | ||
962 | + | ||
963 | + body: controller.obx( | ||
964 | + (state)=>Text(state.name), | ||
965 | + | ||
966 | + // ローディング中はカスタムのインジケーターの設定も可能ですが、 | ||
967 | + // デフォルトで Center(child:CircularProgressIndicator()) となります。 | ||
968 | + onLoading: CustomLoadingIndicator(), | ||
969 | + onEmpty: Text('No data found'), | ||
970 | + | ||
971 | + // 同様にエラーWidgetはカスタム可能ですが、 | ||
972 | + // デフォルトは Center(child:Text(error)) です。 | ||
973 | + onError: (error)=>Text(error), | ||
974 | + ), | ||
975 | + ); | ||
976 | +} | ||
977 | +``` | ||
978 | + | ||
979 | +#### GetView | ||
980 | + | ||
981 | +このWidgetは私のお気に入りです。とてもシンプルで扱いやすいですよ! | ||
982 | + | ||
983 | +このWidgetを一言で表現すると、「controllerをgetterに持つ `const` な StatelessWidget」です。 | ||
984 | + | ||
985 | +```dart | ||
986 | + class AwesomeController extends GetController { | ||
987 | + final String title = 'My Awesome View'; | ||
988 | + } | ||
989 | + | ||
990 | + // controllerの `型` を渡すのを忘れずに! | ||
991 | + class AwesomeView extends GetView<AwesomeController> { | ||
992 | + @override | ||
993 | + Widget build(BuildContext context) { | ||
994 | + return Container( | ||
995 | + padding: EdgeInsets.all(20), | ||
996 | + child: Text(controller.title), // `controller.なんとか` でアクセス | ||
997 | + ); | ||
998 | + } | ||
999 | + } | ||
1000 | +``` | ||
1001 | + | ||
1002 | +#### GetResponsiveView | ||
1003 | + | ||
1004 | +GetViewをレスポンシブデザインに対応させたい場合はこのWidgetを継承してください。 | ||
1005 | +画面サイズやデバイスタイプなどの情報を持つ `screen` プロパティを保持しています。 | ||
1006 | + | ||
1007 | +##### 使い方 | ||
1008 | + | ||
1009 | +Widgetをビルドする方法は2つあります。 | ||
1010 | + | ||
1011 | +- `builder` メソッドを使う。 | ||
1012 | +- `desktop`, `tablet`, `phone`, `watch` メソッドを使う。 | ||
1013 | + 画面サイズ、デバイスタイプに応じたWidgetがビルドされます。 | ||
1014 | + たとえば画面が [ScreenType.Tablet] なら `tablet` メソッドが実行されます。 | ||
1015 | + **注:** `alwaysUseBuilder` プロパティをfalseにする必要があります。 | ||
1016 | + | ||
1017 | +`settings` プロパティでブレイクポイントを設定することもできます。 | ||
1018 | + | ||
1019 | + | ||
1020 | +この画面のコード | ||
1021 | +[コード](https://github.com/SchabanBo/get_page_example/blob/master/lib/pages/responsive_example/responsive_view.dart) | ||
1022 | + | ||
1023 | +#### GetWidget | ||
1024 | + | ||
1025 | +このWidgetはあまり知られておらず、使用するケースは稀です。 | ||
1026 | +GetViewとの違いは、Controllerを`キャッシュ`してくれる点です。 | ||
1027 | +このキャッシュがあるため `const` にはできません。 | ||
1028 | + | ||
1029 | +> それでは一体いつControllerをキャッシュする必要があるのかって? | ||
1030 | + | ||
1031 | +それは **GetX** のこれまた使う機会の少ない `Get.create()` を使うときです。 | ||
1032 | + | ||
1033 | +`Get.create(()=>Controller())` は `Get.find<Controller>()` を実行するたびに | ||
1034 | +新しいControllerインスタンスを生成します。 | ||
1035 | + | ||
1036 | +そこで `GetWidget` の出番です。たとえば、Todoアイテムのリスト内容を保持したいとき。 | ||
1037 | +Widgetが更新されてもアイテムはControllerのキャッシュを参照してくれます。 | ||
1038 | + | ||
1039 | +#### GetxService | ||
1040 | + | ||
1041 | +このクラスは `GetxController` に似ており、同様のライフサイクル(`onInit()`, `onReady()`, `onClose()`)を共有しますが、そこに「ロジック」はありません。**GetX**の依存オブジェクト注入システムに、このサブクラスがメモリから **削除できない** ということを知らせるだけです。 | ||
1042 | + | ||
1043 | +そのため `Get.find()` で `ApiService`, `StorageService`, `CacheService` のようなサービス系クラスにいつでもアクセスできるようにしておくと非常に便利です。 | ||
1044 | + | ||
1045 | +```dart | ||
1046 | +Future<void> main() async { | ||
1047 | + await initServices(); /// サービスクラスの初期化をawait | ||
1048 | + runApp(SomeApp()); | ||
1049 | +} | ||
1050 | + | ||
1051 | +/// Flutterアプリ実行前にサービスクラスを初期化してフローをコントロールするのは賢いやり方です。 | ||
1052 | +/// たとえば GetMaterialAppを更新する必要がないようにUser別の | ||
1053 | +/// Theme、apiKey、言語設定などをApiサービス実行前にロードしたり。 | ||
1054 | +void initServices() async { | ||
1055 | + print('starting services ...'); | ||
1056 | + /// get_storage, hive, shared_pref の初期化はここで行います。 | ||
1057 | + /// あるいは moor の connection など非同期のメソッドならなんでも。 | ||
1058 | + await Get.putAsync(() => DbService().init()); | ||
1059 | + await Get.putAsync(SettingsService()).init(); | ||
1060 | + print('All services started...'); | ||
1061 | +} | ||
1062 | + | ||
1063 | +class DbService extends GetxService { | ||
1064 | + Future<DbService> init() async { | ||
1065 | + print('$runtimeType delays 2 sec'); | ||
1066 | + await 2.delay(); | ||
1067 | + print('$runtimeType ready!'); | ||
1068 | + return this; | ||
1069 | + } | ||
1070 | +} | ||
1071 | + | ||
1072 | +class SettingsService extends GetxService { | ||
1073 | + void init() async { | ||
1074 | + print('$runtimeType delays 1 sec'); | ||
1075 | + await 1.delay(); | ||
1076 | + print('$runtimeType ready!'); | ||
1077 | + } | ||
1078 | +} | ||
1079 | + | ||
1080 | +``` | ||
1081 | + | ||
1082 | +`GetxService` を破棄する唯一の方法は `Get.reset()` メソッドを使うことです。 | ||
1083 | +これはアプリにおける「ホットリブート」のようなものです。あるクラスのインスタンスを | ||
1084 | +ライフサイクルの間ずっと残しておきたい場合は `GetxService` を使うというのを覚えておいてください。 | ||
1085 | + | ||
1086 | + | ||
1087 | +## テストの実行 | ||
1088 | + | ||
1089 | +Controllerのライフサイクル含め、他のクラスと同様にテストを実行することができます。 | ||
1090 | + | ||
1091 | +```dart | ||
1092 | +class Controller extends GetxController { | ||
1093 | + @override | ||
1094 | + void onInit() { | ||
1095 | + super.onInit(); | ||
1096 | + // 値を name2 に変更 | ||
1097 | + name.value = 'name2'; | ||
1098 | + } | ||
1099 | + | ||
1100 | + @override | ||
1101 | + void onClose() { | ||
1102 | + name.value = ''; | ||
1103 | + super.onClose(); | ||
1104 | + } | ||
1105 | + | ||
1106 | + final name = 'name1'.obs; | ||
1107 | + | ||
1108 | + void changeName() => name.value = 'name3'; | ||
1109 | +} | ||
1110 | + | ||
1111 | +void main() { | ||
1112 | + test(''' | ||
1113 | +Test the state of the reactive variable "name" across all of its lifecycles''', | ||
1114 | + () { | ||
1115 | + /// ライフサイクルごとのテストは必ずしも行う必要はありませんが、 | ||
1116 | + /// GetXの依存オブジェクト注入機能を使用しているのであれば実行をおすすめします。 | ||
1117 | + final controller = Controller(); | ||
1118 | + expect(controller.name.value, 'name1'); | ||
1119 | + | ||
1120 | + /// このようにライフサイクル経過ごとの状態をテスト可能です。 | ||
1121 | + Get.put(controller); // onInit が実行される | ||
1122 | + expect(controller.name.value, 'name2'); | ||
1123 | + | ||
1124 | + /// 関数もテストしましょう | ||
1125 | + controller.changeName(); | ||
1126 | + expect(controller.name.value, 'name3'); | ||
1127 | + | ||
1128 | + /// onClose が実行される | ||
1129 | + Get.delete<Controller>(); | ||
1130 | + | ||
1131 | + expect(controller.name.value, ''); | ||
1132 | + }); | ||
1133 | +} | ||
1134 | +``` | ||
1135 | + | ||
1136 | +#### mockitoやmocktailを使う場合 | ||
1137 | +GetxController/GetxService をモックする場合 Mock をミックスインしてください。 | ||
1138 | + | ||
1139 | +```dart | ||
1140 | +class NotificationServiceMock extends GetxService with Mock implements NotificationService {} | ||
1141 | +``` | ||
1142 | + | ||
1143 | +#### Get.reset() | ||
1144 | +WidgetやGroupのテスト時に、テストの最後かtearDownの中で Get.reset() を実行することで設定をリセットすることができます。 | ||
1145 | + | ||
1146 | +#### Get.testMode | ||
1147 | +Controllerの中でナビゲーションを使用している場合は、`Get.testMode = true`をmainの開始で実行してください。 | ||
1148 | + | ||
1149 | + | ||
1150 | +# バージョン2.0からの破壊的変更 | ||
1151 | + | ||
1152 | +1- Rx型の名称 | ||
1153 | + | ||
1154 | +| 変更前 | 変更後 | | ||
1155 | +| ------- | ---------- | | ||
1156 | +| StringX | `RxString` | | ||
1157 | +| IntX | `RxInt` | | ||
1158 | +| MapX | `RxMap` | | ||
1159 | +| ListX | `RxList` | | ||
1160 | +| NumX | `RxNum` | | ||
1161 | +| DoubleX | `RxDouble` | | ||
1162 | + | ||
1163 | +RxControllerとGetBuilderが統合され、Controllerにどれを使うか覚えておく必要がなくなりました。GetxControllerを使うだけで、リアクティブと非リアクティブな状態管理の両方に対応できるようになりました。 | ||
1164 | + | ||
1165 | +2- 名前付きRoute | ||
1166 | +変更前: | ||
1167 | + | ||
1168 | +```dart | ||
1169 | +GetMaterialApp( | ||
1170 | + namedRoutes: { | ||
1171 | + '/': GetRoute(page: Home()), | ||
1172 | + } | ||
1173 | +) | ||
1174 | +``` | ||
1175 | + | ||
1176 | +変更後: | ||
1177 | + | ||
1178 | +```dart | ||
1179 | +GetMaterialApp( | ||
1180 | + getPages: [ | ||
1181 | + GetPage(name: '/', page: () => Home()), | ||
1182 | + ] | ||
1183 | +) | ||
1184 | +``` | ||
1185 | + | ||
1186 | +変更の効果: | ||
1187 | +ページ表示にはパラメータやログイントークンを起点にする方法もありますが、以前のアプローチではこれができず、柔軟性に欠けていました。 | ||
1188 | +ページを関数から取得するよう変更したことで、このようなアプローチを可能にし、アプリ起動直後にRouteがメモリに割り当てられることもないため、RAMの消費量を大幅に削減することもできました。 | ||
1189 | + | ||
1190 | +```dart | ||
1191 | + | ||
1192 | +GetStorage box = GetStorage(); | ||
1193 | + | ||
1194 | +GetMaterialApp( | ||
1195 | + getPages: [ | ||
1196 | + GetPage(name: '/', page:(){ | ||
1197 | + return box.hasData('token') ? Home() : Login(); | ||
1198 | + }) | ||
1199 | + ] | ||
1200 | +) | ||
1201 | +``` | ||
1202 | + | ||
1203 | +# なぜGetXなのか | ||
1204 | + | ||
1205 | +1- Flutterのアップデートが重なると、依存パッケージがうまく動かなくなることがあります。コンパイルエラーを起こしたり、その時点で解決方法がないようなエラーが発生したり。開発者はそのエラーを追跡し、該当リポジトリにissueを提起し、問題が解決されるのを待つ必要があります。Getは開発に必要な主要リソース(状態管理、依存オブジェクト管理、Route管理)を一元化し、Pubspecにパッケージを1つ追加するだけでコーディングを開始することができます。Flutterがアップデートしたときに必要なことは、Getも併せてアップデートすることだけです。それですぐに作業を再開できます。またGetはパッケージ間の互換性の問題も解消します。互いに依存するパッケージAの最新バージョンとBの最新バージョンの間に互換性がない、ということが何度あったでしょうか。Getを使えばすべてが同じパッケージ内にあるため、互換性の心配はありません。 | ||
1206 | + | ||
1207 | +2- Flutterは手軽で素晴らしいフレームワークですが、`Navigator.of(context).push (context, builder [...]`のように、ほとんどの開発者にとって不要な定型文が一部にあります。Getを使えばそのような定型文を簡素化できます。Routeを呼ぶためだけに8行のコードを書く代わりに、`Get.to(Home())`を実行すれば、次の画面に遷移することができるのです。またウェブURLを動的なものにすることは現在Flutterでは本当に骨の折れる作業ですが、GetXを使えば非常に簡単です。そしてFlutterにおける状態管理と依存オブジェクト管理については、たくさんのパターンやパッケージがあるので多くの議論を生んでいます。しかしGetXのアプローチは大変シンプルです。これは一例ですが、変数の最後に「.obs」を追加して「Obx()」の中にWidgetを配置するだけで、その変数の状態変化が自動でWidgetに反映されます。 | ||
1208 | + | ||
1209 | +3- GetXではパフォーマンスのことをあまり気にせず開発ができます。Flutterのパフォーマンスはそれだけで素晴らしいものですが、状態管理と併せて BLoC / データストア / Controller などを届けるためのサービスロケーターを使用することを想像してみてください。そのインスタンスが必要ないときはリソースを解放するイベントを明示的に呼び出さなければなりません。そんなとき、使用されなくなったら自動でメモリから削除してくれればいいのに、と考えたことはありませんか?それを実現してくれるのがGetXです。SmartManagement機能により未使用のリソースはすべて自動でメモリから破棄されるので、本来の開発作業に集中することができます。メモリ管理のためのロジックを作らなくても、常に必要最小限のリソースを使っていることが保証されるのです。 | ||
1210 | + | ||
1211 | +4- コードのデカップリング(分離)がしやすい。「ビューをビジネスロジックから分離する」というコンセプトを聞いたことがあるかもしれません。これはなにもBLoC、MVC、MVVMに限ったことではなく、どのアーキテクチャパターンもこのコンセプトが考え方の基本にあると言っていいでしょう。しかし、Flutterではcontextの使用によりこのコンセプトが薄まってしまいがちです。 | ||
1212 | +InheritedWidgetを参照するためにcontextが必要なとき、ビューの中でそれを使用するか、引数としてcontextを渡しますよね?私はこの方法は美しくないと感じます。常にビュー内のビジネスロジックに依存しなければならないのは、特にチームで仕事をする場面においては不便だと感じます。GetXによるアプローチでは、StatefulWidgetやinitStateなどの使用を禁止しているわけではありませんが、それらよりもずっとスッキリ書けるようになっています。Controller自体にライフサイクルがあるため、たとえばREST APIのリクエストを行うときも、ビューの中の何かに依存するということがありません。Controllerのライフサイクルの一つである onInit を使用してhttpを呼び出し、データが到着すると変数にセットされます。GetXはリアクティブな変数を扱うことができるので、インスタンス変数が変わりし次第、その変数に依存するWidgetがすべて自動更新されます。これによりUIの担当者はWidgetの見た目に注力することができ、ボタンクリックなどのユーザーイベント以外のものをビジネスロジックに渡す必要がなくなります。その一方でビジネスロジックの担当者はビジネスロジックだけに集中し、個別のテストを簡単に行うことができます。 | ||
1213 | + | ||
1214 | +GetXライブラリは今後も更新され続け、新しい機能を実装していきます。気軽にプルリクエストを出していただき、ライブラリの成長に貢献していただけるとうれしいです。 | ||
1215 | + | ||
1216 | +# コミュニティ | ||
1217 | + | ||
1218 | +## コミュニティチャンネル | ||
1219 | + | ||
1220 | +GetXコミュニティは非常に活発で有益な情報であふれています。ご質問がある場合や、このフレームワークの使用に関して支援が必要な場合は、ぜひコミュニティチャンネルにご参加ください。このリポジトリは、issueの提起およびリクエスト専用ですが、気軽にコミュニティにご参加いただければ幸いです。 | ||
1221 | + | ||
1222 | +| **Slack** | **Discord** | **Telegram** | | ||
1223 | +| :-------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------- | | ||
1224 | +| [](https://communityinviter.com/apps/getxworkspace/getx) | [](https://discord.com/invite/9Hpt99N) | [](https://t.me/joinchat/PhdbJRmsZNpAqSLJL6bH7g) | | ||
1225 | + | ||
1226 | +## コントリビュート方法 | ||
1227 | + | ||
1228 | +_GetXプロジェクトに貢献してみませんか?あなたをコントリビューターの一人としてご紹介できるのを楽しみにしています。GetおよびFlutterをより良いものにするためのコントリビュート例をご紹介します。_ | ||
1229 | + | ||
1230 | +- Readmeの多言語対応。 | ||
1231 | +- Readmeの追加ドキュメント執筆 (ドキュメントで触れられていない機能がまだまだたくさんあります)。 | ||
1232 | +- Getの使い方を紹介する記事やビデオの作成(Readmeに掲載させていただきます。将来的にWikiができればそこにも掲載予定)。 | ||
1233 | +- コードやテストのプルリクエスト。 | ||
1234 | +- 新機能の提案。 | ||
1235 | + | ||
1236 | +どのような形の貢献であれ歓迎しますので、ぜひコミュニティにご参加ください! | ||
1237 | + | ||
1238 | +## GetXに関する記事と動画 | ||
1239 | + | ||
1240 | +- [Flutter Getx EcoSystem package for arabic people](https://www.youtube.com/playlist?list=PLV1fXIAyjeuZ6M8m56zajMUwu4uE3-SL0) - Tutorial by [Pesa Coder](https://github.com/UsamaElgendy). | ||
1241 | +- [Dynamic Themes in 3 lines using GetX™](https://medium.com/swlh/flutter-dynamic-themes-in-3-lines-c3b375f292e3) - Tutorial by [Rod Brown](https://github.com/RodBr). | ||
1242 | +- [Complete GetX™ Navigation](https://www.youtube.com/watch?v=RaqPIoJSTtI) - Route management video by Amateur Coder. | ||
1243 | +- [Complete GetX State Management](https://www.youtube.com/watch?v=CNpXbeI_slw) - State management video by Amateur Coder. | ||
1244 | +- [GetX™ Other Features](https://youtu.be/ttQtlX_Q0eU) - Utils, storage, bindings and other features video by Amateur Coder. | ||
1245 | +- [Firestore User with GetX | Todo App](https://www.youtube.com/watch?v=BiV0DcXgk58) - Video by Amateur Coder. | ||
1246 | +- [Firebase Auth with GetX | Todo App](https://www.youtube.com/watch?v=-H-T_BSgfOE) - Video by Amateur Coder. | ||
1247 | +- [The Flutter GetX™ Ecosystem ~ State Management](https://medium.com/flutter-community/the-flutter-getx-ecosystem-state-management-881c7235511d) - State management by [Aachman Garg](https://github.com/imaachman). | ||
1248 | +- [The Flutter GetX™ Ecosystem ~ Dependency Injection](https://medium.com/flutter-community/the-flutter-getx-ecosystem-dependency-injection-8e763d0ec6b9) - Dependency Injection by [Aachman Garg](https://github.com/imaachman). | ||
1249 | +- [GetX, the all-in-one Flutter package](https://www.youtube.com/watch?v=IYQgtu9TM74) - A brief tutorial covering State Management and Navigation by Thad Carnevalli. | ||
1250 | +- [Build a To-do List App from scratch using Flutter and GetX](https://www.youtube.com/watch?v=EcnqFasHf18) - UI + State Management + Storage video by Thad Carnevalli. | ||
1251 | +- [GetX Flutter Firebase Auth Example](https://medium.com/@jeffmcmorris/getx-flutter-firebase-auth-example-b383c1dd1de2) - Article by Jeff McMorris. | ||
1252 | +- [Flutter State Management with GetX – Complete App](https://www.appwithflutter.com/flutter-state-management-with-getx/) - by App With Flutter. | ||
1253 | +- [Flutter Routing with Animation using Get Package](https://www.appwithflutter.com/flutter-routing-using-get-package/) - by App With Flutter. | ||
1254 | +- [A minimal example on dartpad](https://dartpad.dev/2b3d0d6f9d4e312c5fdbefc414c1727e?) - by [Roi Peker](https://github.com/roipeker) |
@@ -31,6 +31,8 @@ | @@ -31,6 +31,8 @@ | ||
31 | [](README.pl.md) | 31 | [](README.pl.md) |
32 | [](README.ko-kr.md) | 32 | [](README.ko-kr.md) |
33 | [](README-fr.md) | 33 | [](README-fr.md) |
34 | +[](README.ja-JP.md) | ||
35 | + | ||
34 | 36 | ||
35 | </div> | 37 | </div> |
36 | 38 |
documentation/ar_EG/dependency_management.md
0 → 100644
1 | +# Dependency Management | ||
2 | +- [Dependency Management](#dependency-management) | ||
3 | + - [Instancing methods](#instancing-methods) | ||
4 | + - [Get.put()](#getput) | ||
5 | + - [Get.lazyPut](#getlazyput) | ||
6 | + - [Get.putAsync](#getputasync) | ||
7 | + - [Get.create](#getcreate) | ||
8 | + - [Using instantiated methods/classes](#using-instantiated-methodsclasses) | ||
9 | + - [Specifying an alternate instance](#specifying-an-alternate-instance) | ||
10 | + - [Differences between methods](#differences-between-methods) | ||
11 | + - [Bindings](#bindings) | ||
12 | + - [Bindings class](#bindings-class) | ||
13 | + - [BindingsBuilder](#bindingsbuilder) | ||
14 | + - [SmartManagement](#smartmanagement) | ||
15 | + - [How to change](#how-to-change) | ||
16 | + - [SmartManagement.full](#smartmanagementfull) | ||
17 | + - [SmartManagement.onlyBuilders](#smartmanagementonlybuilders) | ||
18 | + - [SmartManagement.keepFactory](#smartmanagementkeepfactory) | ||
19 | + - [How bindings work under the hood](#how-bindings-work-under-the-hood) | ||
20 | + - [Notes](#notes) | ||
21 | + | ||
22 | +Get has a simple and powerful dependency manager that allows you to retrieve the same class as your Bloc or Controller with just 1 lines of code, no Provider context, no inheritedWidget: | ||
23 | + | ||
24 | +```dart | ||
25 | +Controller controller = Get.put(Controller()); // Rather Controller controller = Controller(); | ||
26 | +``` | ||
27 | + | ||
28 | +Instead of instantiating your class within the class you are using, you are instantiating it within the Get instance, which will make it available throughout your App. | ||
29 | +So you can use your controller (or Bloc class) normally | ||
30 | + | ||
31 | +- Note: If you are using Get's State Manager, pay more attention to the [Bindings](#bindings) api, which will make easier to connect your view to your controller. | ||
32 | +- Note²: Get dependency management is decloupled from other parts of the package, so if for example your app is already using a state manager (any one, it doesn't matter), you don't need to change that, you can use this dependency injection manager with no problems at all | ||
33 | + | ||
34 | +## Instancing methods | ||
35 | +The methods and it's configurable parameters are: | ||
36 | + | ||
37 | +### Get.put() | ||
38 | + | ||
39 | +The most common way of inserting a dependency. Good for the controllers of your views for example. | ||
40 | + | ||
41 | +```dart | ||
42 | +Get.put<SomeClass>(SomeClass()); | ||
43 | +Get.put<LoginController>(LoginController(), permanent: true); | ||
44 | +Get.put<ListItemController>(ListItemController, tag: "some unique string"); | ||
45 | +``` | ||
46 | + | ||
47 | +This is all options you can set when using put: | ||
48 | +```dart | ||
49 | +Get.put<S>( | ||
50 | + // mandatory: the class that you want to get to save, like a controller or anything | ||
51 | + // note: "S" means that it can be a class of any type | ||
52 | + S dependency | ||
53 | + | ||
54 | + // optional: this is for when you want multiple classess that are of the same type | ||
55 | + // since you normally get a class by using Get.find<Controller>(), | ||
56 | + // you need to use tag to tell which instance you need | ||
57 | + // must be unique string | ||
58 | + String tag, | ||
59 | + | ||
60 | + // optional: by default, get will dispose instances after they are not used anymore (example, | ||
61 | + // the controller of a view that is closed), but you might need that the instance | ||
62 | + // to be kept there throughout the entire app, like an instance of sharedPreferences or something | ||
63 | + // so you use this | ||
64 | + // defaults to false | ||
65 | + bool permanent = false, | ||
66 | + | ||
67 | + // optional: allows you after using an abstract class in a test, replace it with another one and follow the test. | ||
68 | + // defaults to false | ||
69 | + bool overrideAbstract = false, | ||
70 | + | ||
71 | + // optional: allows you to create the dependency using function instead of the dependency itself. | ||
72 | + // this one is not commonly used | ||
73 | + InstanceBuilderCallback<S> builder, | ||
74 | +) | ||
75 | +``` | ||
76 | + | ||
77 | +### Get.lazyPut | ||
78 | +It is possible to lazyLoad a dependency so that it will be instantiated only when is used. Very useful for computational expensive classes or if you want to instantiate several classes in just one place (like in a Bindings class) and you know you will not gonna use that class at that time. | ||
79 | + | ||
80 | +```dart | ||
81 | +/// ApiMock will only be called when someone uses Get.find<ApiMock> for the first time | ||
82 | +Get.lazyPut<ApiMock>(() => ApiMock()); | ||
83 | + | ||
84 | +Get.lazyPut<FirebaseAuth>( | ||
85 | + () { | ||
86 | + // ... some logic if needed | ||
87 | + return FirebaseAuth(); | ||
88 | + }, | ||
89 | + tag: Math.random().toString(), | ||
90 | + fenix: true | ||
91 | +) | ||
92 | + | ||
93 | +Get.lazyPut<Controller>( () => Controller() ) | ||
94 | +``` | ||
95 | + | ||
96 | +This is all options you can set when using lazyPut: | ||
97 | +```dart | ||
98 | +Get.lazyPut<S>( | ||
99 | + // mandatory: a method that will be executed when your class is called for the first time | ||
100 | + InstanceBuilderCallback builder, | ||
101 | + | ||
102 | + // optional: same as Get.put(), it is used for when you want multiple different instance of a same class | ||
103 | + // must be unique | ||
104 | + String tag, | ||
105 | + | ||
106 | + // optional: It is similar to "permanent", the difference is that the instance is discarded when | ||
107 | + // is not being used, but when it's use is needed again, Get will recreate the instance | ||
108 | + // just the same as "SmartManagement.keepFactory" in the bindings api | ||
109 | + // defaults to false | ||
110 | + bool fenix = false | ||
111 | + | ||
112 | +) | ||
113 | +``` | ||
114 | + | ||
115 | +### Get.putAsync | ||
116 | +If you want to register an asynchronous instance, you can use `Get.putAsync`: | ||
117 | + | ||
118 | +```dart | ||
119 | +Get.putAsync<SharedPreferences>(() async { | ||
120 | + final prefs = await SharedPreferences.getInstance(); | ||
121 | + await prefs.setInt('counter', 12345); | ||
122 | + return prefs; | ||
123 | +}); | ||
124 | + | ||
125 | +Get.putAsync<YourAsyncClass>( () async => await YourAsyncClass() ) | ||
126 | +``` | ||
127 | + | ||
128 | +This is all options you can set when using putAsync: | ||
129 | +```dart | ||
130 | +Get.putAsync<S>( | ||
131 | + | ||
132 | + // mandatory: an async method that will be executed to instantiate your class | ||
133 | + AsyncInstanceBuilderCallback<S> builder, | ||
134 | + | ||
135 | + // optional: same as Get.put(), it is used for when you want multiple different instance of a same class | ||
136 | + // must be unique | ||
137 | + String tag, | ||
138 | + | ||
139 | + // optional: same as in Get.put(), used when you need to maintain that instance alive in the entire app | ||
140 | + // defaults to false | ||
141 | + bool permanent = false | ||
142 | +) | ||
143 | +``` | ||
144 | + | ||
145 | +### Get.create | ||
146 | + | ||
147 | +This one is tricky. A detailed explanation of what this is and the differences between the other one can be found on [Differences between methods:](#differences-between-methods) section | ||
148 | + | ||
149 | +```dart | ||
150 | +Get.Create<SomeClass>(() => SomeClass()); | ||
151 | +Get.Create<LoginController>(() => LoginController()); | ||
152 | +``` | ||
153 | + | ||
154 | +This is all options you can set when using create: | ||
155 | + | ||
156 | +```dart | ||
157 | +Get.create<S>( | ||
158 | + // required: a function that returns a class that will be "fabricated" every | ||
159 | + // time `Get.find()` is called | ||
160 | + // Example: Get.create<YourClass>(() => YourClass()) | ||
161 | + FcBuilderFunc<S> builder, | ||
162 | + | ||
163 | + // optional: just like Get.put(), but it is used when you need multiple instances | ||
164 | + // of a of a same class | ||
165 | + // Useful in case you have a list that each item need it's own controller | ||
166 | + // needs to be a unique string. Just change from tag to name | ||
167 | + String name, | ||
168 | + | ||
169 | + // optional: just like int`Get.put()`, it is for when you need to keep the | ||
170 | + // instance alive thoughout the entire app. The difference is in Get.create | ||
171 | + // permanent is true by default | ||
172 | + bool permanent = true | ||
173 | +``` | ||
174 | + | ||
175 | +## Using instantiated methods/classes | ||
176 | + | ||
177 | +Imagine that you have navigated through numerous routes, and you need a data that was left behind in your controller, you would need a state manager combined with the Provider or Get_it, correct? Not with Get. You just need to ask Get to "find" for your controller, you don't need any additional dependencies: | ||
178 | + | ||
179 | +```dart | ||
180 | +final controller = Get.find<Controller>(); | ||
181 | +// OR | ||
182 | +Controller controller = Get.find(); | ||
183 | + | ||
184 | +// Yes, it looks like Magic, Get will find your controller, and will deliver it to you. | ||
185 | +// You can have 1 million controllers instantiated, Get will always give you the right controller. | ||
186 | +``` | ||
187 | + | ||
188 | +And then you will be able to recover your controller data that was obtained back there: | ||
189 | + | ||
190 | +```dart | ||
191 | +Text(controller.textFromApi); | ||
192 | +``` | ||
193 | + | ||
194 | +Since the returned value is a normal class, you can do anything you want: | ||
195 | +```dart | ||
196 | +int count = Get.find<SharedPreferences>().getInt('counter'); | ||
197 | +print(count); // out: 12345 | ||
198 | +``` | ||
199 | + | ||
200 | +To remove an instance of Get: | ||
201 | + | ||
202 | +```dart | ||
203 | +Get.delete<Controller>(); //usually you don't need to do this because GetX already delete unused controllers | ||
204 | +``` | ||
205 | + | ||
206 | +## Specifying an alternate instance | ||
207 | + | ||
208 | +A currently inserted instance can be replaced with a similar or extended class instance by using the `replace` or `lazyReplace` method. This can then be retrieved by using the original class. | ||
209 | + | ||
210 | +```dart | ||
211 | +abstract class BaseClass {} | ||
212 | +class ParentClass extends BaseClass {} | ||
213 | + | ||
214 | +class ChildClass extends ParentClass { | ||
215 | + bool isChild = true; | ||
216 | +} | ||
217 | + | ||
218 | + | ||
219 | +Get.put<BaseClass>(ParentClass()); | ||
220 | + | ||
221 | +Get.replace<BaseClass>(ChildClass()); | ||
222 | + | ||
223 | +final instance = Get.find<BaseClass>(); | ||
224 | +print(instance is ChildClass); //true | ||
225 | + | ||
226 | + | ||
227 | +class OtherClass extends BaseClass {} | ||
228 | +Get.lazyReplace<BaseClass>(() => OtherClass()); | ||
229 | + | ||
230 | +final instance = Get.find<BaseClass>(); | ||
231 | +print(instance is ChildClass); // false | ||
232 | +print(instance is OtherClass); //true | ||
233 | +``` | ||
234 | + | ||
235 | +## Differences between methods | ||
236 | + | ||
237 | +First, let's of the `fenix` of Get.lazyPut and the `permanent` of the other methods. | ||
238 | + | ||
239 | +The fundamental difference between `permanent` and `fenix` is how you want to store your instances. | ||
240 | + | ||
241 | +Reinforcing: by default, GetX deletes instances when they are not in use. | ||
242 | +It means that: If screen 1 has controller 1 and screen 2 has controller 2 and you remove the first route from stack, (like if you use `Get.off()` or `Get.offNamed()`) the controller 1 lost its use so it will be erased. | ||
243 | + | ||
244 | +But if you want to opt for using `permanent:true`, then the controller will not be lost in this transition - which is very useful for services that you want to keep alive throughout the entire application. | ||
245 | + | ||
246 | +`fenix` in the other hand is for services that you don't worry in losing between screen changes, but when you need that service, you expect that it is alive. So basically, it will dispose the unused controller/service/class, but when you need it, it will "recreate from the ashes" a new instance. | ||
247 | + | ||
248 | +Proceeding with the differences between methods: | ||
249 | + | ||
250 | +- Get.put and Get.putAsync follows the same creation order, with the difference that the second uses an asynchronous method: those two methods creates and initializes the instance. That one is inserted directly in the memory, using the internal method `insert` with the parameters `permanent: false` and `isSingleton: true` (this isSingleton parameter only purpose is to tell if it is to use the dependency on `dependency` or if it is to use the dependency on `FcBuilderFunc`). After that, `Get.find()` is called that immediately initialize the instances that are on memory. | ||
251 | + | ||
252 | +- Get.create: As the name implies, it will "create" your dependency! Similar to `Get.put()`, it also calls the internal method `insert` to instancing. But `permanent` became true and `isSingleton` became false (since we are "creating" our dependency, there is no way for it to be a singleton instace, that's why is false). And because it has `permanent: true`, we have by default the benefit of not losing it between screens! Also, `Get.find()` is not called immediately, it wait to be used in the screen to be called. It is created this way to make use of the parameter `permanent`, since then, worth noticing, `Get.create()` was made with the goal of create not shared instances, but don't get disposed, like for example a button in a listView, that you want a unique instance for that list - because of that, Get.create must be used together with GetWidget. | ||
253 | + | ||
254 | +- Get.lazyPut: As the name implies, it is a lazy proccess. The instance is create, but it is not called to be used immediately, it remains waiting to be called. Contrary to the other methods, `insert` is not called here. Instead, the instance is inserted in another part of the memory, a part responsible to tell if the instance can be recreated or not, let's call it "factory". If we want to create something to be used later, it will not be mix with things been used right now. And here is where `fenix` magic enters: if you opt to leaving `fenix: false`, and your `smartManagement` are not `keepFactory`, then when using `Get.find` the instance will change the place in the memory from the "factory" to common instance memory area. Right after that, by default it is removed from the "factory". Now, if you opt for `fenix: true`, the instance continues to exist in this dedicated part, even going to the common area, to be called again in the future. | ||
255 | + | ||
256 | +## Bindings | ||
257 | + | ||
258 | +One of the great differentials of this package, perhaps, is the possibility of full integration of the routes, state manager and dependency manager. | ||
259 | +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. | ||
260 | +In version 2.10 Get completely implemented the Bindings API. | ||
261 | +Now you no longer need to use the init method. You don't even have to type your controllers if you don't want to. You can start your controllers and services in the appropriate place for that. | ||
262 | +The Binding class is a class that will decouple dependency injection, while "binding" routes to the state manager and dependency manager. | ||
263 | +This allows Get to know which screen is being displayed when a particular controller is used and to know where and how to dispose of it. | ||
264 | +In addition, the Binding class will allow you to have SmartManager configuration control. You can configure the dependencies to be arranged when removing a route from the stack, or when the widget that used it is laid out, or neither. You will have intelligent dependency management working for you, but even so, you can configure it as you wish. | ||
265 | + | ||
266 | +### Bindings class | ||
267 | + | ||
268 | +- Create a class and implements Binding | ||
269 | + | ||
270 | +```dart | ||
271 | +class HomeBinding implements Bindings {} | ||
272 | +``` | ||
273 | + | ||
274 | +Your IDE will automatically ask you to override the "dependencies" method, and you just need to click on the lamp, override the method, and insert all the classes you are going to use on that route: | ||
275 | + | ||
276 | +```dart | ||
277 | +class HomeBinding implements Bindings { | ||
278 | + @override | ||
279 | + void dependencies() { | ||
280 | + Get.lazyPut<HomeController>(() => HomeController()); | ||
281 | + Get.put<Service>(()=> Api()); | ||
282 | + } | ||
283 | +} | ||
284 | + | ||
285 | +class DetailsBinding implements Bindings { | ||
286 | + @override | ||
287 | + void dependencies() { | ||
288 | + Get.lazyPut<DetailsController>(() => DetailsController()); | ||
289 | + } | ||
290 | +} | ||
291 | +``` | ||
292 | + | ||
293 | +Now you just need to inform your route, that you will use that binding to make the connection between route manager, dependencies and states. | ||
294 | + | ||
295 | +- Using named routes: | ||
296 | + | ||
297 | +```dart | ||
298 | +getPages: [ | ||
299 | + GetPage( | ||
300 | + name: '/', | ||
301 | + page: () => HomeView(), | ||
302 | + binding: HomeBinding(), | ||
303 | + ), | ||
304 | + GetPage( | ||
305 | + name: '/details', | ||
306 | + page: () => DetailsView(), | ||
307 | + binding: DetailsBinding(), | ||
308 | + ), | ||
309 | +]; | ||
310 | +``` | ||
311 | + | ||
312 | +- Using normal routes: | ||
313 | + | ||
314 | +```dart | ||
315 | +Get.to(Home(), binding: HomeBinding()); | ||
316 | +Get.to(DetailsView(), binding: DetailsBinding()) | ||
317 | +``` | ||
318 | + | ||
319 | +There, you don't have to worry about memory management of your application anymore, Get will do it for you. | ||
320 | + | ||
321 | +The Binding class is called when a route is called, you can create an "initialBinding in your GetMaterialApp to insert all the dependencies that will be created. | ||
322 | + | ||
323 | +```dart | ||
324 | +GetMaterialApp( | ||
325 | + initialBinding: SampleBind(), | ||
326 | + home: Home(), | ||
327 | +); | ||
328 | +``` | ||
329 | + | ||
330 | +### BindingsBuilder | ||
331 | + | ||
332 | +The default way of creating a binding is by creating a class that implements Bindings. | ||
333 | +But alternatively, you can use `BindingsBuilder` callback so that you can simply use a function to instantiate whatever you desire. | ||
334 | + | ||
335 | +Example: | ||
336 | + | ||
337 | +```dart | ||
338 | +getPages: [ | ||
339 | + GetPage( | ||
340 | + name: '/', | ||
341 | + page: () => HomeView(), | ||
342 | + binding: BindingsBuilder(() { | ||
343 | + Get.lazyPut<ControllerX>(() => ControllerX()); | ||
344 | + Get.put<Service>(()=> Api()); | ||
345 | + }), | ||
346 | + ), | ||
347 | + GetPage( | ||
348 | + name: '/details', | ||
349 | + page: () => DetailsView(), | ||
350 | + binding: BindingsBuilder(() { | ||
351 | + Get.lazyPut<DetailsController>(() => DetailsController()); | ||
352 | + }), | ||
353 | + ), | ||
354 | +]; | ||
355 | +``` | ||
356 | + | ||
357 | +That way you can avoid to create one Binding class for each route making this even simpler. | ||
358 | + | ||
359 | +Both ways of doing work perfectly fine and we want you to use what most suit your tastes. | ||
360 | + | ||
361 | +### SmartManagement | ||
362 | + | ||
363 | +GetX by default disposes unused controllers from memory, even if a failure occurs and a widget that uses it is not properly disposed. | ||
364 | +This is what is called the `full` mode of dependency management. | ||
365 | +But if you want to change the way GetX controls the disposal of classes, you have `SmartManagement` class that you can set different behaviors. | ||
366 | + | ||
367 | +#### How to change | ||
368 | + | ||
369 | +If you want to change this config (which you usually don't need) this is the way: | ||
370 | + | ||
371 | +```dart | ||
372 | +void main () { | ||
373 | + runApp( | ||
374 | + GetMaterialApp( | ||
375 | + smartManagement: SmartManagement.onlyBuilders //here | ||
376 | + home: Home(), | ||
377 | + ) | ||
378 | + ) | ||
379 | +} | ||
380 | +``` | ||
381 | + | ||
382 | +#### SmartManagement.full | ||
383 | + | ||
384 | +It is the default one. Dispose classes that are not being used and were not set to be permanent. In the majority of the cases you will want to keep this config untouched. If you new to GetX then don't change this. | ||
385 | + | ||
386 | +#### SmartManagement.onlyBuilders | ||
387 | +With this option, only controllers started in `init:` or loaded into a Binding with `Get.lazyPut()` will be disposed. | ||
388 | + | ||
389 | +If you use `Get.put()` or `Get.putAsync()` or any other approach, SmartManagement will not have permissions to exclude this dependency. | ||
390 | + | ||
391 | +With the default behavior, even widgets instantiated with "Get.put" will be removed, unlike SmartManagement.onlyBuilders. | ||
392 | + | ||
393 | +#### SmartManagement.keepFactory | ||
394 | + | ||
395 | +Just like SmartManagement.full, it will remove it's dependencies when it's not being used anymore. However, it will keep their factory, which means it will recreate the dependency if you need that instance again. | ||
396 | + | ||
397 | +### How bindings work under the hood | ||
398 | +Bindings creates transitory factories, which are created the moment you click to go to another screen, and will be destroyed as soon as the screen-changing animation happens. | ||
399 | +This happens so fast that the analyzer will not even be able to register it. | ||
400 | +When you navigate to this screen again, a new temporary factory will be called, so this is preferable to using SmartManagement.keepFactory, but if you don't want to create Bindings, or want to keep all your dependencies on the same Binding, it will certainly help you. | ||
401 | +Factories take up little memory, they don't hold instances, but a function with the "shape" of that class you want. | ||
402 | +This has a very low cost in memory, but since the purpose of this lib is to get the maximum performance possible using the minimum resources, Get removes even the factories by default. | ||
403 | +Use whichever is most convenient for you. | ||
404 | + | ||
405 | +## Notes | ||
406 | + | ||
407 | +- DO NOT USE SmartManagement.keepFactory if you are using multiple Bindings. It was designed to be used without Bindings, or with a single Binding linked in the GetMaterialApp's initialBinding. | ||
408 | + | ||
409 | +- Using Bindings is completely optional, if you want you can use `Get.put()` and `Get.find()` on classes that use a given controller without any problem. | ||
410 | +However, if you work with Services or any other abstraction, I recommend using Bindings for a better organization. |
documentation/ar_EG/route_management.md
0 → 100644
1 | +- [Route Management](#route-management) | ||
2 | + - [How to use](#how-to-use) | ||
3 | + - [Navigation without named routes](#navigation-without-named-routes) | ||
4 | + - [Navigation with named routes](#navigation-with-named-routes) | ||
5 | + - [Send data to named Routes](#send-data-to-named-routes) | ||
6 | + - [Dynamic urls links](#dynamic-urls-links) | ||
7 | + - [Middleware](#middleware) | ||
8 | + - [Navigation without context](#navigation-without-context) | ||
9 | + - [SnackBars](#snackbars) | ||
10 | + - [Dialogs](#dialogs) | ||
11 | + - [BottomSheets](#bottomsheets) | ||
12 | + - [Nested Navigation](#nested-navigation) | ||
13 | + | ||
14 | +# Route Management | ||
15 | + | ||
16 | +This is the complete explanation of all there is to Getx when the matter is route management. | ||
17 | + | ||
18 | +## How to use | ||
19 | + | ||
20 | +Add this to your pubspec.yaml file: | ||
21 | + | ||
22 | +```yaml | ||
23 | +dependencies: | ||
24 | + get: | ||
25 | +``` | ||
26 | + | ||
27 | +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! | ||
28 | + | ||
29 | +```dart | ||
30 | +GetMaterialApp( // Before: MaterialApp( | ||
31 | + home: MyHome(), | ||
32 | +) | ||
33 | +``` | ||
34 | + | ||
35 | +## Navigation without named routes | ||
36 | + | ||
37 | +To navigate to a new screen: | ||
38 | + | ||
39 | +```dart | ||
40 | +Get.to(NextScreen()); | ||
41 | +``` | ||
42 | + | ||
43 | +To close snackbars, dialogs, bottomsheets, or anything you would normally close with Navigator.pop(context); | ||
44 | + | ||
45 | +```dart | ||
46 | +Get.back(); | ||
47 | +``` | ||
48 | + | ||
49 | +To go to the next screen and no option to go back to the previous screen (for use in SplashScreens, login screens and etc.) | ||
50 | + | ||
51 | +```dart | ||
52 | +Get.off(NextScreen()); | ||
53 | +``` | ||
54 | + | ||
55 | +To go to the next screen and cancel all previous routes (useful in shopping carts, polls, and tests) | ||
56 | + | ||
57 | +```dart | ||
58 | +Get.offAll(NextScreen()); | ||
59 | +``` | ||
60 | + | ||
61 | +To navigate to the next route, and receive or update data as soon as you return from it: | ||
62 | + | ||
63 | +```dart | ||
64 | +var data = await Get.to(Payment()); | ||
65 | +``` | ||
66 | + | ||
67 | +on other screen, send a data for previous route: | ||
68 | + | ||
69 | +```dart | ||
70 | +Get.back(result: 'success'); | ||
71 | +``` | ||
72 | + | ||
73 | +And use it: | ||
74 | + | ||
75 | +ex: | ||
76 | + | ||
77 | +```dart | ||
78 | +if(data == 'success') madeAnything(); | ||
79 | +``` | ||
80 | + | ||
81 | +Don't you want to learn our syntax? | ||
82 | +Just change the Navigator (uppercase) to navigator (lowercase), and you will have all the functions of the standard navigation, without having to use context | ||
83 | +Example: | ||
84 | + | ||
85 | +```dart | ||
86 | + | ||
87 | +// Default Flutter navigator | ||
88 | +Navigator.of(context).push( | ||
89 | + context, | ||
90 | + MaterialPageRoute( | ||
91 | + builder: (BuildContext context) { | ||
92 | + return HomePage(); | ||
93 | + }, | ||
94 | + ), | ||
95 | +); | ||
96 | + | ||
97 | +// Get using Flutter syntax without needing context | ||
98 | +navigator.push( | ||
99 | + MaterialPageRoute( | ||
100 | + builder: (_) { | ||
101 | + return HomePage(); | ||
102 | + }, | ||
103 | + ), | ||
104 | +); | ||
105 | + | ||
106 | +// Get syntax (It is much better, but you have the right to disagree) | ||
107 | +Get.to(HomePage()); | ||
108 | + | ||
109 | + | ||
110 | +``` | ||
111 | + | ||
112 | +## Navigation with named routes | ||
113 | + | ||
114 | +- If you prefer to navigate by namedRoutes, Get also supports this. | ||
115 | + | ||
116 | +To navigate to nextScreen | ||
117 | + | ||
118 | +```dart | ||
119 | +Get.toNamed("/NextScreen"); | ||
120 | +``` | ||
121 | + | ||
122 | +To navigate and remove the previous screen from the tree. | ||
123 | + | ||
124 | +```dart | ||
125 | +Get.offNamed("/NextScreen"); | ||
126 | +``` | ||
127 | + | ||
128 | +To navigate and remove all previous screens from the tree. | ||
129 | + | ||
130 | +```dart | ||
131 | +Get.offAllNamed("/NextScreen"); | ||
132 | +``` | ||
133 | + | ||
134 | +To define routes, use GetMaterialApp: | ||
135 | + | ||
136 | +```dart | ||
137 | +void main() { | ||
138 | + runApp( | ||
139 | + GetMaterialApp( | ||
140 | + initialRoute: '/', | ||
141 | + getPages: [ | ||
142 | + GetPage(name: '/', page: () => MyHomePage()), | ||
143 | + GetPage(name: '/second', page: () => Second()), | ||
144 | + GetPage( | ||
145 | + name: '/third', | ||
146 | + page: () => Third(), | ||
147 | + transition: Transition.zoom | ||
148 | + ), | ||
149 | + ], | ||
150 | + ) | ||
151 | + ); | ||
152 | +} | ||
153 | +``` | ||
154 | + | ||
155 | +To handle navigation to non-defined routes (404 error), you can define an unknownRoute page in GetMaterialApp. | ||
156 | + | ||
157 | +```dart | ||
158 | +void main() { | ||
159 | + runApp( | ||
160 | + GetMaterialApp( | ||
161 | + unknownRoute: GetPage(name: '/notfound', page: () => UnknownRoutePage()), | ||
162 | + initialRoute: '/', | ||
163 | + getPages: [ | ||
164 | + GetPage(name: '/', page: () => MyHomePage()), | ||
165 | + GetPage(name: '/second', page: () => Second()), | ||
166 | + ], | ||
167 | + ) | ||
168 | + ); | ||
169 | +} | ||
170 | +``` | ||
171 | + | ||
172 | +### Send data to named Routes | ||
173 | + | ||
174 | +Just send what you want for arguments. Get accepts anything here, whether it is a String, a Map, a List, or even a class instance. | ||
175 | + | ||
176 | +```dart | ||
177 | +Get.toNamed("/NextScreen", arguments: 'Get is the best'); | ||
178 | +``` | ||
179 | + | ||
180 | +on your class or controller: | ||
181 | + | ||
182 | +```dart | ||
183 | +print(Get.arguments); | ||
184 | +//print out: Get is the best | ||
185 | +``` | ||
186 | + | ||
187 | +### Dynamic urls links | ||
188 | + | ||
189 | +Get offer advanced dynamic urls just like on the Web. Web developers have probably already wanted this feature on Flutter, and most likely have seen a package promise this feature and deliver a totally different syntax than a URL would have on web, but Get also solves that. | ||
190 | + | ||
191 | +```dart | ||
192 | +Get.offAllNamed("/NextScreen?device=phone&id=354&name=Enzo"); | ||
193 | +``` | ||
194 | + | ||
195 | +on your controller/bloc/stateful/stateless class: | ||
196 | + | ||
197 | +```dart | ||
198 | +print(Get.parameters['id']); | ||
199 | +// out: 354 | ||
200 | +print(Get.parameters['name']); | ||
201 | +// out: Enzo | ||
202 | +``` | ||
203 | + | ||
204 | +You can also receive NamedParameters with Get easily: | ||
205 | + | ||
206 | +```dart | ||
207 | +void main() { | ||
208 | + runApp( | ||
209 | + GetMaterialApp( | ||
210 | + initialRoute: '/', | ||
211 | + getPages: [ | ||
212 | + GetPage( | ||
213 | + name: '/', | ||
214 | + page: () => MyHomePage(), | ||
215 | + ), | ||
216 | + GetPage( | ||
217 | + name: '/profile/', | ||
218 | + page: () => MyProfile(), | ||
219 | + ), | ||
220 | + //You can define a different page for routes with arguments, and another without arguments, but for that you must use the slash '/' on the route that will not receive arguments as above. | ||
221 | + GetPage( | ||
222 | + name: '/profile/:user', | ||
223 | + page: () => UserProfile(), | ||
224 | + ), | ||
225 | + GetPage( | ||
226 | + name: '/third', | ||
227 | + page: () => Third(), | ||
228 | + transition: Transition.cupertino | ||
229 | + ), | ||
230 | + ], | ||
231 | + ) | ||
232 | + ); | ||
233 | +} | ||
234 | +``` | ||
235 | + | ||
236 | +Send data on route name | ||
237 | + | ||
238 | +```dart | ||
239 | +Get.toNamed("/profile/34954"); | ||
240 | +``` | ||
241 | + | ||
242 | +On second screen take the data by parameter | ||
243 | + | ||
244 | +```dart | ||
245 | +print(Get.parameters['user']); | ||
246 | +// out: 34954 | ||
247 | +``` | ||
248 | + | ||
249 | +or send multiple parameters like this | ||
250 | + | ||
251 | +```dart | ||
252 | +Get.toNamed("/profile/34954?flag=true&country=italy"); | ||
253 | +``` | ||
254 | +or | ||
255 | +```dart | ||
256 | +var parameters = <String, String>{"flag": "true","country": "italy",}; | ||
257 | +Get.toNamed("/profile/34954", parameters: parameters); | ||
258 | +``` | ||
259 | + | ||
260 | +On second screen take the data by parameters as usually | ||
261 | + | ||
262 | +```dart | ||
263 | +print(Get.parameters['user']); | ||
264 | +print(Get.parameters['flag']); | ||
265 | +print(Get.parameters['country']); | ||
266 | +// out: 34954 true italy | ||
267 | +``` | ||
268 | + | ||
269 | + | ||
270 | + | ||
271 | +And now, all you need to do is use Get.toNamed() to navigate your named routes, without any context (you can call your routes directly from your BLoC or Controller class), and when your app is compiled to the web, your routes will appear in the url <3 | ||
272 | + | ||
273 | +### Middleware | ||
274 | + | ||
275 | +If you want to listen Get events to trigger actions, you can to use routingCallback to it | ||
276 | + | ||
277 | +```dart | ||
278 | +GetMaterialApp( | ||
279 | + routingCallback: (routing) { | ||
280 | + if(routing.current == '/second'){ | ||
281 | + openAds(); | ||
282 | + } | ||
283 | + } | ||
284 | +) | ||
285 | +``` | ||
286 | + | ||
287 | +If you are not using GetMaterialApp, you can use the manual API to attach Middleware observer. | ||
288 | + | ||
289 | +```dart | ||
290 | +void main() { | ||
291 | + runApp( | ||
292 | + MaterialApp( | ||
293 | + onGenerateRoute: Router.generateRoute, | ||
294 | + initialRoute: "/", | ||
295 | + navigatorKey: Get.key, | ||
296 | + navigatorObservers: [ | ||
297 | + GetObserver(MiddleWare.observer), // HERE !!! | ||
298 | + ], | ||
299 | + ), | ||
300 | + ); | ||
301 | +} | ||
302 | +``` | ||
303 | + | ||
304 | +Create a MiddleWare class | ||
305 | + | ||
306 | +```dart | ||
307 | +class MiddleWare { | ||
308 | + static observer(Routing routing) { | ||
309 | + /// You can listen in addition to the routes, the snackbars, dialogs and bottomsheets on each screen. | ||
310 | + ///If you need to enter any of these 3 events directly here, | ||
311 | + ///you must specify that the event is != Than you are trying to do. | ||
312 | + if (routing.current == '/second' && !routing.isSnackbar) { | ||
313 | + Get.snackbar("Hi", "You are on second route"); | ||
314 | + } else if (routing.current =='/third'){ | ||
315 | + print('last route called'); | ||
316 | + } | ||
317 | + } | ||
318 | +} | ||
319 | +``` | ||
320 | + | ||
321 | +Now, use Get on your code: | ||
322 | + | ||
323 | +```dart | ||
324 | +class First extends StatelessWidget { | ||
325 | + @override | ||
326 | + Widget build(BuildContext context) { | ||
327 | + return Scaffold( | ||
328 | + appBar: AppBar( | ||
329 | + leading: IconButton( | ||
330 | + icon: Icon(Icons.add), | ||
331 | + onPressed: () { | ||
332 | + Get.snackbar("hi", "i am a modern snackbar"); | ||
333 | + }, | ||
334 | + ), | ||
335 | + title: Text('First Route'), | ||
336 | + ), | ||
337 | + body: Center( | ||
338 | + child: ElevatedButton( | ||
339 | + child: Text('Open route'), | ||
340 | + onPressed: () { | ||
341 | + Get.toNamed("/second"); | ||
342 | + }, | ||
343 | + ), | ||
344 | + ), | ||
345 | + ); | ||
346 | + } | ||
347 | +} | ||
348 | + | ||
349 | +class Second extends StatelessWidget { | ||
350 | + @override | ||
351 | + Widget build(BuildContext context) { | ||
352 | + return Scaffold( | ||
353 | + appBar: AppBar( | ||
354 | + leading: IconButton( | ||
355 | + icon: Icon(Icons.add), | ||
356 | + onPressed: () { | ||
357 | + Get.snackbar("hi", "i am a modern snackbar"); | ||
358 | + }, | ||
359 | + ), | ||
360 | + title: Text('second Route'), | ||
361 | + ), | ||
362 | + body: Center( | ||
363 | + child: ElevatedButton( | ||
364 | + child: Text('Open route'), | ||
365 | + onPressed: () { | ||
366 | + Get.toNamed("/third"); | ||
367 | + }, | ||
368 | + ), | ||
369 | + ), | ||
370 | + ); | ||
371 | + } | ||
372 | +} | ||
373 | + | ||
374 | +class Third extends StatelessWidget { | ||
375 | + @override | ||
376 | + Widget build(BuildContext context) { | ||
377 | + return Scaffold( | ||
378 | + appBar: AppBar( | ||
379 | + title: Text("Third Route"), | ||
380 | + ), | ||
381 | + body: Center( | ||
382 | + child: ElevatedButton( | ||
383 | + onPressed: () { | ||
384 | + Get.back(); | ||
385 | + }, | ||
386 | + child: Text('Go back!'), | ||
387 | + ), | ||
388 | + ), | ||
389 | + ); | ||
390 | + } | ||
391 | +} | ||
392 | +``` | ||
393 | + | ||
394 | +## Navigation without context | ||
395 | + | ||
396 | +### SnackBars | ||
397 | + | ||
398 | +To have a simple SnackBar with Flutter, you must get the context of Scaffold, or you must use a GlobalKey attached to your Scaffold | ||
399 | + | ||
400 | +```dart | ||
401 | +final snackBar = SnackBar( | ||
402 | + content: Text('Hi!'), | ||
403 | + action: SnackBarAction( | ||
404 | + label: 'I am a old and ugly snackbar :(', | ||
405 | + onPressed: (){} | ||
406 | + ), | ||
407 | +); | ||
408 | +// Find the Scaffold in the widget tree and use | ||
409 | +// it to show a SnackBar. | ||
410 | +Scaffold.of(context).showSnackBar(snackBar); | ||
411 | +``` | ||
412 | + | ||
413 | +With Get: | ||
414 | + | ||
415 | +```dart | ||
416 | +Get.snackbar('Hi', 'i am a modern snackbar'); | ||
417 | +``` | ||
418 | + | ||
419 | +With Get, all you have to do is call your Get.snackbar from anywhere in your code or customize it however you want! | ||
420 | + | ||
421 | +```dart | ||
422 | +Get.snackbar( | ||
423 | + "Hey i'm a Get SnackBar!", // title | ||
424 | + "It's unbelievable! I'm using SnackBar without context, without boilerplate, without Scaffold, it is something truly amazing!", // message | ||
425 | + icon: Icon(Icons.alarm), | ||
426 | + shouldIconPulse: true, | ||
427 | + onTap:(){}, | ||
428 | + barBlur: 20, | ||
429 | + isDismissible: true, | ||
430 | + duration: Duration(seconds: 3), | ||
431 | +); | ||
432 | + | ||
433 | + | ||
434 | + ////////// ALL FEATURES ////////// | ||
435 | + // Color colorText, | ||
436 | + // Duration duration, | ||
437 | + // SnackPosition snackPosition, | ||
438 | + // Widget titleText, | ||
439 | + // Widget messageText, | ||
440 | + // bool instantInit, | ||
441 | + // Widget icon, | ||
442 | + // bool shouldIconPulse, | ||
443 | + // double maxWidth, | ||
444 | + // EdgeInsets margin, | ||
445 | + // EdgeInsets padding, | ||
446 | + // double borderRadius, | ||
447 | + // Color borderColor, | ||
448 | + // double borderWidth, | ||
449 | + // Color backgroundColor, | ||
450 | + // Color leftBarIndicatorColor, | ||
451 | + // List<BoxShadow> boxShadows, | ||
452 | + // Gradient backgroundGradient, | ||
453 | + // TextButton mainButton, | ||
454 | + // OnTap onTap, | ||
455 | + // bool isDismissible, | ||
456 | + // bool showProgressIndicator, | ||
457 | + // AnimationController progressIndicatorController, | ||
458 | + // Color progressIndicatorBackgroundColor, | ||
459 | + // Animation<Color> progressIndicatorValueColor, | ||
460 | + // SnackStyle snackStyle, | ||
461 | + // Curve forwardAnimationCurve, | ||
462 | + // Curve reverseAnimationCurve, | ||
463 | + // Duration animationDuration, | ||
464 | + // double barBlur, | ||
465 | + // double overlayBlur, | ||
466 | + // Color overlayColor, | ||
467 | + // Form userInputForm | ||
468 | + /////////////////////////////////// | ||
469 | +``` | ||
470 | + | ||
471 | +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 | ||
472 | +`Get.rawSnackbar();` which provides the RAW API on which Get.snackbar was built. | ||
473 | + | ||
474 | +### Dialogs | ||
475 | + | ||
476 | +To open dialog: | ||
477 | + | ||
478 | +```dart | ||
479 | +Get.dialog(YourDialogWidget()); | ||
480 | +``` | ||
481 | + | ||
482 | +To open default dialog: | ||
483 | + | ||
484 | +```dart | ||
485 | +Get.defaultDialog( | ||
486 | + onConfirm: () => print("Ok"), | ||
487 | + middleText: "Dialog made in 3 lines of code" | ||
488 | +); | ||
489 | +``` | ||
490 | + | ||
491 | +You can also use Get.generalDialog instead of showGeneralDialog. | ||
492 | + | ||
493 | +For all other Flutter dialog widgets, including cupertinos, you can use Get.overlayContext instead of context, and open it anywhere in your code. | ||
494 | +For widgets that don't use Overlay, you can use Get.context. | ||
495 | +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. | ||
496 | + | ||
497 | +### BottomSheets | ||
498 | + | ||
499 | +Get.bottomSheet is like showModalBottomSheet, but don't need of context. | ||
500 | + | ||
501 | +```dart | ||
502 | +Get.bottomSheet( | ||
503 | + Container( | ||
504 | + child: Wrap( | ||
505 | + children: <Widget>[ | ||
506 | + ListTile( | ||
507 | + leading: Icon(Icons.music_note), | ||
508 | + title: Text('Music'), | ||
509 | + onTap: () {} | ||
510 | + ), | ||
511 | + ListTile( | ||
512 | + leading: Icon(Icons.videocam), | ||
513 | + title: Text('Video'), | ||
514 | + onTap: () {}, | ||
515 | + ), | ||
516 | + ], | ||
517 | + ), | ||
518 | + ) | ||
519 | +); | ||
520 | +``` | ||
521 | + | ||
522 | +## Nested Navigation | ||
523 | + | ||
524 | +Get made Flutter's nested navigation even easier. | ||
525 | +You don't need the context, and you will find your navigation stack by Id. | ||
526 | + | ||
527 | +- NOTE: Creating parallel navigation stacks can be dangerous. The ideal is not to use NestedNavigators, or to use sparingly. If your project requires it, go ahead, but keep in mind that keeping multiple navigation stacks in memory may not be a good idea for RAM consumption. | ||
528 | + | ||
529 | +See how simple it is: | ||
530 | + | ||
531 | +```dart | ||
532 | +Navigator( | ||
533 | + key: Get.nestedKey(1), // create a key by index | ||
534 | + initialRoute: '/', | ||
535 | + onGenerateRoute: (settings) { | ||
536 | + if (settings.name == '/') { | ||
537 | + return GetPageRoute( | ||
538 | + page: () => Scaffold( | ||
539 | + appBar: AppBar( | ||
540 | + title: Text("Main"), | ||
541 | + ), | ||
542 | + body: Center( | ||
543 | + child: TextButton( | ||
544 | + color: Colors.blue, | ||
545 | + onPressed: () { | ||
546 | + Get.toNamed('/second', id:1); // navigate by your nested route by index | ||
547 | + }, | ||
548 | + child: Text("Go to second"), | ||
549 | + ), | ||
550 | + ), | ||
551 | + ), | ||
552 | + ); | ||
553 | + } else if (settings.name == '/second') { | ||
554 | + return GetPageRoute( | ||
555 | + page: () => Center( | ||
556 | + child: Scaffold( | ||
557 | + appBar: AppBar( | ||
558 | + title: Text("Main"), | ||
559 | + ), | ||
560 | + body: Center( | ||
561 | + child: Text("second") | ||
562 | + ), | ||
563 | + ), | ||
564 | + ), | ||
565 | + ); | ||
566 | + } | ||
567 | + } | ||
568 | +), | ||
569 | +``` |
documentation/ar_EG/state_management.md
0 → 100644
1 | +* [State Management](#state-management) | ||
2 | + + [Reactive State Manager](#reactive-state-manager) | ||
3 | + - [Advantages](#advantages) | ||
4 | + - [Maximum performance:](#maximum-performance) | ||
5 | + - [Declaring a reactive variable](#declaring-a-reactive-variable) | ||
6 | + - [Having a reactive state, is easy.](#having-a-reactive-state-is-easy) | ||
7 | + - [Using the values in the view](#using-the-values-in-the-view) | ||
8 | + - [Conditions to rebuild](#conditions-to-rebuild) | ||
9 | + - [Where .obs can be used](#where-obs-can-be-used) | ||
10 | + - [Note about Lists](#note-about-lists) | ||
11 | + - [Why i have to use .value](#why-i-have-to-use-value) | ||
12 | + - [Obx()](#obx) | ||
13 | + - [Workers](#workers) | ||
14 | + + [Simple State Manager](#simple-state-manager) | ||
15 | + - [Advantages](#advantages-1) | ||
16 | + - [Usage](#usage) | ||
17 | + - [How it handles controllers](#how-it-handles-controllers) | ||
18 | + - [You won't need StatefulWidgets anymore](#you-wont-need-statefulwidgets-anymore) | ||
19 | + - [Why it exists](#why-it-exists) | ||
20 | + - [Other ways of using it](#other-ways-of-using-it) | ||
21 | + - [Unique IDs](#unique-ids) | ||
22 | + + [Mixing the two state managers](#mixing-the-two-state-managers) | ||
23 | + + [GetBuilder vs GetX vs Obx vs MixinBuilder](#getbuilder-vs-getx-vs-obx-vs-mixinbuilder) | ||
24 | + | ||
25 | +# State Management | ||
26 | + | ||
27 | +GetX does not use Streams or ChangeNotifier like other state managers. Why? In addition to building applications for android, iOS, web, linux, macos and linux, with GetX you can build server applications with the same syntax as Flutter/GetX. In order to improve response time and reduce RAM consumption, we created GetValue and GetStream, which are low latency solutions that deliver a lot of performance, at a low operating cost. We use this base to build all of our resources, including state management. | ||
28 | + | ||
29 | +* _Complexity_: Some state managers are complex and have a lot of boilerplate. With GetX you don't have to define a class for each event, the code is highly clean and clear, and you do a lot more by writing less. Many people have given up on Flutter because of this topic, and they now finally have a stupidly simple solution for managing states. | ||
30 | +* _No code generators_: You spend half your development time writing your application logic. Some state managers rely on code generators to have minimally readable code. Changing a variable and having to run build_runner can be unproductive, and often the waiting time after a flutter clean will be long, and you will have to drink a lot of coffee. | ||
31 | + | ||
32 | +With GetX everything is reactive, and nothing depends on code generators, increasing your productivity in all aspects of your development. | ||
33 | + | ||
34 | +* _It does not depend on context_: You probably already needed to send the context of your view to a controller, making the View's coupling with your business logic high. You have probably had to use a dependency for a place that has no context, and had to pass the context through various classes and functions. This just doesn't exist with GetX. You have access to your controllers from within your controllers without any context. You don't need to send the context by parameter for literally nothing. | ||
35 | +* _Granular control_: most state managers are based on ChangeNotifier. ChangeNotifier will notify all widgets that depend on it when notifyListeners is called. If you have 40 widgets on one screen, which have a variable of your ChangeNotifier class, when you update one, all of them will be rebuilt. | ||
36 | + | ||
37 | +With GetX, even nested widgets are respected. If you have Obx watching your ListView, and another watching a checkbox inside the ListView, when changing the CheckBox value, only it will be updated, when changing the List value, only the ListView will be updated. | ||
38 | + | ||
39 | +* _It only reconstructs if its variable REALLY changes_: GetX has flow control, that means if you display a Text with 'Paola', if you change the observable variable to 'Paola' again, the widget will not be reconstructed. That's because GetX knows that 'Paola' is already being displayed in Text, and will not do unnecessary reconstructions. | ||
40 | + | ||
41 | +Most (if not all) current state managers will rebuild on the screen. | ||
42 | + | ||
43 | +## Reactive State Manager | ||
44 | + | ||
45 | +Reactive programming can alienate many people because it is said to be complicated. GetX turns reactive programming into something quite simple: | ||
46 | + | ||
47 | +* You won't need to create StreamControllers. | ||
48 | +* You won't need to create a StreamBuilder for each variable | ||
49 | +* You will not need to create a class for each state. | ||
50 | +* You will not need to create a get for an initial value. | ||
51 | + | ||
52 | +Reactive programming with Get is as easy as using setState. | ||
53 | + | ||
54 | +Let's imagine that you have a name variable and want that every time you change it, all widgets that use it are automatically changed. | ||
55 | + | ||
56 | +This is your count variable: | ||
57 | + | ||
58 | +``` dart | ||
59 | +var name = 'Jonatas Borges'; | ||
60 | +``` | ||
61 | + | ||
62 | +To make it observable, you just need to add ".obs" to the end of it: | ||
63 | + | ||
64 | +``` dart | ||
65 | +var name = 'Jonatas Borges'.obs; | ||
66 | +``` | ||
67 | + | ||
68 | +That's all. It's *that* simple. | ||
69 | + | ||
70 | +From now on, we might refer to this reactive-".obs"(ervables) variables as _Rx_. | ||
71 | + | ||
72 | +What did we do under the hood? We created a `Stream` of `String` s, assigned the initial value `"Jonatas Borges"` , we notified all widgets that use `"Jonatas Borges"` that they now "belong" to this variable, and when the _Rx_ value changes, they will have to change as well. | ||
73 | + | ||
74 | +This is the **magic of GetX**, thanks to Dart's capabilities. | ||
75 | + | ||
76 | +But, as we know, a `Widget` can only be changed if it is inside a function, because static classes do not have the power to "auto-change". | ||
77 | + | ||
78 | +You will need to create a `StreamBuilder` , subscribe to this variable to listen for changes, and create a "cascade" of nested `StreamBuilder` if you want to change several variables in the same scope, right? | ||
79 | + | ||
80 | +No, you don't need a `StreamBuilder` , but you are right about static classes. | ||
81 | + | ||
82 | +Well, in the view, we usually have a lot of boilerplate when we want to change a specific Widget, that's the Flutter way. | ||
83 | +With **GetX** you can also forget about this boilerplate code. | ||
84 | + | ||
85 | +`StreamBuilder( … )` ? `initialValue: …` ? `builder: …` ? Nope, you just need to place this variable inside an `Obx()` Widget. | ||
86 | + | ||
87 | +``` dart | ||
88 | +Obx (() => Text (controller.name)); | ||
89 | +``` | ||
90 | + | ||
91 | +_What do you need to memorize?_ Only `Obx(() =>` . | ||
92 | + | ||
93 | +You are just passing that Widget through an arrow-function into an `Obx()` (the "Observer" of the _Rx_). | ||
94 | + | ||
95 | +`Obx` is pretty smart, and will only change if the value of `controller.name` changes. | ||
96 | + | ||
97 | +If `name` is `"John"` , and you change it to `"John"` ( `name.value = "John"` ), as it's the same `value` as before, nothing will change on the screen, and `Obx` , to save resources, will simply ignore the new value and not rebuild the Widget. **Isn't that amazing?** | ||
98 | + | ||
99 | +> So, what if I have 5 _Rx_ (observable) variables within an `Obx` ? | ||
100 | + | ||
101 | +It will just update when **any** of them changes. | ||
102 | + | ||
103 | +> And if I have 30 variables in a class, when I update one, will it update **all** the variables that are in that class? | ||
104 | + | ||
105 | +Nope, just the **specific Widget** that uses that _Rx_ variable. | ||
106 | + | ||
107 | +So, **GetX** only updates the screen, when the _Rx_ variable changes it's value. | ||
108 | + | ||
109 | +``` | ||
110 | + | ||
111 | +final isOpen = false.obs; | ||
112 | + | ||
113 | +// NOTHING will happen... same value. | ||
114 | +void onButtonTap() => isOpen.value=false; | ||
115 | +``` | ||
116 | + | ||
117 | +### Advantages | ||
118 | + | ||
119 | +**GetX()** helps you when you need **granular** control over what's being updated. | ||
120 | + | ||
121 | +If you do not need `unique IDs` , because all your variables will be modified when you perform an action, then use `GetBuilder` , | ||
122 | +because it's a Simple State Updater (in blocks, like `setState()` ), made in just a few lines of code. | ||
123 | +It was made simple, to have the least CPU impact, and just to fulfill a single purpose (a _State_ rebuild) and spend the minimum resources possible. | ||
124 | + | ||
125 | +If you need a **powerful** State Manager, you can't go wrong with **GetX**. | ||
126 | + | ||
127 | +It doesn't work with variables, but __flows__, everything in it are `Streams` under the hood. | ||
128 | + | ||
129 | +You can use _rxDart_ in conjunction with it, because everything are `Streams`, | ||
130 | +you can listen to the `event` of each "_Rx_ variable", | ||
131 | +because everything in it are `Streams`. | ||
132 | + | ||
133 | +It is literally a _BLoC_ approach, easier than _MobX_, and without code generators or decorations. | ||
134 | +You can turn **anything** into an _"Observable"_ with just a `.obs` . | ||
135 | + | ||
136 | +### Maximum performance: | ||
137 | + | ||
138 | +In addition to having a smart algorithm for minimal rebuilds, **GetX** uses comparators | ||
139 | +to make sure the State has changed. | ||
140 | + | ||
141 | +If you experience any errors in your app, and send a duplicate change of State, | ||
142 | +**GetX** will ensure it will not crash. | ||
143 | + | ||
144 | +With **GetX** the State only changes if the `value` change. | ||
145 | +That's the main difference between **GetX**, and using _ `computed` from MobX_. | ||
146 | +When joining two __observables__, and one changes; the listener of that _observable_ will change as well. | ||
147 | + | ||
148 | +With **GetX**, if you join two variables, `GetX()` (similar to `Observer()` ) will only rebuild if it implies a real change of State. | ||
149 | + | ||
150 | +### Declaring a reactive variable | ||
151 | + | ||
152 | +You have 3 ways to turn a variable into an "observable". | ||
153 | + | ||
154 | +1 - The first is using **`Rx{Type}`**. | ||
155 | + | ||
156 | +``` dart | ||
157 | +// initial value is recommended, but not mandatory | ||
158 | +final name = RxString(''); | ||
159 | +final isLogged = RxBool(false); | ||
160 | +final count = RxInt(0); | ||
161 | +final balance = RxDouble(0.0); | ||
162 | +final items = RxList<String>([]); | ||
163 | +final myMap = RxMap<String, int>({}); | ||
164 | +``` | ||
165 | + | ||
166 | +2 - The second is to use **`Rx`** and use Darts Generics, `Rx<Type>` | ||
167 | + | ||
168 | +``` dart | ||
169 | +final name = Rx<String>(''); | ||
170 | +final isLogged = Rx<Bool>(false); | ||
171 | +final count = Rx<Int>(0); | ||
172 | +final balance = Rx<Double>(0.0); | ||
173 | +final number = Rx<Num>(0); | ||
174 | +final items = Rx<List<String>>([]); | ||
175 | +final myMap = Rx<Map<String, int>>({}); | ||
176 | + | ||
177 | +// Custom classes - it can be any class, literally | ||
178 | +final user = Rx<User>(); | ||
179 | +``` | ||
180 | + | ||
181 | +3 - The third, more practical, easier and preferred approach, just add **`.obs`** as a property of your `value` : | ||
182 | + | ||
183 | +``` dart | ||
184 | +final name = ''.obs; | ||
185 | +final isLogged = false.obs; | ||
186 | +final count = 0.obs; | ||
187 | +final balance = 0.0.obs; | ||
188 | +final number = 0.obs; | ||
189 | +final items = <String>[].obs; | ||
190 | +final myMap = <String, int>{}.obs; | ||
191 | + | ||
192 | +// Custom classes - it can be any class, literally | ||
193 | +final user = User().obs; | ||
194 | +``` | ||
195 | + | ||
196 | +##### Having a reactive state, is easy. | ||
197 | + | ||
198 | +As we know, _Dart_ is now heading towards _null safety_. | ||
199 | +To be prepared, from now on, you should always start your _Rx_ variables with an **initial value**. | ||
200 | + | ||
201 | +> Transforming a variable into an _observable_ + _initial value_ with **GetX** is the simplest, and most practical approach. | ||
202 | + | ||
203 | +You will literally add a " `.obs` " to the end of your variable, and **that’s it**, you’ve made it observable, | ||
204 | +and its `.value` , well, will be the _initial value_). | ||
205 | + | ||
206 | +### Using the values in the view | ||
207 | + | ||
208 | +``` dart | ||
209 | +// controller file | ||
210 | +final count1 = 0.obs; | ||
211 | +final count2 = 0.obs; | ||
212 | +int get sum => count1.value + count2.value; | ||
213 | +``` | ||
214 | + | ||
215 | +``` dart | ||
216 | +// view file | ||
217 | +GetX<Controller>( | ||
218 | + builder: (controller) { | ||
219 | + print("count 1 rebuild"); | ||
220 | + return Text('${controller.count1.value}'); | ||
221 | + }, | ||
222 | +), | ||
223 | +GetX<Controller>( | ||
224 | + builder: (controller) { | ||
225 | + print("count 2 rebuild"); | ||
226 | + return Text('${controller.count2.value}'); | ||
227 | + }, | ||
228 | +), | ||
229 | +GetX<Controller>( | ||
230 | + builder: (controller) { | ||
231 | + print("count 3 rebuild"); | ||
232 | + return Text('${controller.sum}'); | ||
233 | + }, | ||
234 | +), | ||
235 | +``` | ||
236 | + | ||
237 | +If we increment `count1.value++` , it will print: | ||
238 | + | ||
239 | +* `count 1 rebuild` | ||
240 | + | ||
241 | +* `count 3 rebuild` | ||
242 | + | ||
243 | +because `count1` has a value of `1` , and `1 + 0 = 1` , changing the `sum` getter value. | ||
244 | + | ||
245 | +If we change `count2.value++` , it will print: | ||
246 | + | ||
247 | +* `count 2 rebuild` | ||
248 | + | ||
249 | +* `count 3 rebuild` | ||
250 | + | ||
251 | +because `count2.value` changed, and the result of the `sum` is now `2` . | ||
252 | + | ||
253 | +* NOTE: By default, the very first event will rebuild the widget, even if it is the same `value`. | ||
254 | + | ||
255 | + This behavior exists due to Boolean variables. | ||
256 | + | ||
257 | +Imagine you did this: | ||
258 | + | ||
259 | +``` dart | ||
260 | +var isLogged = false.obs; | ||
261 | +``` | ||
262 | + | ||
263 | +And then, you checked if a user is "logged in" to trigger an event in `ever` . | ||
264 | + | ||
265 | +``` dart | ||
266 | +@override | ||
267 | +onInit() async { | ||
268 | + ever(isLogged, fireRoute); | ||
269 | + isLogged.value = await Preferences.hasToken(); | ||
270 | +} | ||
271 | + | ||
272 | +fireRoute(logged) { | ||
273 | + if (logged) { | ||
274 | + Get.off(Home()); | ||
275 | + } else { | ||
276 | + Get.off(Login()); | ||
277 | + } | ||
278 | +} | ||
279 | +``` | ||
280 | + | ||
281 | +if `hasToken` was `false` , there would be no change to `isLogged` , so `ever()` would never be called. | ||
282 | +To avoid this type of behavior, the first change to an _observable_ will always trigger an event, | ||
283 | +even if it contains the same `.value` . | ||
284 | + | ||
285 | +You can remove this behavior if you want, using: | ||
286 | + `isLogged.firstRebuild = false;` | ||
287 | + | ||
288 | +### Conditions to rebuild | ||
289 | + | ||
290 | +In addition, Get provides refined state control. You can condition an event (such as adding an object to a list), on a certain condition. | ||
291 | + | ||
292 | +``` dart | ||
293 | +// First parameter: condition, must return true or false. | ||
294 | +// Second parameter: the new value to apply if the condition is true. | ||
295 | +list.addIf(item < limit, item); | ||
296 | +``` | ||
297 | + | ||
298 | +Without decorations, without a code generator, without complications :smile: | ||
299 | + | ||
300 | +Do you know Flutter's counter app? Your Controller class might look like this: | ||
301 | + | ||
302 | +``` dart | ||
303 | +class CountController extends GetxController { | ||
304 | + final count = 0.obs; | ||
305 | +} | ||
306 | +``` | ||
307 | + | ||
308 | +With a simple: | ||
309 | + | ||
310 | +``` dart | ||
311 | +controller.count.value++ | ||
312 | +``` | ||
313 | + | ||
314 | +You could update the counter variable in your UI, regardless of where it is stored. | ||
315 | + | ||
316 | +### Where .obs can be used | ||
317 | + | ||
318 | +You can transform anything on obs. Here are two ways of doing it: | ||
319 | + | ||
320 | +* You can convert your class values to obs | ||
321 | + | ||
322 | +``` dart | ||
323 | +class RxUser { | ||
324 | + final name = "Camila".obs; | ||
325 | + final age = 18.obs; | ||
326 | +} | ||
327 | +``` | ||
328 | + | ||
329 | +* or you can convert the entire class to be an observable | ||
330 | + | ||
331 | +``` dart | ||
332 | +class User { | ||
333 | + User({String name, int age}); | ||
334 | + var name; | ||
335 | + var age; | ||
336 | +} | ||
337 | + | ||
338 | +// when instantianting: | ||
339 | +final user = User(name: "Camila", age: 18).obs; | ||
340 | +``` | ||
341 | + | ||
342 | +### Note about Lists | ||
343 | + | ||
344 | +Lists 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. | ||
345 | + | ||
346 | +You also don't need to use ".value" with lists, the amazing dart api allowed us to remove that. | ||
347 | +Unfortunaly 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. | ||
348 | + | ||
349 | +``` dart | ||
350 | +// On the controller | ||
351 | +final String title = 'User Info:'.obs | ||
352 | +final list = List<User>().obs; | ||
353 | + | ||
354 | +// on the view | ||
355 | +Text(controller.title.value), // String need to have .value in front of it | ||
356 | +ListView.builder ( | ||
357 | + itemCount: controller.list.length // lists don't need it | ||
358 | +) | ||
359 | +``` | ||
360 | + | ||
361 | +When you are making your own classes observable, there is a different way to update them: | ||
362 | + | ||
363 | +``` dart | ||
364 | +// on the model file | ||
365 | +// we are going to make the entire class observable instead of each attribute | ||
366 | +class User() { | ||
367 | + User({this.name = '', this.age = 0}); | ||
368 | + String name; | ||
369 | + int age; | ||
370 | +} | ||
371 | + | ||
372 | +// on the controller file | ||
373 | +final user = User().obs; | ||
374 | +// when you need to update the user variable: | ||
375 | +user.update( (user) { // this parameter is the class itself that you want to update | ||
376 | +user.name = 'Jonny'; | ||
377 | +user.age = 18; | ||
378 | +}); | ||
379 | +// an alternative way of update the user variable: | ||
380 | +user(User(name: 'João', age: 35)); | ||
381 | + | ||
382 | +// on view: | ||
383 | +Obx(()=> Text("Name ${user.value.name}: Age: ${user.value.age}")) | ||
384 | +// you can also access the model values without the .value: | ||
385 | +user().name; // notice that is the user variable, not the class (variable has lowercase u) | ||
386 | +``` | ||
387 | + | ||
388 | +You don't have to work with sets if you don't want to. you can use the "assign 'and" assignAll "api. | ||
389 | +The "assign" api will clear your list, and add a single object that you want to start there. | ||
390 | +The "assignAll" api will clear the existing list and add any iterable objects that you inject into it. | ||
391 | + | ||
392 | +### Why i have to use .value | ||
393 | + | ||
394 | +We could remove the obligation to use 'value' to `String` and `int` with a simple decoration and code generator, but the purpose of this library is precisely avoid external dependencies. We want to offer an environment ready for programming, involving the essentials (management of routes, dependencies and states), in a simple, lightweight and performant way, without a need of an external package. | ||
395 | + | ||
396 | +You can literally add 3 letters to your pubspec (get) and a colon and start programming. All solutions included by default, from route management to state management, aim at ease, productivity and performance. | ||
397 | + | ||
398 | +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. | ||
399 | + | ||
400 | +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. | ||
401 | + | ||
402 | +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. | ||
403 | + | ||
404 | +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. | ||
405 | + | ||
406 | +### Obx() | ||
407 | + | ||
408 | +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. | ||
409 | +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. | ||
410 | + | ||
411 | +### Workers | ||
412 | + | ||
413 | +Workers will assist you, triggering specific callbacks when an event occurs. | ||
414 | + | ||
415 | +``` dart | ||
416 | +/// Called every time `count1` changes. | ||
417 | +ever(count1, (_) => print("$_ has been changed")); | ||
418 | + | ||
419 | +/// Called only first time the variable $_ is changed | ||
420 | +once(count1, (_) => print("$_ was changed once")); | ||
421 | + | ||
422 | +/// Anti DDos - Called every time the user stops typing for 1 second, for example. | ||
423 | +debounce(count1, (_) => print("debouce$_"), time: Duration(seconds: 1)); | ||
424 | + | ||
425 | +/// Ignore all changes within 1 second. | ||
426 | +interval(count1, (_) => print("interval $_"), time: Duration(seconds: 1)); | ||
427 | +``` | ||
428 | + | ||
429 | +All workers (except `debounce` ) have a `condition` named parameter, which can be a `bool` or a callback that returns a `bool` . | ||
430 | +This `condition` defines when the `callback` function executes. | ||
431 | + | ||
432 | +All workers returns a `Worker` instance, that you can use to cancel ( via `dispose()` ) the worker. | ||
433 | + | ||
434 | + | ||
435 | +* **`ever`** | ||
436 | + | ||
437 | + is called every time the _Rx_ variable emits a new value. | ||
438 | + | ||
439 | +* **`everAll`** | ||
440 | + | ||
441 | + Much like `ever` , but it takes a `List` of _Rx_ values Called every time its variable is changed. That's it. | ||
442 | + | ||
443 | +* **`once`** | ||
444 | + | ||
445 | +'once' is called only the first time the variable has been changed. | ||
446 | + | ||
447 | +* **`debounce`** | ||
448 | + | ||
449 | +'debounce' is very useful in search functions, where you only want the API to be called when the user finishes typing. If the user types "Jonny", you will have 5 searches in the APIs, by the letter J, o, n, n, and y. With Get this does not happen, because you will have a "debounce" Worker that will only be triggered at the end of typing. | ||
450 | + | ||
451 | +* **`interval`** | ||
452 | + | ||
453 | +'interval' is different from the debouce. debouce if the user makes 1000 changes to a variable within 1 second, he will send only the last one after the stipulated timer (the default is 800 milliseconds). Interval will instead ignore all user actions for the stipulated period. If you send events for 1 minute, 1000 per second, debounce will only send you the last one, when the user stops strafing events. interval will deliver events every second, and if set to 3 seconds, it will deliver 20 events that minute. This is recommended to avoid abuse, in functions where the user can quickly click on something and get some advantage (imagine that the user can earn coins by clicking on something, if he clicked 300 times in the same minute, he would have 300 coins, using interval, you you can set a time frame for 3 seconds, and even then clicking 300 or a thousand times, the maximum he would get in 1 minute would be 20 coins, clicking 300 or 1 million times). The debounce is suitable for anti-DDos, for functions like search where each change to onChange would cause a query to your api. Debounce will wait for the user to stop typing the name, to make the request. If it were used in the coin scenario mentioned above, the user would only win 1 coin, because it is only executed, when the user "pauses" for the established time. | ||
454 | + | ||
455 | +* NOTE: Workers should always be used when starting a Controller or Class, so it should always be on onInit (recommended), Class constructor, or the initState of a StatefulWidget (this practice is not recommended in most cases, but it shouldn't have any side effects). | ||
456 | + | ||
457 | +## Simple State Manager | ||
458 | + | ||
459 | +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. | ||
460 | + | ||
461 | +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. | ||
462 | + | ||
463 | +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. | ||
464 | + | ||
465 | +### Advantages | ||
466 | + | ||
467 | +1. Update only the required widgets. | ||
468 | + | ||
469 | +2. Does not use changeNotifier, it is the state manager that uses less memory (close to 0mb). | ||
470 | + | ||
471 | +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. | ||
472 | + | ||
473 | +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. | ||
474 | + | ||
475 | +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. | ||
476 | + | ||
477 | +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. | ||
478 | + | ||
479 | +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). | ||
480 | + | ||
481 | +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. | ||
482 | + | ||
483 | +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. | ||
484 | + | ||
485 | +### Usage | ||
486 | + | ||
487 | +``` dart | ||
488 | +// Create controller class and extends GetxController | ||
489 | +class Controller extends GetxController { | ||
490 | + int counter = 0; | ||
491 | + void increment() { | ||
492 | + counter++; | ||
493 | + update(); // use update() to update counter variable on UI when increment be called | ||
494 | + } | ||
495 | +} | ||
496 | +// On your Stateless/Stateful class, use GetBuilder to update Text when increment be called | ||
497 | +GetBuilder<Controller>( | ||
498 | + init: Controller(), // INIT IT ONLY THE FIRST TIME | ||
499 | + builder: (_) => Text( | ||
500 | + '${_.counter}', | ||
501 | + ), | ||
502 | +) | ||
503 | +//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. | ||
504 | +``` | ||
505 | + | ||
506 | +**Done!** | ||
507 | + | ||
508 | +* You have already learned how to manage states with Get. | ||
509 | + | ||
510 | +* Note: You may want a larger organization, and not use the init property. For that, you can create a class and extends Bindings class, and within it mention the controllers that will be created within that route. Controllers will not be created at that time, on the contrary, this is just a statement, so that the first time you use a Controller, Get will know where to look. Get will remain lazyLoad, and will continue to dispose Controllers when they are no longer needed. See the pub.dev example to see how it works. | ||
511 | + | ||
512 | +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): | ||
513 | + | ||
514 | +``` dart | ||
515 | +class OtherClass extends StatelessWidget { | ||
516 | + @override | ||
517 | + Widget build(BuildContext context) { | ||
518 | + return Scaffold( | ||
519 | + body: Center( | ||
520 | + child: GetBuilder<Controller>( | ||
521 | + builder: (s) => Text('${s.counter}'), | ||
522 | + ), | ||
523 | + ), | ||
524 | + ); | ||
525 | + } | ||
526 | + | ||
527 | +``` | ||
528 | + | ||
529 | +If you need to use your controller in many other places, and outside of GetBuilder, just create a get in your controller and have it easily. (or use `Get.find<Controller>()` ) | ||
530 | + | ||
531 | +``` dart | ||
532 | +class Controller extends GetxController { | ||
533 | + | ||
534 | + /// You do not need that. I recommend using it just for ease of syntax. | ||
535 | + /// with static method: Controller.to.increment(); | ||
536 | + /// with no static method: Get.find<Controller>().increment(); | ||
537 | + /// There is no difference in performance, nor any side effect of using either syntax. Only one does not need the type, and the other the IDE will autocomplete it. | ||
538 | + static Controller get to => Get.find(); // add this line | ||
539 | + | ||
540 | + int counter = 0; | ||
541 | + void increment() { | ||
542 | + counter++; | ||
543 | + update(); | ||
544 | + } | ||
545 | +} | ||
546 | +``` | ||
547 | + | ||
548 | +And then you can access your controller directly, that way: | ||
549 | + | ||
550 | +``` dart | ||
551 | +FloatingActionButton( | ||
552 | + onPressed: () { | ||
553 | + Controller.to.increment(), | ||
554 | + } // This is incredibly simple! | ||
555 | + child: Text("${Controller.to.counter}"), | ||
556 | +), | ||
557 | +``` | ||
558 | + | ||
559 | +When you press FloatingActionButton, all widgets that are listening to the 'counter' variable will be updated automatically. | ||
560 | + | ||
561 | +### How it handles controllers | ||
562 | + | ||
563 | +Let's say we have this: | ||
564 | + | ||
565 | + `Class a => Class B (has controller X) => Class C (has controller X)` | ||
566 | + | ||
567 | +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. | ||
568 | + | ||
569 | +### You won't need StatefulWidgets anymore | ||
570 | + | ||
571 | +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. | ||
572 | +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! | ||
573 | +Unless you need to use a mixin, like TickerProviderStateMixin, it will be totally unnecessary to use a StatefulWidget with Get. | ||
574 | + | ||
575 | +You can call all methods of a StatefulWidget directly from a GetBuilder. | ||
576 | +If you need to call initState() or dispose() method for example, you can call them directly; | ||
577 | + | ||
578 | +``` dart | ||
579 | +GetBuilder<Controller>( | ||
580 | + initState: (_) => Controller.to.fetchApi(), | ||
581 | + dispose: (_) => Controller.to.closeStreams(), | ||
582 | + builder: (s) => Text('${s.username}'), | ||
583 | +), | ||
584 | +``` | ||
585 | + | ||
586 | +A much better approach than this is to use the onInit() and onClose() method directly from your controller. | ||
587 | + | ||
588 | +``` dart | ||
589 | +@override | ||
590 | +void onInit() { | ||
591 | + fetchApi(); | ||
592 | + super.onInit(); | ||
593 | +} | ||
594 | +``` | ||
595 | + | ||
596 | +* 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. | ||
597 | + | ||
598 | +### Why it exists | ||
599 | + | ||
600 | +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. | ||
601 | + | ||
602 | +So to simplify this: | ||
603 | +You don't need to call methods in initState and send them by parameter to your controller, nor use your controller constructor for that, you have the onInit() method that is called at the right time for you to start your services. | ||
604 | +You do not need to call the device, you have the onClose() method that will be called at the exact moment when your controller is no longer needed and will be removed from memory. That way, leave views for widgets only, refrain from any kind of business logic from it. | ||
605 | + | ||
606 | +Do not call a dispose method inside GetxController, it will not do anything, remember that the controller is not a Widget, you should not "dispose" it, and it will be automatically and intelligently removed from memory by Get. If you used any stream on it and want to close it, just insert it into the close method. Example: | ||
607 | + | ||
608 | +``` dart | ||
609 | +class Controller extends GetxController { | ||
610 | + StreamController<User> user = StreamController<User>(); | ||
611 | + StreamController<String> name = StreamController<String>(); | ||
612 | + | ||
613 | + /// close stream = onClose method, not dispose. | ||
614 | + @override | ||
615 | + void onClose() { | ||
616 | + user.close(); | ||
617 | + name.close(); | ||
618 | + super.onClose(); | ||
619 | + } | ||
620 | +} | ||
621 | +``` | ||
622 | + | ||
623 | +Controller life cycle: | ||
624 | + | ||
625 | +* onInit() where it is created. | ||
626 | +* onClose() where it is closed to make any changes in preparation for the delete method | ||
627 | +* 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. | ||
628 | + | ||
629 | +### Other ways of using it | ||
630 | + | ||
631 | +You can use Controller instance directly on GetBuilder value: | ||
632 | + | ||
633 | +``` dart | ||
634 | +GetBuilder<Controller>( | ||
635 | + init: Controller(), | ||
636 | + builder: (value) => Text( | ||
637 | + '${value.counter}', //here | ||
638 | + ), | ||
639 | +), | ||
640 | +``` | ||
641 | + | ||
642 | +You may also need an instance of your controller outside of your GetBuilder, and you can use these approaches to achieve this: | ||
643 | + | ||
644 | +``` dart | ||
645 | +class Controller extends GetxController { | ||
646 | + static Controller get to => Get.find(); | ||
647 | +[...] | ||
648 | +} | ||
649 | +// on you view: | ||
650 | +GetBuilder<Controller>( | ||
651 | + init: Controller(), // use it only first time on each controller | ||
652 | + builder: (_) => Text( | ||
653 | + '${Controller.to.counter}', //here | ||
654 | + ) | ||
655 | +), | ||
656 | +``` | ||
657 | + | ||
658 | +or | ||
659 | + | ||
660 | +``` dart | ||
661 | +class Controller extends GetxController { | ||
662 | + // static Controller get to => Get.find(); // with no static get | ||
663 | +[...] | ||
664 | +} | ||
665 | +// on stateful/stateless class | ||
666 | +GetBuilder<Controller>( | ||
667 | + init: Controller(), // use it only first time on each controller | ||
668 | + builder: (_) => Text( | ||
669 | + '${Get.find<Controller>().counter}', //here | ||
670 | + ), | ||
671 | +), | ||
672 | +``` | ||
673 | + | ||
674 | +* 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: | ||
675 | + | ||
676 | +``` dart | ||
677 | +Controller controller = Controller(); | ||
678 | +[...] | ||
679 | +GetBuilder<Controller>( | ||
680 | + init: controller, //here | ||
681 | + builder: (_) => Text( | ||
682 | + '${controller.counter}', // here | ||
683 | + ), | ||
684 | +), | ||
685 | + | ||
686 | +``` | ||
687 | + | ||
688 | +### Unique IDs | ||
689 | + | ||
690 | +If you want to refine a widget's update control with GetBuilder, you can assign them unique IDs: | ||
691 | + | ||
692 | +``` dart | ||
693 | +GetBuilder<Controller>( | ||
694 | + id: 'text' | ||
695 | + init: Controller(), // use it only first time on each controller | ||
696 | + builder: (_) => Text( | ||
697 | + '${Get.find<Controller>().counter}', //here | ||
698 | + ), | ||
699 | +), | ||
700 | +``` | ||
701 | + | ||
702 | +And update it this form: | ||
703 | + | ||
704 | +``` dart | ||
705 | +update(['text']); | ||
706 | +``` | ||
707 | + | ||
708 | +You can also impose conditions for the update: | ||
709 | + | ||
710 | +``` dart | ||
711 | +update(['text'], counter < 10); | ||
712 | +``` | ||
713 | + | ||
714 | +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). | ||
715 | + | ||
716 | +## Mixing the two state managers | ||
717 | + | ||
718 | +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. | ||
719 | + | ||
720 | +Extending GetxController is important, as they have life cycles, and can "start" and "end" events in their onInit() and onClose() methods. You can use any class for this, but I strongly recommend you use the GetxController class to place your variables, whether they are observable or not. | ||
721 | + | ||
722 | +## StateMixin | ||
723 | + | ||
724 | +Another way to handle your `UI` state is use the `StateMixin<T>` . | ||
725 | +To implement it, use the `with` to add the `StateMixin<T>` | ||
726 | +to your controller which allows a T model. | ||
727 | + | ||
728 | +``` dart | ||
729 | +class Controller extends GetController with StateMixin<User>{} | ||
730 | +``` | ||
731 | + | ||
732 | +The `change()` method change the State whenever we want. | ||
733 | +Just pass the data and the status in this way: | ||
734 | + | ||
735 | +```dart | ||
736 | +change(data, status: RxStatus.success()); | ||
737 | +``` | ||
738 | + | ||
739 | +RxStatus allow these status: | ||
740 | + | ||
741 | +``` dart | ||
742 | +RxStatus.loading(); | ||
743 | +RxStatus.success(); | ||
744 | +RxStatus.empty(); | ||
745 | +RxStatus.error('message'); | ||
746 | +``` | ||
747 | + | ||
748 | +To represent it in the UI, use: | ||
749 | + | ||
750 | +```dart | ||
751 | +class OtherClass extends GetView<Controller> { | ||
752 | + @override | ||
753 | + Widget build(BuildContext context) { | ||
754 | + return Scaffold( | ||
755 | + | ||
756 | + body: controller.obx( | ||
757 | + (state)=>Text(state.name), | ||
758 | + | ||
759 | + // here you can put your custom loading indicator, but | ||
760 | + // by default would be Center(child:CircularProgressIndicator()) | ||
761 | + onLoading: CustomLoadingIndicator(), | ||
762 | + onEmpty: Text('No data found'), | ||
763 | + | ||
764 | + // here also you can set your own error widget, but by | ||
765 | + // default will be an Center(child:Text(error)) | ||
766 | + onError: (error)=>Text(error), | ||
767 | + ), | ||
768 | + ); | ||
769 | +} | ||
770 | +``` | ||
771 | + | ||
772 | +## GetBuilder vs GetX vs Obx vs MixinBuilder | ||
773 | + | ||
774 | +In a decade working with programming I was able to learn some valuable lessons. | ||
775 | + | ||
776 | +My first contact with reactive programming was so "wow, this is incredible" and in fact reactive programming is incredible. | ||
777 | +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. | ||
778 | + | ||
779 | +Reactive programming has a higher 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. | ||
780 | + | ||
781 | +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. | ||
782 | + | ||
783 | +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). | ||
784 | + | ||
785 | +However, GetBuilder is still a mechanical state manager, you need to call update() just like you would need to call Provider's notifyListeners(). | ||
786 | + | ||
787 | +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. | ||
788 | + | ||
789 | +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. |
@@ -14,7 +14,7 @@ | @@ -14,7 +14,7 @@ | ||
14 | - [SmartManagement](#smartmanagement) | 14 | - [SmartManagement](#smartmanagement) |
15 | - [How to change](#how-to-change) | 15 | - [How to change](#how-to-change) |
16 | - [SmartManagement.full](#smartmanagementfull) | 16 | - [SmartManagement.full](#smartmanagementfull) |
17 | - - [SmartManagement.onlyBuilders](#smartmanagementonlybuilders) | 17 | + - [SmartManagement.onlyBuilder](#smartmanagementonlybuilder) |
18 | - [SmartManagement.keepFactory](#smartmanagementkeepfactory) | 18 | - [SmartManagement.keepFactory](#smartmanagementkeepfactory) |
19 | - [How bindings work under the hood](#how-bindings-work-under-the-hood) | 19 | - [How bindings work under the hood](#how-bindings-work-under-the-hood) |
20 | - [Notes](#notes) | 20 | - [Notes](#notes) |
@@ -372,7 +372,7 @@ If you want to change this config (which you usually don't need) this is the way | @@ -372,7 +372,7 @@ If you want to change this config (which you usually don't need) this is the way | ||
372 | void main () { | 372 | void main () { |
373 | runApp( | 373 | runApp( |
374 | GetMaterialApp( | 374 | GetMaterialApp( |
375 | - smartManagement: SmartManagement.onlyBuilders //here | 375 | + smartManagement: SmartManagement.onlyBuilder //here |
376 | home: Home(), | 376 | home: Home(), |
377 | ) | 377 | ) |
378 | ) | 378 | ) |
@@ -383,12 +383,12 @@ void main () { | @@ -383,12 +383,12 @@ void main () { | ||
383 | 383 | ||
384 | It is the default one. Dispose classes that are not being used and were not set to be permanent. In the majority of the cases you will want to keep this config untouched. If you new to GetX then don't change this. | 384 | It is the default one. Dispose classes that are not being used and were not set to be permanent. In the majority of the cases you will want to keep this config untouched. If you new to GetX then don't change this. |
385 | 385 | ||
386 | -#### SmartManagement.onlyBuilders | 386 | +#### SmartManagement.onlyBuilder |
387 | With this option, only controllers started in `init:` or loaded into a Binding with `Get.lazyPut()` will be disposed. | 387 | With this option, only controllers started in `init:` or loaded into a Binding with `Get.lazyPut()` will be disposed. |
388 | 388 | ||
389 | If you use `Get.put()` or `Get.putAsync()` or any other approach, SmartManagement will not have permissions to exclude this dependency. | 389 | If you use `Get.put()` or `Get.putAsync()` or any other approach, SmartManagement will not have permissions to exclude this dependency. |
390 | 390 | ||
391 | -With the default behavior, even widgets instantiated with "Get.put" will be removed, unlike SmartManagement.onlyBuilders. | 391 | +With the default behavior, even widgets instantiated with "Get.put" will be removed, unlike SmartManagement.onlyBuilder. |
392 | 392 | ||
393 | #### SmartManagement.keepFactory | 393 | #### SmartManagement.keepFactory |
394 | 394 |
@@ -40,8 +40,7 @@ Pour accéder à un nouvel écran: | @@ -40,8 +40,7 @@ Pour accéder à un nouvel écran: | ||
40 | Get.to(NextScreen()); | 40 | Get.to(NextScreen()); |
41 | ``` | 41 | ``` |
42 | 42 | ||
43 | -To close snackbars, dialogs, bottomsheets, or anything you would normally close with Navigator.pop(context); | ||
44 | -Pour fermer les snackbars, dialogs, bottomsheets ou tout ce que vous fermez normalement avec Navigator.pop (context); | 43 | +Pour fermer les snackbars, dialogs, bottomsheets ou tout ce que vous fermez normalement avec Navigator.pop(context); |
45 | 44 | ||
46 | ```dart | 45 | ```dart |
47 | Get.back(); | 46 | Get.back(); |
documentation/ja_JP/dependency_management.md
0 → 100644
1 | +# 依存オブジェクト管理 | ||
2 | +- [依存オブジェクト管理](#依存オブジェクト管理) | ||
3 | + - [インスタンス化に使用するメソッド](#インスタンス化に使用するメソッド) | ||
4 | + - [Get.put()](#getput) | ||
5 | + - [Get.lazyPut()](#getlazyput) | ||
6 | + - [Get.putAsync()](#getputasync) | ||
7 | + - [Get.create()](#getcreate) | ||
8 | + - [インスタンス化したクラスを使う](#インスタンス化したクラスを使う) | ||
9 | + - [依存オブジェクトの置換](#依存オブジェクトの置換) | ||
10 | + - [各メソッドの違い](#各メソッドの違い) | ||
11 | + - [Bindings(Routeと依存オブジェクトの結束)](#Bindings(Routeと依存オブジェクトの結束)) | ||
12 | + - [Bindingsクラス](#bindingsクラス) | ||
13 | + - [BindingsBuilder](#bindingsbuilder) | ||
14 | + - [SmartManagement](#smartmanagement) | ||
15 | + - [設定の変更方法](#設定の変更方法) | ||
16 | + - [SmartManagement.full](#smartmanagementfull) | ||
17 | + - [SmartManagement.onlyBuilder](#smartmanagementonlybuilder) | ||
18 | + - [SmartManagement.keepFactory](#smartmanagementkeepfactory) | ||
19 | + - [Bindingsの仕組み](#Bindingsの仕組み) | ||
20 | + - [補足](#補足) | ||
21 | + | ||
22 | +Getにはシンプルで強力な依存オブジェクト管理機能があります。たった1行のコードで、Provider contextやinheritedWidgetを使うことなく、BlocもしくはControllerのインスタンスを取得することができます。 | ||
23 | + | ||
24 | +```dart | ||
25 | +Controller controller = Get.put(Controller()); // Controller controller = Controller() の代わりに | ||
26 | +``` | ||
27 | + | ||
28 | +UIクラスの中でControllerクラスをインスタンス化する代わりに、Getインスタンスの中でインスタンス化することで、アプリ全体でControllerを利用できるようになります。 | ||
29 | + | ||
30 | +- 注: Getの状態管理機能を使用する場合は、[Bindings](#bindings)の使用も検討してください。Bindingsを使うことでビューにControllerを結合させることができます。 | ||
31 | +- 注²: Getの依存オブジェクト管理機能は、パッケージの他の部分から独立しています。そのため、たとえばあなたのアプリが既に他の状態管理ライブラリを使用している場合(どんなものでも)、何の問題もなく2つを組み合わせることができます。 | ||
32 | + | ||
33 | +## インスタンス化に使用するメソッド | ||
34 | +Controllerを初期化するのに使用するメソッドとその設定パラメーターについてご説明します。 | ||
35 | + | ||
36 | +### Get.put() | ||
37 | + | ||
38 | +依存オブジェクトを注入するための最も基本のメソッド。たとえば、ビューで使用するControllerに使います。 | ||
39 | + | ||
40 | +```dart | ||
41 | +Get.put<SomeClass>(SomeClass()); | ||
42 | +Get.put<LoginController>(LoginController(), permanent: true); | ||
43 | +Get.put<ListItemController>(ListItemController, tag: "some unique string"); | ||
44 | +``` | ||
45 | + | ||
46 | +以下が put() を使用する際に設定できるパラメーターです。 | ||
47 | +```dart | ||
48 | +Get.put<S>( | ||
49 | + // 必須。インスタンスを保存しておきたいControllerなどを設定 | ||
50 | + // 注: "S" 型はジェネリクスで、どんな型でもOK | ||
51 | + S dependency | ||
52 | + | ||
53 | + // オプション。これは同じ型のControllerインスタンスが複数存在する場合に、 | ||
54 | + // タグIDにより識別したい場合に使用します。 | ||
55 | + // Get.find<Controller>(tag: ) でこのputで設定したインスタンスを探します。 | ||
56 | + // tag はユニークなStringである必要があります。 | ||
57 | + String tag, | ||
58 | + | ||
59 | + // オプション。デフォルトでは使用されなくなったインスタンスは破棄されますが、 | ||
60 | + // (たとえばビューが閉じられた場合など) SharedPreferencesのインスタンスなど、 | ||
61 | + // アプリ全体を通して生かしておきたい場合があるかと思います。 | ||
62 | + // その場合はこれをtrueにしてください。デフォルトはfalseです。 | ||
63 | + bool permanent = false, | ||
64 | + | ||
65 | + // オプション。テストで抽象クラスを使用した後、別クラスに置換してテストを追うことができます。 | ||
66 | + // デフォルトはfalseです。 | ||
67 | + bool overrideAbstract = false, | ||
68 | + | ||
69 | + // オプション: 依存オブジェクトを関数を使って作ることができます。 | ||
70 | + // 使用頻度は低いと思います。 | ||
71 | + InstanceBuilderCallback<S> builder, | ||
72 | +) | ||
73 | +``` | ||
74 | + | ||
75 | +### Get.lazyPut() | ||
76 | +依存オブジェクトをすぐにロードする代わりに、lazy(遅延、消極的)ロードすることができます。実際に使用されるときに初めてインスタンス化されます。計算量の多いクラスや、Bindingsを使って複数のControllerをまとめてインスタンス化したいが、その時点ではすぐにそれらを使用しないことがわかっている場合などに非常に便利です。 | ||
77 | + | ||
78 | +```dart | ||
79 | +/// この場合のApiMockは Get.find<ApiMock> を使用した時点でインスタンス化されます。 | ||
80 | +Get.lazyPut<ApiMock>(() => ApiMock()); | ||
81 | + | ||
82 | +Get.lazyPut<FirebaseAuth>( | ||
83 | + () { | ||
84 | + // 必要ならここに何かのロジック | ||
85 | + return FirebaseAuth(); | ||
86 | + }, | ||
87 | + tag: Math.random().toString(), | ||
88 | + fenix: true | ||
89 | +) | ||
90 | + | ||
91 | +Get.lazyPut<Controller>( () => Controller() ) | ||
92 | +``` | ||
93 | + | ||
94 | +これが .lazyPut で設定できるパラメーターです。 | ||
95 | +```dart | ||
96 | +Get.lazyPut<S>( | ||
97 | + // 必須。クラスSが初めてfindの対象になったときに実行されるメソッド | ||
98 | + InstanceBuilderCallback builder, | ||
99 | + | ||
100 | + // オプション。Get.put()のtagと同様に同じクラスの異なるインスタンスが必要なときに使用 | ||
101 | + // ユニークな値を指定 | ||
102 | + String tag, | ||
103 | + | ||
104 | + // オプション。"permanent" に似ていますが、使用されていないときはインスタンスが | ||
105 | + // 破棄され、再び使用するときにGetがインスタンスを再び作成する点が異なります。 | ||
106 | + // Bindings APIの "SmartManagement.keepFactory " と同じです。 | ||
107 | + // デフォルトはfalse | ||
108 | + bool fenix = false | ||
109 | + | ||
110 | +) | ||
111 | +``` | ||
112 | + | ||
113 | +### Get.putAsync() | ||
114 | +SharedPreferencesなど、非同期のインスタンスを登録したいときに使います。 | ||
115 | + | ||
116 | +```dart | ||
117 | +Get.putAsync<SharedPreferences>(() async { | ||
118 | + final prefs = await SharedPreferences.getInstance(); | ||
119 | + await prefs.setInt('counter', 12345); | ||
120 | + return prefs; | ||
121 | +}); | ||
122 | + | ||
123 | +Get.putAsync<YourAsyncClass>( () async => await YourAsyncClass() ) | ||
124 | +``` | ||
125 | + | ||
126 | +これが .putAsync で設定できるパラメーターです。 | ||
127 | +```dart | ||
128 | +Get.putAsync<S>( | ||
129 | + | ||
130 | + // 必須。クラスをインスタンス化するための非同期メソッドを指定 | ||
131 | + AsyncInstanceBuilderCallback<S> builder, | ||
132 | + | ||
133 | + // オプション。Get.put() と同じです。同じクラスの異なるインスタンスを作りたいときに使用 | ||
134 | + // ユニークな値を指定 | ||
135 | + String tag, | ||
136 | + | ||
137 | + // オプション。Get.put() と同じです。アプリ全体を通して生かしておきたい場合に使用 | ||
138 | + // デフォルトはfalse | ||
139 | + bool permanent = false | ||
140 | +) | ||
141 | +``` | ||
142 | + | ||
143 | +### Get.create() | ||
144 | + | ||
145 | +これは使いどころに迷うかもしれません。他のものとの違いなど詳細な説明は「[各メソッドの違い](#各メソッドの違い)」のセクションをご一読ください。 | ||
146 | + | ||
147 | +```dart | ||
148 | +Get.Create<SomeClass>(() => SomeClass()); | ||
149 | +Get.Create<LoginController>(() => LoginController()); | ||
150 | +``` | ||
151 | + | ||
152 | +これが .create で設定できるパラメーターです。 | ||
153 | + | ||
154 | +```dart | ||
155 | +Get.create<S>( | ||
156 | + // 必須。Get.find() が呼ばれるたびにインスタンスがこの関数で新たに組み立てられる。 | ||
157 | + // 例: Get.create<YourClass>(() => YourClass()) | ||
158 | + FcBuilderFunc<S> builder, | ||
159 | + | ||
160 | + // オプション。Get.put() のtagと同様で、同じクラスによる | ||
161 | + // 複数インスタンスを扱うときに使用します。 | ||
162 | + // リストのアイテムにそれぞれコントローラが必要な場合に便利です。 | ||
163 | + // 文字列はユニークである必要があります。 | ||
164 | + String name, | ||
165 | + | ||
166 | + // オプション。Get.put() と同様アプリ全体でインスタンスを維持するときに使用。 | ||
167 | + // 唯一の違いは Get.create() のpermanentはデフォルトでtrueということだけです。 | ||
168 | + bool permanent = true | ||
169 | +``` | ||
170 | + | ||
171 | +## インスタンス化したクラスを使う | ||
172 | + | ||
173 | +いくつかのRouteを渡り歩いた後、以前のControllerに残してきたデータが必要になったとしたら、Provider や Get_it と組み合わせる必要がありますよね?Getの場合は違います。GetにControllerの「検索」を依頼するだけで追加の依存オブジェクトの注入は必要ありません。 | ||
174 | + | ||
175 | +```dart | ||
176 | +final controller = Get.find<Controller>(); | ||
177 | +// もしくは | ||
178 | +Controller controller = Get.find(); | ||
179 | + | ||
180 | +// マジックみたいですよね。でも実際にGetはControllerのインスタンスを探して届けてくれます。 | ||
181 | +// 100万ものControllerをインスタンス化していても、Getは常に正しいControllerを探してくれます。 | ||
182 | +``` | ||
183 | + | ||
184 | +そしてそのControllerが以前取得したデータをあなたは復元することができます。 | ||
185 | + | ||
186 | +```dart | ||
187 | +Text(controller.textFromApi); | ||
188 | +``` | ||
189 | + | ||
190 | +戻り値は通常のクラスなので、そのクラスで可能なことは何でもできます。 | ||
191 | +```dart | ||
192 | +int count = Get.find<SharedPreferences>().getInt('counter'); | ||
193 | +print(count); // 出力: 12345 | ||
194 | +``` | ||
195 | + | ||
196 | +インスタンスを明示的に削除したい場合はこのようにしてください。 | ||
197 | + | ||
198 | +```dart | ||
199 | +Get.delete<Controller>(); // 通常であれば、GetXは未使用Controllerを自動削除するので、この作業は必要ありません。 | ||
200 | +``` | ||
201 | + | ||
202 | +## 依存オブジェクトの置換 | ||
203 | + | ||
204 | +注入されている依存オブジェクトは `replace` または `lazyReplace` メソッドを使って、子クラスなど関連クラスのインスタンスに置き換えることができます。これは元の抽象クラスを型指定することで取得することができます。 | ||
205 | + | ||
206 | +```dart | ||
207 | +abstract class BaseClass {} | ||
208 | +class ParentClass extends BaseClass {} | ||
209 | + | ||
210 | +class ChildClass extends ParentClass { | ||
211 | + bool isChild = true; | ||
212 | +} | ||
213 | + | ||
214 | + | ||
215 | +Get.put<BaseClass>(ParentClass()); | ||
216 | + | ||
217 | +Get.replace<BaseClass>(ChildClass()); | ||
218 | + | ||
219 | +final instance = Get.find<BaseClass>(); | ||
220 | +print(instance is ChildClass); //true | ||
221 | + | ||
222 | + | ||
223 | +class OtherClass extends BaseClass {} | ||
224 | +Get.lazyReplace<BaseClass>(() => OtherClass()); | ||
225 | + | ||
226 | +final instance = Get.find<BaseClass>(); | ||
227 | +print(instance is ChildClass); // false | ||
228 | +print(instance is OtherClass); //true | ||
229 | +``` | ||
230 | + | ||
231 | +## Differences between methods | ||
232 | + | ||
233 | +まずは Get.lazyPut の `fenix` プロパティと、他メソッドの `permanent` プロパティの違いについてご説明します。 | ||
234 | + | ||
235 | +`permanent` と `fenix` の根本的な違いは、インスタンスをどのように保持したいかという点に尽きます。 | ||
236 | + | ||
237 | +しつこいようですが、GetXでは使われていないインスタンスは削除されるのがデフォルトの動作です。 | ||
238 | +これはもし画面AがController A、画面BがController Bを持っている場合において、画面Bに遷移するときに画面Aをスタックから削除した場合(`Get.off()` や `Get.offNamed()` を使うなどして)、Controller Aは消えてなくなるということです。 | ||
239 | + | ||
240 | +しかし Get.put() する際に `permanent:true` としていれば、Controller Aはこの画面削除により失われることはありません。これはアプリケーション全体を通してControllerを残しておきたい場合に大変便利です。 | ||
241 | + | ||
242 | +一方の `fenix` は、画面削除に伴っていったんはControllerが消去されますが、再び使いたいと思ったときに復活させることができます。つまり基本的には未使用の Controller / サービス / その他クラス は消去されますが、必要なときは新しいインスタンスを「燃えカス」から作り直すことができるのです。 | ||
243 | + | ||
244 | +各メソッドを使用する際のプロセスの違いをご説明します。 | ||
245 | + | ||
246 | +- Get.put と Get.putAsync はインスタンスを作成して初期化するプロセスは同じですが、後者は非同期メソッドを使用するという違いがあります。この2つのメソッドは内部に保有するメソッド `insert` に `permanent: false` と `isSingleton: true` という引数を渡して、メモリに直接インスタンスを挿入します (この isSingleton が行っていることは、依存オブジェクトを `dependency` と `builder` プロパティのどちらから拝借するかを判断することだけです)。その後に `Get.find()` が呼ばれると、メモリ上にあるインスタンスを即座に初期化するというプロセスをたどります。 | ||
247 | + | ||
248 | +- Get.create はその名の通り、依存オブジェクトを「クリエイト」します。Get.put() と同様に内部メソッドである `insert` を呼び出してインスタンス化します。違いは `permanent: true` で `isSingleton: false` である点です (依存オブジェクトを「クリエイト」しているため、シングルトンにはなりません。それが false になっている理由です)。また `permanent: true` となっているので、デフォルトでは画面の破棄などでインスタンスを失わないというメリットがあります。また `Get.find()` はすぐに呼ばれず、画面内で実際に使用されてから呼ばれます。これは `permanent` の特性を活かすための設計ですが、それゆえ `Get.create()` は共有しないけど破棄もされないインスタンスを作成する目的で作られたと言えます。たとえば、ListViewの中のボタンアイテムに使うControllerインスタンスのように、そのリスト内でしか使わないけどリストアイテムごとに固有のインスタンスが必要なケースなどが考えられます。そのため、Get.create は GetWidget との併用がマストです。 | ||
249 | + | ||
250 | +- Get.lazyPut は初期化をlazy(遅延、消極的)に行います。実行されるとインスタンスは作成されますが、すぐに使用できるように初期化はされず、待機状態になります。また他のメソッドと異なり `insert` メソッドは呼び出されません。その代わり、インスタンスはメモリの別の部分に挿入されます。この部分を「ファクトリー」と呼ぶことにしましょう。「ファクトリー」は、そのインスタンスが再生成できるかどうかを決める役割を持っています。これは後で使う予定のものを、現在進行形で使われているものと混ざらないようにするための工夫です。ここで `fenix` によるマジックが登場します。デフォルトの `fenix: false` のままにしており、かつ `SmartManagement` が `keepFactory` ではない場合において `Get.find` を使用すると、インスタンスは「ファクトリー」から共有メモリ領域に移動します。その直後にインスタンスは「ファクトリー」から削除されます。しかし `fenix: true` としていた場合、インスタンスは「ファクトリー」に残るため、共有メモリ領域から削除されても再び呼び出すことができるのです。 | ||
251 | + | ||
252 | +## Bindings(Routeと依存オブジェクトの結束) | ||
253 | + | ||
254 | +このパッケージの一番の差別化要素は、Route管理 / 状態管理 / 依存オブジェクト管理 を統合したことにあると思っています。 | ||
255 | +スタックからRouteが削除されれば、関係するController、変数、オブジェクトのインスタンスがすべてメモリから削除されます。たとえばStreamやTimerを使用している場合も同様ですので、開発者は何も心配する必要はありません。 | ||
256 | +Getはバージョン2.10からBindings APIをフル実装しました。 | ||
257 | +Bindingsを使用すれば init でControllerを起動する必要はありません。またControllerの型を指定する必要もありません。Controllerやサービスは各々適切な場所で起動することができるようになりました。 | ||
258 | +Bindingsは依存オブジェクトの注入をビューから切り離すことができるクラスです。それに加え、状態と依存オブジェクトの管理機能をRouteに「結束(bind)」してくれます。 | ||
259 | +これによりGetは、あるControllerが使用されたときにどの画面UIが表示されているかを知ることができます。つまり、そのControllerをどのタイミングでどう処分するかを判断することができるということです。 | ||
260 | +さらにBindingsでは SmartManager の制御により、依存オブジェクトをどのタイミング(スタックからRouteを削除したときか、それに依存するWidgetを表示したときか、いずれでもないか)で整理するかを設定することができます。インテリジェントな依存オブジェクトの自動管理機能を持ちつつ、自分の好きなように設定できるのです。 | ||
261 | + | ||
262 | +### Bindingsクラス | ||
263 | + | ||
264 | +- Bindings機能を実装したクラスを作成することができます。 | ||
265 | + | ||
266 | +```dart | ||
267 | +class HomeBinding implements Bindings {} | ||
268 | +``` | ||
269 | + | ||
270 | +"dependencies" メソッドをオーバーライドするようIDEに自動で指摘されます。表示をクリックしてメソッドを override し、そのRoute内で使用するすべてのクラスを挿入してください。 | ||
271 | + | ||
272 | +```dart | ||
273 | +class HomeBinding implements Bindings { | ||
274 | + @override | ||
275 | + void dependencies() { | ||
276 | + Get.lazyPut<HomeController>(() => HomeController()); | ||
277 | + Get.put<Service>(()=> Api()); | ||
278 | + } | ||
279 | +} | ||
280 | + | ||
281 | +class DetailsBinding implements Bindings { | ||
282 | + @override | ||
283 | + void dependencies() { | ||
284 | + Get.lazyPut<DetailsController>(() => DetailsController()); | ||
285 | + } | ||
286 | +} | ||
287 | +``` | ||
288 | + | ||
289 | +Bindingsを設定したら、このクラスが Route管理 / 依存オブジェクト管理 / 状態管理 を互いに接続する目的で使用されるものだということをRouteに知らせてあげます。 | ||
290 | + | ||
291 | +- 名前付きRouteを使う場合 | ||
292 | + | ||
293 | +```dart | ||
294 | +getPages: [ | ||
295 | + GetPage( | ||
296 | + name: '/', | ||
297 | + page: () => HomeView(), | ||
298 | + binding: HomeBinding(), | ||
299 | + ), | ||
300 | + GetPage( | ||
301 | + name: '/details', | ||
302 | + page: () => DetailsView(), | ||
303 | + binding: DetailsBinding(), | ||
304 | + ), | ||
305 | +]; | ||
306 | +``` | ||
307 | + | ||
308 | +- 通常のRouteを使う場合 | ||
309 | + | ||
310 | +```dart | ||
311 | +Get.to(Home(), binding: HomeBinding()); | ||
312 | +Get.to(DetailsView(), binding: DetailsBinding()) | ||
313 | +``` | ||
314 | + | ||
315 | +これでアプリケーションのメモリ管理を気にする必要がなくなります。Getがすべてやってくれます。 | ||
316 | + | ||
317 | +BindingsクラスはRouteの呼び出しと同時に呼び出されます。また、すべてに共通の依存オブジェクトを挿入するためには GetMaterialApp の initialBinding プロパティを使用してください。 | ||
318 | + | ||
319 | +```dart | ||
320 | +GetMaterialApp( | ||
321 | + initialBinding: SampleBind(), | ||
322 | + home: Home(), | ||
323 | +); | ||
324 | +``` | ||
325 | + | ||
326 | +### BindingsBuilder | ||
327 | + | ||
328 | +Bindingsを作成する一般的な方法は Bindings を実装したクラスを作成することですが、`BindingsBuilder` コールバックを使う方法もあります。 | ||
329 | + | ||
330 | +Example: | ||
331 | + | ||
332 | +```dart | ||
333 | +getPages: [ | ||
334 | + GetPage( | ||
335 | + name: '/', | ||
336 | + page: () => HomeView(), | ||
337 | + binding: BindingsBuilder(() { | ||
338 | + Get.lazyPut<ControllerX>(() => ControllerX()); | ||
339 | + Get.put<Service>(()=> Api()); | ||
340 | + }), | ||
341 | + ), | ||
342 | + GetPage( | ||
343 | + name: '/details', | ||
344 | + page: () => DetailsView(), | ||
345 | + binding: BindingsBuilder(() { | ||
346 | + Get.lazyPut<DetailsController>(() => DetailsController()); | ||
347 | + }), | ||
348 | + ), | ||
349 | +]; | ||
350 | +``` | ||
351 | + | ||
352 | +この方法ならRouteごとにBindingsクラスを作る必要はありません。 | ||
353 | + | ||
354 | +どちらの方法でも効果は変わりませんのでお好みの方法を使ってください。 | ||
355 | + | ||
356 | +### SmartManagement | ||
357 | + | ||
358 | +エラーが発生してControllerを使用するWidgetが正しく破棄されなかった場合でも、Controllerが未使用になればGetXはデフォルトの動作通りそれをメモリから削除します。 | ||
359 | +これがいわゆる依存オブジェクト管理機能の `full` モードと呼ばれるものです。 | ||
360 | +しかしもしGetXによるオブジェクト破棄の方法をコントロールしたい場合は、`SmartManagement`クラスを使って設定してください。 | ||
361 | + | ||
362 | +#### 設定の変更方法 | ||
363 | + | ||
364 | +この設定は通常変更する必要はありませんが、変更されたい場合はこのようにしてください。 | ||
365 | + | ||
366 | +```dart | ||
367 | +void main () { | ||
368 | + runApp( | ||
369 | + GetMaterialApp( | ||
370 | + smartManagement: SmartManagement.onlyBuilder // ここで設定 | ||
371 | + home: Home(), | ||
372 | + ) | ||
373 | + ) | ||
374 | +} | ||
375 | +``` | ||
376 | + | ||
377 | +#### SmartManagement.full | ||
378 | + | ||
379 | +これがデフォルトのモードです。使用されていない、かつ `permanent: true` が設定されていないオブジェクトを自動で破棄してくれます。特殊な事情がない限り、この設定は触らない方がいいでしょう。GetXを使って間がない場合は特に。 | ||
380 | + | ||
381 | +#### SmartManagement.onlyBuilder | ||
382 | +`init:` もしくはBindings内で `Get.lazyPut()` により設定したビルダー製のオブジェクトだけを破棄するモードです。 | ||
383 | + | ||
384 | +もしそれが `Get.put()` や `Get.putAsync()` などのアプローチで生成したオブジェクトだとしたら、SmartManagement は勝手にメモリから除外することはできません。 | ||
385 | + | ||
386 | +それに対してデフォルトのモードでは `Get.put()` で生成したオブジェクトも破棄します。 | ||
387 | + | ||
388 | +#### SmartManagement.keepFactory | ||
389 | + | ||
390 | +SmartManagement.full と同じように、オブジェクトが使用されていない状態になれば破棄します。ただし、前述の「ファクトリー」に存在するものだけは残します。つまりそのインスタンスが再び必要になった際は依存オブジェクトを再度生成するということです。 | ||
391 | + | ||
392 | +### Bindingsの仕組み | ||
393 | +Bindingsは「一過性のファクトリー」のようなものを作成します。これはそのRouteに画面遷移した瞬間に作成され、そこから画面移動するアニメーションが発生した瞬間に破棄されます。 | ||
394 | +この動作は非常に高速で行われるので、アナライザーでは捕捉できないほどです。 | ||
395 | +再び元の画面に戻ると新しい「一過性のファクトリー」が呼び出されます。そのためこれは SmartManagement.keepFactory を使用するよりも多くの場合好ましいですが、Bindingsを作成したくない場合やすべての依存オブジェクトを同じBindingsに持っておきたい場合は SmartManagement.keepFactory を使うといいでしょう。 | ||
396 | +ファクトリーのメモリ使用量は少なく、インスタンスを保持することはありません。その代わりにそのクラスのインスタンスを形作る関数を保持します。 | ||
397 | +メモリコストは非常に低いのですが、最小リソースで最大パフォーマンスを得ることが目的のGetではデフォルトでファクトリーを削除します。 | ||
398 | +どちらか都合に合う方ををお使いいただければと思います。 | ||
399 | + | ||
400 | +## 補足 | ||
401 | + | ||
402 | +- 複数のBindingsを使う場合は SmartManagement.keepFactory は**使わない**でください。これは Bindings を使わないケースや、GetMaterialAppのinitialBindingに設定された単独のBindingと一緒に使うケースを想定されて作られました。 | ||
403 | + | ||
404 | +- Bindingsを使うことは必須ではありません。`Get.put()` と `Get.find()` だけでも全く問題ありません。 | ||
405 | +ただし、サービスやその他抽象度の高いクラスをアプリに取り入れる場合はコード整理のために使うことをおすすめします。 |
-
Please register or login to post a comment