roi peker

fixes, bindings and extensions

- fixes the onClose(), onInit() async override in GetxController
- added a shortcut `BindingsBuilder.put()` to register a single Controller in a view.
- fixes some doc comments
- added new extensions for `num` and `duration` to apply delayed calls.
import 'package:get/get.dart';
/// [Bindings] should be extended or implemented.
/// When using [GetMaterialApp], all [GetPage]s and navigation methods (like Get.to())
/// have a [binding] property that takes an instance of Bindings to manage the
... ... @@ -16,11 +18,27 @@ abstract class Bindings {
/// page: () => Home(),
/// binding: BindingsBuilder(() => Get.put(HomeController())),
/// ),
/// ````
class BindingsBuilder extends Bindings {
/// ```
class BindingsBuilder<T> extends Bindings {
/// Register your dependencies in the [builder] callback.
final void Function() builder;
/// Shortcut to register 1 Controller with Get.put().
///
/// Sample:
/// ```
/// GetPage(
/// name: '/',
/// page: () => Home(),
/// binding: BindingsBuilder.put(() => HomeController()),
/// ),
/// ```
factory BindingsBuilder.put(InstanceBuilderCallback<T> builder,
{String tag, bool permanent = false}) {
return BindingsBuilder(() => GetInstance()
.put<T>(null, tag: tag, permanent: permanent, builder: builder));
}
BindingsBuilder(this.builder);
@override
... ...
import 'dart:async';
import 'package:flutter/scheduler.dart';
import '../rx_typedefs/rx_typedefs.dart';
/// This class is the foundation for all reactive (Rx) classes that makes Get
... ... @@ -28,8 +30,9 @@ abstract class RxInterface<T> {
}
/// Unlike GetxController, which serves to control events on each of its pages,
/// GetxService is not automatically disposed. It is ideal for situations where,
/// once started, that service will remain in memory, such as Auth control for example.
/// GetxService is not automatically disposed (nor can be removed with Get.delete()).
/// It is ideal for situations where, once started, that service will remain in memory,
/// such as Auth control for example. Only way to remove it is Get.reset().
abstract class GetxService extends DisposableInterface {}
/// Special callable class to keep the contract of a regular method, and avoid
... ... @@ -66,8 +69,8 @@ abstract class DisposableInterface {
}
/// Called immediately after the widget is allocated in memory.
/// You might use this initialize something for the controller.
void onInit() async {}
/// You might use this to initialize something for the controller.
void onInit() {}
/// Called 1 frame after onInit(). It is the perfect place to enter navigation events,
/// like snackbar, dialogs, or a new route, or async request.
... ... @@ -78,7 +81,7 @@ abstract class DisposableInterface {
/// Or dispose objects that can potentially create some memory leaks,
/// like TextEditingControllers, AnimationControllers.
/// Might be useful as well to persist some data on disk.
Future<void> onClose() async {}
void onClose() async {}
}
/// Used like [SingleTickerProviderMixin] but only with Get Controllers.
... ...
import 'dart:collection';
import 'package:flutter/widgets.dart';
import 'package:get/src/instance/get_instance.dart';
import 'package:get/src/navigation/root/smart_management.dart';
... ... @@ -40,15 +41,6 @@ class GetxController extends DisposableInterface {
}
void disposeKey(String key) => _updatersIds.remove(key);
@override
void onInit() async {}
@override
void onReady() async {}
@override
Future<void> onClose() async {}
}
class GetBuilder<T extends GetxController> extends StatefulWidget {
... ... @@ -160,7 +152,9 @@ class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>> {
Widget build(BuildContext context) => widget.builder(controller);
}
/// This is a experimental feature
/// This is a experimental feature.
/// Meant to be used with SimpleBuilder, it auto-registers the variable
/// like Rx() does with Obx().
class Value<T> extends GetxController {
Value([this._value]);
T _value;
... ...
import 'dart:ui';
/// Duration utilities.
extension GetDurationUtils on Duration {
/// Utility to delay some callback (or code execution).
///
/// Sample:
/// ```
/// void main() async {
/// final _delay = 3.seconds;
/// print('+ wait $_delay');
/// await _delay.delay();
/// print('- finish wait $_delay');
/// print('+ callback in 700ms');
/// await 0.7.seconds.delay(() {
/// }
///```
Future delay([VoidCallback callback]) async => Future.delayed(this, callback);
}
... ...
export 'context_extensions.dart';
export 'double_extensions.dart';
export 'duration_extensions.dart';
export 'dynamic_extensions.dart';
export 'num_extensions.dart';
export 'string_extensions.dart';
... ...
import 'dart:ui';
import '../regex/get_utils.dart';
extension GetNumUtils on num {
bool isLowerThan(num b) => GetUtils.isLowerThan(this, b);
bool isGreaterThan(num b) => GetUtils.isGreaterThan(this, b);
bool isEqual(num b) => GetUtils.isEqual(this, b);
/// Utility to delay some callback (or code execution).
/// TODO: Add a separated implementation of delay() with the ability
/// to stop it.
///
/// Sample:
/// ```
/// void main() async {
/// print('+ wait for 2 seconds');
/// await 2.delay();
/// print('- 2 seconds completed');
/// print('+ callback in 1.2sec');
/// 1.delay(() => print('- 1.2sec callback called'));
/// print('currently running callback 1.2sec');
/// }
///```
Future delay([VoidCallback callback]) async =>
Future.delayed(Duration(milliseconds: (this * 1000).round()), callback);
/// Easy way to make Durations from numbers.
///
/// Sample:
/// ```
/// print(1.seconds + 200.milliseconds);
/// print(1.hours + 30.minutes);
/// print(1.5.hours);
///```
Duration get milliseconds => Duration(microseconds: (this * 1000).round());
Duration get seconds => Duration(milliseconds: (this * 1000).round());
Duration get minutes =>
Duration(seconds: (this * Duration.secondsPerMinute).round());
Duration get hours =>
Duration(minutes: (this * Duration.minutesPerHour).round());
Duration get days => Duration(hours: (this * Duration.hoursPerDay).round());
//final _delayMaps = <Function, Future>{};
// TODO: create a proper Future and control the Timer.
// Future delay([double seconds = 0, VoidCallback callback]) async {
// final ms = (seconds * 1000).round();
// return Future.delayed(Duration(milliseconds: ms), callback);
// return _delayMaps[callback];
// }
//killDelay(VoidCallback callback) {
// if (_delayMaps.containsKey(callback)) {
// _delayMaps[callback]?.timeout(Duration.zero, onTimeout: () {
// print('callbacl eliminado!');
// });
// _delayMaps.remove(callback);
// }
//}
}
... ...