Jonatas

fix GetWidget

import 'package:flutter/widgets.dart';
import '../../../get_instance/src/get_instance.dart';
import '../rx_flutter/rx_disposable.dart';
import '../../../instance_manager.dart';
import 'get_widget_cache.dart';
/// GetView is a great way of quickly access your Controller
/// without having to call Get.find<AwesomeController>() yourself.
... ... @@ -39,27 +40,6 @@ abstract class GetView<T> extends StatelessWidget {
Widget build(BuildContext context);
}
class _Wrapper<T> {
T data;
}
abstract class GetWidget<T extends DisposableInterface>
extends StatelessWidget {
GetWidget({Key key}) : super(key: key);
final _value = _Wrapper<T>();
final String tag = null;
T get controller {
_value.data ??= GetInstance().find<T>(tag: tag);
return _value.data;
}
@override
Widget build(BuildContext context);
}
// abstract class GetView<A, B> extends StatelessWidget {
// const GetView({Key key}) : super(key: key);
// A get controller => GetInstance().find();
... ... @@ -79,46 +59,46 @@ abstract class GetWidget<T extends DisposableInterface>
// Widget build(BuildContext context);
// }
class GetStatelessElement extends ComponentElement {
GetStatelessElement(GetStatelessWidget widget) : super(widget);
/// GetWidget is a great way of quickly access your individual Controller
/// without having to call Get.find<AwesomeController>() yourself.
/// Get save you controller on cache, so, you can to use Get.create() safely
/// GetWidget is perfect to multiples instance of a same controller. Each
/// GetWidget will have your own controller, and will be call events as [onInit]
/// and [onClose] when the controller get in/get out on memory.
abstract class GetWidget<S extends GetLifeCycleBase> extends GetWidgetCache {
const GetWidget({Key key}) : super(key: key);
@override
GetStatelessWidget get widget => super.widget as GetStatelessWidget;
@protected
final String tag = null;
@override
Widget build() => widget.build(this);
S get controller => GetWidget._cache[this] as S;
@override
void update(GetStatelessWidget newWidget) {
super.update(newWidget);
markNeedsBuild();
rebuild();
}
static final _cache = <GetWidget, GetLifeCycleBase>{};
@protected
Widget build(BuildContext context);
@override
void mount(Element parent, dynamic newSlot) {
if (widget?.controller?.initialized != null &&
!widget.controller.initialized) {
widget?.controller?.onStart();
}
WidgetCache createWidgetCache() => _GetCache<S>();
}
super.mount(parent, newSlot);
class _GetCache<S extends GetLifeCycleBase> extends WidgetCache<GetWidget<S>> {
S _controller;
@override
void onInit() {
_controller = Get.find<S>(tag: widget.tag);
GetWidget._cache[widget] = _controller;
super.onInit();
}
@override
void unmount() {
widget?.controller?.onDelete();
super.unmount();
void onClose() {
GetWidget._cache.remove(widget);
super.onClose();
}
}
abstract class GetStatelessWidget<T extends DisposableInterface>
extends Widget {
const GetStatelessWidget({Key key}) : super(key: key);
@override
GetStatelessElement createElement() => GetStatelessElement(this);
@protected
Widget build(BuildContext context);
T get controller;
Widget build(BuildContext context) {
return widget.build(context);
}
}
... ...
import 'package:flutter/widgets.dart';
abstract class GetWidgetCache extends Widget {
const GetWidgetCache({Key key}) : super(key: key);
@override
GetWidgetCacheElement createElement() => GetWidgetCacheElement(this);
@protected
@factory
WidgetCache createWidgetCache();
}
class GetWidgetCacheElement extends ComponentElement {
GetWidgetCacheElement(GetWidgetCache widget)
: cache = widget.createWidgetCache(),
super(widget) {
cache._element = this;
cache._widget = widget;
}
@override
void mount(Element parent, dynamic newSlot) {
cache.onInit();
super.mount(parent, newSlot);
}
@override
Widget build() => cache.build(this);
final WidgetCache<GetWidgetCache> cache;
@override
void performRebuild() {
super.performRebuild();
}
@override
void activate() {
super.activate();
markNeedsBuild();
}
@override
void unmount() {
super.unmount();
cache.onClose();
cache._element = null;
}
}
@optionalTypeArgs
abstract class WidgetCache<T extends GetWidgetCache> {
T get widget => _widget;
T _widget;
BuildContext get context => _element;
GetWidgetCacheElement _element;
@protected
@mustCallSuper
void onInit() {}
@protected
@mustCallSuper
void onClose() {}
@protected
Widget build(BuildContext context);
}
... ...
//import 'package:flutter/foundation.dart';
//import 'package:flutter/material.dart';
//import 'package:get/state_manager.dart';
//
//import '../../instance/get_instance.dart';
//
//abstract class GetState<T> extends DisposableInterface {
// GetState(this.initialValue) {
// _state = initialValue;
// }
//
// final Set<StateSetter> _updaters = <StateSetter>{};
//
// @protected
// void update(T value, [bool condition = true]) {
// if (!condition) return;
// _state = value;
// _updaters.forEach((rs) => rs(() {}));
// }
//
// T _state;
//
// final T initialValue;
//
// void addListener(StateSetter value) {
// _updaters.add(value);
// }
//
// void removeListener(StateSetter value) {
// _updaters.add(value);
// }
//
// // @protected
// T get state => _state;
//
// @protected
// void updater(void fn(T value)) {
// fn(_state);
// update(_state);
// }
//}
//
//class StateBuilder<T extends GetState> extends StatefulWidget {
// final Widget Function(dynamic) builder;
// final bool global;
// final String tag;
// final bool autoRemove;
// final bool assignId;
// final void Function(State state) initState, dispose, didChangeDependencies;
// final void Function(StateBuilder oldWidget, State state) didUpdateWidget;
// final T Function() state;
//
// const StateBuilder({
// Key key,
// this.state,
// this.global = true,
// @required this.builder,
// this.autoRemove = true,
// this.assignId = false,
// this.initState,
// this.tag,
// this.dispose,
// this.didChangeDependencies,
// this.didUpdateWidget,
// }) : assert(builder != null),
// super(key: key);
//
// @override
// _StateBuilderState<T> createState() => _StateBuilderState<T>();
//}
//
//class _StateBuilderState<T extends GetState> extends State<StateBuilder<T>> {
// T controller;
//
// bool isCreator = false;
//
// @override
// void initState() {
// super.initState();
// if (widget.initState != null) widget.initState(this);
// if (widget.global) {
// final isPrepared = GetInstance().isPrepared<T>(tag: widget.tag);
// final isRegistred = GetInstance().isRegistred<T>(tag: widget.tag);
//
// if (isPrepared) {
// isCreator = true;
// } else if (isRegistred) {
// isCreator = false;
// } else {
// isCreator = true;
// }
//
// if (isCreator) {
// controller?.onStart();
// }
//
// final instance = GetInstance().putOrFind(
// widget.state,
// tag: widget.tag,
// );
// controller = instance;
// controller._updaters.add(setState);
// } else {
// controller = widget.state();
// isCreator = true;
// controller._updaters.add(setState);
// controller?.onStart();
// }
// }
//
// @override
// void dispose() {
// super.dispose();
// if (widget.dispose != null) widget.dispose(this);
// if (isCreator || widget.assignId) {
// if (widget.autoRemove &&
// GetInstance().isRegistred<T>(
// tag: widget.tag,
// )) {
// controller._updaters.remove(setState);
// GetInstance().delete<T>(tag: widget.tag);
// }
// } else {
// controller._updaters.remove(setState);
// }
// }
//
// @override
// void didChangeDependencies() {
// super.didChangeDependencies();
// if (widget.didChangeDependencies != null) {
// widget.didChangeDependencies(this);
// }
// }
//
// @override
// void didUpdateWidget(StateBuilder oldWidget) {
// super.didUpdateWidget(oldWidget as StateBuilder<T>);
// if (widget.didUpdateWidget != null) {
// widget.didUpdateWidget(oldWidget, this);
// }
// }
//
// @override
// Widget build(BuildContext context) {
// return widget.builder(controller.state);
// }
//}