Committed by
GitHub
Merge pull request #556 from roipeker/master
fixes for Controller and improvements with extensions and BindingBuilder.
Showing
6 changed files
with
105 additions
and
17 deletions
1 | +import 'package:get/get.dart'; | ||
2 | + | ||
1 | /// [Bindings] should be extended or implemented. | 3 | /// [Bindings] should be extended or implemented. |
2 | /// When using [GetMaterialApp], all [GetPage]s and navigation methods (like Get.to()) | 4 | /// When using [GetMaterialApp], all [GetPage]s and navigation methods (like Get.to()) |
3 | /// have a [binding] property that takes an instance of Bindings to manage the | 5 | /// have a [binding] property that takes an instance of Bindings to manage the |
@@ -16,11 +18,27 @@ abstract class Bindings { | @@ -16,11 +18,27 @@ abstract class Bindings { | ||
16 | /// page: () => Home(), | 18 | /// page: () => Home(), |
17 | /// binding: BindingsBuilder(() => Get.put(HomeController())), | 19 | /// binding: BindingsBuilder(() => Get.put(HomeController())), |
18 | /// ), | 20 | /// ), |
19 | -/// ```` | ||
20 | -class BindingsBuilder extends Bindings { | 21 | +/// ``` |
22 | +class BindingsBuilder<T> extends Bindings { | ||
21 | /// Register your dependencies in the [builder] callback. | 23 | /// Register your dependencies in the [builder] callback. |
22 | final void Function() builder; | 24 | final void Function() builder; |
23 | 25 | ||
26 | + /// Shortcut to register 1 Controller with Get.put(). | ||
27 | + /// | ||
28 | + /// Sample: | ||
29 | + /// ``` | ||
30 | + /// GetPage( | ||
31 | + /// name: '/', | ||
32 | + /// page: () => Home(), | ||
33 | + /// binding: BindingsBuilder.put(() => HomeController()), | ||
34 | + /// ), | ||
35 | + /// ``` | ||
36 | + factory BindingsBuilder.put(InstanceBuilderCallback<T> builder, | ||
37 | + {String tag, bool permanent = false}) { | ||
38 | + return BindingsBuilder(() => GetInstance() | ||
39 | + .put<T>(null, tag: tag, permanent: permanent, builder: builder)); | ||
40 | + } | ||
41 | + | ||
24 | BindingsBuilder(this.builder); | 42 | BindingsBuilder(this.builder); |
25 | 43 | ||
26 | @override | 44 | @override |
1 | import 'dart:async'; | 1 | import 'dart:async'; |
2 | + | ||
2 | import 'package:flutter/scheduler.dart'; | 3 | import 'package:flutter/scheduler.dart'; |
4 | + | ||
3 | import '../rx_typedefs/rx_typedefs.dart'; | 5 | import '../rx_typedefs/rx_typedefs.dart'; |
4 | 6 | ||
5 | /// This class is the foundation for all reactive (Rx) classes that makes Get | 7 | /// This class is the foundation for all reactive (Rx) classes that makes Get |
@@ -28,8 +30,9 @@ abstract class RxInterface<T> { | @@ -28,8 +30,9 @@ abstract class RxInterface<T> { | ||
28 | } | 30 | } |
29 | 31 | ||
30 | /// Unlike GetxController, which serves to control events on each of its pages, | 32 | /// Unlike GetxController, which serves to control events on each of its pages, |
31 | -/// GetxService is not automatically disposed. It is ideal for situations where, | ||
32 | -/// once started, that service will remain in memory, such as Auth control for example. | 33 | +/// GetxService is not automatically disposed (nor can be removed with Get.delete()). |
34 | +/// It is ideal for situations where, once started, that service will remain in memory, | ||
35 | +/// such as Auth control for example. Only way to remove it is Get.reset(). | ||
33 | abstract class GetxService extends DisposableInterface {} | 36 | abstract class GetxService extends DisposableInterface {} |
34 | 37 | ||
35 | /// Special callable class to keep the contract of a regular method, and avoid | 38 | /// Special callable class to keep the contract of a regular method, and avoid |
@@ -66,8 +69,8 @@ abstract class DisposableInterface { | @@ -66,8 +69,8 @@ abstract class DisposableInterface { | ||
66 | } | 69 | } |
67 | 70 | ||
68 | /// Called immediately after the widget is allocated in memory. | 71 | /// Called immediately after the widget is allocated in memory. |
69 | - /// You might use this initialize something for the controller. | ||
70 | - void onInit() async {} | 72 | + /// You might use this to initialize something for the controller. |
73 | + void onInit() {} | ||
71 | 74 | ||
72 | /// Called 1 frame after onInit(). It is the perfect place to enter navigation events, | 75 | /// Called 1 frame after onInit(). It is the perfect place to enter navigation events, |
73 | /// like snackbar, dialogs, or a new route, or async request. | 76 | /// like snackbar, dialogs, or a new route, or async request. |
@@ -78,7 +81,7 @@ abstract class DisposableInterface { | @@ -78,7 +81,7 @@ abstract class DisposableInterface { | ||
78 | /// Or dispose objects that can potentially create some memory leaks, | 81 | /// Or dispose objects that can potentially create some memory leaks, |
79 | /// like TextEditingControllers, AnimationControllers. | 82 | /// like TextEditingControllers, AnimationControllers. |
80 | /// Might be useful as well to persist some data on disk. | 83 | /// Might be useful as well to persist some data on disk. |
81 | - Future<void> onClose() async {} | 84 | + void onClose() async {} |
82 | } | 85 | } |
83 | 86 | ||
84 | /// Used like [SingleTickerProviderMixin] but only with Get Controllers. | 87 | /// Used like [SingleTickerProviderMixin] but only with Get Controllers. |
1 | import 'dart:collection'; | 1 | import 'dart:collection'; |
2 | + | ||
2 | import 'package:flutter/widgets.dart'; | 3 | import 'package:flutter/widgets.dart'; |
3 | import 'package:get/src/instance/get_instance.dart'; | 4 | import 'package:get/src/instance/get_instance.dart'; |
4 | import 'package:get/src/navigation/root/smart_management.dart'; | 5 | import 'package:get/src/navigation/root/smart_management.dart'; |
@@ -40,15 +41,6 @@ class GetxController extends DisposableInterface { | @@ -40,15 +41,6 @@ class GetxController extends DisposableInterface { | ||
40 | } | 41 | } |
41 | 42 | ||
42 | void disposeKey(String key) => _updatersIds.remove(key); | 43 | void disposeKey(String key) => _updatersIds.remove(key); |
43 | - | ||
44 | - @override | ||
45 | - void onInit() async {} | ||
46 | - | ||
47 | - @override | ||
48 | - void onReady() async {} | ||
49 | - | ||
50 | - @override | ||
51 | - Future<void> onClose() async {} | ||
52 | } | 44 | } |
53 | 45 | ||
54 | class GetBuilder<T extends GetxController> extends StatefulWidget { | 46 | class GetBuilder<T extends GetxController> extends StatefulWidget { |
@@ -160,7 +152,9 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>> { | @@ -160,7 +152,9 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>> { | ||
160 | Widget build(BuildContext context) => widget.builder(controller); | 152 | Widget build(BuildContext context) => widget.builder(controller); |
161 | } | 153 | } |
162 | 154 | ||
163 | -/// This is a experimental feature | 155 | +/// This is a experimental feature. |
156 | +/// Meant to be used with SimpleBuilder, it auto-registers the variable | ||
157 | +/// like Rx() does with Obx(). | ||
164 | class Value<T> extends GetxController { | 158 | class Value<T> extends GetxController { |
165 | Value([this._value]); | 159 | Value([this._value]); |
166 | T _value; | 160 | T _value; |
1 | +import 'dart:ui'; | ||
2 | + | ||
3 | +/// Duration utilities. | ||
4 | +extension GetDurationUtils on Duration { | ||
5 | + /// Utility to delay some callback (or code execution). | ||
6 | + /// | ||
7 | + /// Sample: | ||
8 | + /// ``` | ||
9 | + /// void main() async { | ||
10 | + /// final _delay = 3.seconds; | ||
11 | + /// print('+ wait $_delay'); | ||
12 | + /// await _delay.delay(); | ||
13 | + /// print('- finish wait $_delay'); | ||
14 | + /// print('+ callback in 700ms'); | ||
15 | + /// await 0.7.seconds.delay(() { | ||
16 | + /// } | ||
17 | + ///``` | ||
18 | + Future delay([VoidCallback callback]) async => Future.delayed(this, callback); | ||
19 | +} |
1 | export 'context_extensions.dart'; | 1 | export 'context_extensions.dart'; |
2 | export 'double_extensions.dart'; | 2 | export 'double_extensions.dart'; |
3 | +export 'duration_extensions.dart'; | ||
3 | export 'dynamic_extensions.dart'; | 4 | export 'dynamic_extensions.dart'; |
4 | export 'num_extensions.dart'; | 5 | export 'num_extensions.dart'; |
5 | export 'string_extensions.dart'; | 6 | export 'string_extensions.dart'; |
1 | +import 'dart:ui'; | ||
2 | + | ||
1 | import '../regex/get_utils.dart'; | 3 | import '../regex/get_utils.dart'; |
2 | 4 | ||
3 | extension GetNumUtils on num { | 5 | extension GetNumUtils on num { |
4 | bool isLowerThan(num b) => GetUtils.isLowerThan(this, b); | 6 | bool isLowerThan(num b) => GetUtils.isLowerThan(this, b); |
5 | bool isGreaterThan(num b) => GetUtils.isGreaterThan(this, b); | 7 | bool isGreaterThan(num b) => GetUtils.isGreaterThan(this, b); |
6 | bool isEqual(num b) => GetUtils.isEqual(this, b); | 8 | bool isEqual(num b) => GetUtils.isEqual(this, b); |
9 | + | ||
10 | + /// Utility to delay some callback (or code execution). | ||
11 | + /// TODO: Add a separated implementation of delay() with the ability | ||
12 | + /// to stop it. | ||
13 | + /// | ||
14 | + /// Sample: | ||
15 | + /// ``` | ||
16 | + /// void main() async { | ||
17 | + /// print('+ wait for 2 seconds'); | ||
18 | + /// await 2.delay(); | ||
19 | + /// print('- 2 seconds completed'); | ||
20 | + /// print('+ callback in 1.2sec'); | ||
21 | + /// 1.delay(() => print('- 1.2sec callback called')); | ||
22 | + /// print('currently running callback 1.2sec'); | ||
23 | + /// } | ||
24 | + ///``` | ||
25 | + Future delay([VoidCallback callback]) async => | ||
26 | + Future.delayed(Duration(milliseconds: (this * 1000).round()), callback); | ||
27 | + | ||
28 | + /// Easy way to make Durations from numbers. | ||
29 | + /// | ||
30 | + /// Sample: | ||
31 | + /// ``` | ||
32 | + /// print(1.seconds + 200.milliseconds); | ||
33 | + /// print(1.hours + 30.minutes); | ||
34 | + /// print(1.5.hours); | ||
35 | + ///``` | ||
36 | + Duration get milliseconds => Duration(microseconds: (this * 1000).round()); | ||
37 | + Duration get seconds => Duration(milliseconds: (this * 1000).round()); | ||
38 | + Duration get minutes => | ||
39 | + Duration(seconds: (this * Duration.secondsPerMinute).round()); | ||
40 | + Duration get hours => | ||
41 | + Duration(minutes: (this * Duration.minutesPerHour).round()); | ||
42 | + Duration get days => Duration(hours: (this * Duration.hoursPerDay).round()); | ||
43 | + | ||
44 | +//final _delayMaps = <Function, Future>{}; | ||
45 | +// TODO: create a proper Future and control the Timer. | ||
46 | +// Future delay([double seconds = 0, VoidCallback callback]) async { | ||
47 | +// final ms = (seconds * 1000).round(); | ||
48 | +// return Future.delayed(Duration(milliseconds: ms), callback); | ||
49 | +// return _delayMaps[callback]; | ||
50 | +// } | ||
51 | +//killDelay(VoidCallback callback) { | ||
52 | +// if (_delayMaps.containsKey(callback)) { | ||
53 | +// _delayMaps[callback]?.timeout(Duration.zero, onTimeout: () { | ||
54 | +// print('callbacl eliminado!'); | ||
55 | +// }); | ||
56 | +// _delayMaps.remove(callback); | ||
57 | +// } | ||
58 | +//} | ||
59 | + | ||
7 | } | 60 | } |
-
Please register or login to post a comment