Showing
3 changed files
with
103 additions
and
200 deletions
1 | import 'package:flutter/widgets.dart'; | 1 | import 'package:flutter/widgets.dart'; |
2 | import '../../../get_instance/src/get_instance.dart'; | 2 | import '../../../get_instance/src/get_instance.dart'; |
3 | -import '../rx_flutter/rx_disposable.dart'; | 3 | +import '../../../instance_manager.dart'; |
4 | +import 'get_widget_cache.dart'; | ||
4 | 5 | ||
5 | /// GetView is a great way of quickly access your Controller | 6 | /// GetView is a great way of quickly access your Controller |
6 | /// without having to call Get.find<AwesomeController>() yourself. | 7 | /// without having to call Get.find<AwesomeController>() yourself. |
@@ -39,27 +40,6 @@ abstract class GetView<T> extends StatelessWidget { | @@ -39,27 +40,6 @@ abstract class GetView<T> extends StatelessWidget { | ||
39 | Widget build(BuildContext context); | 40 | Widget build(BuildContext context); |
40 | } | 41 | } |
41 | 42 | ||
42 | -class _Wrapper<T> { | ||
43 | - T data; | ||
44 | -} | ||
45 | - | ||
46 | -abstract class GetWidget<T extends DisposableInterface> | ||
47 | - extends StatelessWidget { | ||
48 | - GetWidget({Key key}) : super(key: key); | ||
49 | - | ||
50 | - final _value = _Wrapper<T>(); | ||
51 | - | ||
52 | - final String tag = null; | ||
53 | - | ||
54 | - T get controller { | ||
55 | - _value.data ??= GetInstance().find<T>(tag: tag); | ||
56 | - return _value.data; | ||
57 | - } | ||
58 | - | ||
59 | - @override | ||
60 | - Widget build(BuildContext context); | ||
61 | -} | ||
62 | - | ||
63 | // abstract class GetView<A, B> extends StatelessWidget { | 43 | // abstract class GetView<A, B> extends StatelessWidget { |
64 | // const GetView({Key key}) : super(key: key); | 44 | // const GetView({Key key}) : super(key: key); |
65 | // A get controller => GetInstance().find(); | 45 | // A get controller => GetInstance().find(); |
@@ -79,46 +59,46 @@ abstract class GetWidget<T extends DisposableInterface> | @@ -79,46 +59,46 @@ abstract class GetWidget<T extends DisposableInterface> | ||
79 | // Widget build(BuildContext context); | 59 | // Widget build(BuildContext context); |
80 | // } | 60 | // } |
81 | 61 | ||
82 | -class GetStatelessElement extends ComponentElement { | ||
83 | - GetStatelessElement(GetStatelessWidget widget) : super(widget); | 62 | +/// GetWidget is a great way of quickly access your individual Controller |
63 | +/// without having to call Get.find<AwesomeController>() yourself. | ||
64 | +/// Get save you controller on cache, so, you can to use Get.create() safely | ||
65 | +/// GetWidget is perfect to multiples instance of a same controller. Each | ||
66 | +/// GetWidget will have your own controller, and will be call events as [onInit] | ||
67 | +/// and [onClose] when the controller get in/get out on memory. | ||
68 | +abstract class GetWidget<S extends GetLifeCycleBase> extends GetWidgetCache { | ||
69 | + const GetWidget({Key key}) : super(key: key); | ||
84 | 70 | ||
85 | - @override | ||
86 | - GetStatelessWidget get widget => super.widget as GetStatelessWidget; | 71 | + @protected |
72 | + final String tag = null; | ||
87 | 73 | ||
88 | - @override | ||
89 | - Widget build() => widget.build(this); | 74 | + S get controller => GetWidget._cache[this] as S; |
90 | 75 | ||
91 | - @override | ||
92 | - void update(GetStatelessWidget newWidget) { | ||
93 | - super.update(newWidget); | ||
94 | - markNeedsBuild(); | ||
95 | - rebuild(); | ||
96 | - } | 76 | + static final _cache = <GetWidget, GetLifeCycleBase>{}; |
77 | + | ||
78 | + @protected | ||
79 | + Widget build(BuildContext context); | ||
97 | 80 | ||
98 | @override | 81 | @override |
99 | - void mount(Element parent, dynamic newSlot) { | ||
100 | - if (widget?.controller?.initialized != null && | ||
101 | - !widget.controller.initialized) { | ||
102 | - widget?.controller?.onStart(); | ||
103 | - } | 82 | + WidgetCache createWidgetCache() => _GetCache<S>(); |
83 | +} | ||
104 | 84 | ||
105 | - super.mount(parent, newSlot); | 85 | +class _GetCache<S extends GetLifeCycleBase> extends WidgetCache<GetWidget<S>> { |
86 | + S _controller; | ||
87 | + @override | ||
88 | + void onInit() { | ||
89 | + _controller = Get.find<S>(tag: widget.tag); | ||
90 | + GetWidget._cache[widget] = _controller; | ||
91 | + super.onInit(); | ||
106 | } | 92 | } |
107 | 93 | ||
108 | @override | 94 | @override |
109 | - void unmount() { | ||
110 | - widget?.controller?.onDelete(); | ||
111 | - super.unmount(); | 95 | + void onClose() { |
96 | + GetWidget._cache.remove(widget); | ||
97 | + super.onClose(); | ||
112 | } | 98 | } |
113 | -} | ||
114 | 99 | ||
115 | -abstract class GetStatelessWidget<T extends DisposableInterface> | ||
116 | - extends Widget { | ||
117 | - const GetStatelessWidget({Key key}) : super(key: key); | ||
118 | @override | 100 | @override |
119 | - GetStatelessElement createElement() => GetStatelessElement(this); | ||
120 | - @protected | ||
121 | - Widget build(BuildContext context); | ||
122 | - | ||
123 | - T get controller; | 101 | + Widget build(BuildContext context) { |
102 | + return widget.build(context); | ||
103 | + } | ||
124 | } | 104 | } |
1 | +import 'package:flutter/widgets.dart'; | ||
2 | + | ||
3 | +abstract class GetWidgetCache extends Widget { | ||
4 | + const GetWidgetCache({Key key}) : super(key: key); | ||
5 | + | ||
6 | + @override | ||
7 | + GetWidgetCacheElement createElement() => GetWidgetCacheElement(this); | ||
8 | + | ||
9 | + @protected | ||
10 | + @factory | ||
11 | + WidgetCache createWidgetCache(); | ||
12 | +} | ||
13 | + | ||
14 | +class GetWidgetCacheElement extends ComponentElement { | ||
15 | + GetWidgetCacheElement(GetWidgetCache widget) | ||
16 | + : cache = widget.createWidgetCache(), | ||
17 | + super(widget) { | ||
18 | + cache._element = this; | ||
19 | + cache._widget = widget; | ||
20 | + } | ||
21 | + | ||
22 | + @override | ||
23 | + void mount(Element parent, dynamic newSlot) { | ||
24 | + cache.onInit(); | ||
25 | + super.mount(parent, newSlot); | ||
26 | + } | ||
27 | + | ||
28 | + @override | ||
29 | + Widget build() => cache.build(this); | ||
30 | + | ||
31 | + final WidgetCache<GetWidgetCache> cache; | ||
32 | + | ||
33 | + @override | ||
34 | + void performRebuild() { | ||
35 | + super.performRebuild(); | ||
36 | + } | ||
37 | + | ||
38 | + @override | ||
39 | + void activate() { | ||
40 | + super.activate(); | ||
41 | + markNeedsBuild(); | ||
42 | + } | ||
43 | + | ||
44 | + @override | ||
45 | + void unmount() { | ||
46 | + super.unmount(); | ||
47 | + cache.onClose(); | ||
48 | + cache._element = null; | ||
49 | + } | ||
50 | +} | ||
51 | + | ||
52 | +@optionalTypeArgs | ||
53 | +abstract class WidgetCache<T extends GetWidgetCache> { | ||
54 | + T get widget => _widget; | ||
55 | + T _widget; | ||
56 | + | ||
57 | + BuildContext get context => _element; | ||
58 | + | ||
59 | + GetWidgetCacheElement _element; | ||
60 | + | ||
61 | + @protected | ||
62 | + @mustCallSuper | ||
63 | + void onInit() {} | ||
64 | + | ||
65 | + @protected | ||
66 | + @mustCallSuper | ||
67 | + void onClose() {} | ||
68 | + | ||
69 | + @protected | ||
70 | + Widget build(BuildContext context); | ||
71 | +} |
1 | -//import 'package:flutter/foundation.dart'; | ||
2 | -//import 'package:flutter/material.dart'; | ||
3 | -//import 'package:get/state_manager.dart'; | ||
4 | -// | ||
5 | -//import '../../instance/get_instance.dart'; | ||
6 | -// | ||
7 | -//abstract class GetState<T> extends DisposableInterface { | ||
8 | -// GetState(this.initialValue) { | ||
9 | -// _state = initialValue; | ||
10 | -// } | ||
11 | -// | ||
12 | -// final Set<StateSetter> _updaters = <StateSetter>{}; | ||
13 | -// | ||
14 | -// @protected | ||
15 | -// void update(T value, [bool condition = true]) { | ||
16 | -// if (!condition) return; | ||
17 | -// _state = value; | ||
18 | -// _updaters.forEach((rs) => rs(() {})); | ||
19 | -// } | ||
20 | -// | ||
21 | -// T _state; | ||
22 | -// | ||
23 | -// final T initialValue; | ||
24 | -// | ||
25 | -// void addListener(StateSetter value) { | ||
26 | -// _updaters.add(value); | ||
27 | -// } | ||
28 | -// | ||
29 | -// void removeListener(StateSetter value) { | ||
30 | -// _updaters.add(value); | ||
31 | -// } | ||
32 | -// | ||
33 | -// // @protected | ||
34 | -// T get state => _state; | ||
35 | -// | ||
36 | -// @protected | ||
37 | -// void updater(void fn(T value)) { | ||
38 | -// fn(_state); | ||
39 | -// update(_state); | ||
40 | -// } | ||
41 | -//} | ||
42 | -// | ||
43 | -//class StateBuilder<T extends GetState> extends StatefulWidget { | ||
44 | -// final Widget Function(dynamic) builder; | ||
45 | -// final bool global; | ||
46 | -// final String tag; | ||
47 | -// final bool autoRemove; | ||
48 | -// final bool assignId; | ||
49 | -// final void Function(State state) initState, dispose, didChangeDependencies; | ||
50 | -// final void Function(StateBuilder oldWidget, State state) didUpdateWidget; | ||
51 | -// final T Function() state; | ||
52 | -// | ||
53 | -// const StateBuilder({ | ||
54 | -// Key key, | ||
55 | -// this.state, | ||
56 | -// this.global = true, | ||
57 | -// @required this.builder, | ||
58 | -// this.autoRemove = true, | ||
59 | -// this.assignId = false, | ||
60 | -// this.initState, | ||
61 | -// this.tag, | ||
62 | -// this.dispose, | ||
63 | -// this.didChangeDependencies, | ||
64 | -// this.didUpdateWidget, | ||
65 | -// }) : assert(builder != null), | ||
66 | -// super(key: key); | ||
67 | -// | ||
68 | -// @override | ||
69 | -// _StateBuilderState<T> createState() => _StateBuilderState<T>(); | ||
70 | -//} | ||
71 | -// | ||
72 | -//class _StateBuilderState<T extends GetState> extends State<StateBuilder<T>> { | ||
73 | -// T controller; | ||
74 | -// | ||
75 | -// bool isCreator = false; | ||
76 | -// | ||
77 | -// @override | ||
78 | -// void initState() { | ||
79 | -// super.initState(); | ||
80 | -// if (widget.initState != null) widget.initState(this); | ||
81 | -// if (widget.global) { | ||
82 | -// final isPrepared = GetInstance().isPrepared<T>(tag: widget.tag); | ||
83 | -// final isRegistred = GetInstance().isRegistred<T>(tag: widget.tag); | ||
84 | -// | ||
85 | -// if (isPrepared) { | ||
86 | -// isCreator = true; | ||
87 | -// } else if (isRegistred) { | ||
88 | -// isCreator = false; | ||
89 | -// } else { | ||
90 | -// isCreator = true; | ||
91 | -// } | ||
92 | -// | ||
93 | -// if (isCreator) { | ||
94 | -// controller?.onStart(); | ||
95 | -// } | ||
96 | -// | ||
97 | -// final instance = GetInstance().putOrFind( | ||
98 | -// widget.state, | ||
99 | -// tag: widget.tag, | ||
100 | -// ); | ||
101 | -// controller = instance; | ||
102 | -// controller._updaters.add(setState); | ||
103 | -// } else { | ||
104 | -// controller = widget.state(); | ||
105 | -// isCreator = true; | ||
106 | -// controller._updaters.add(setState); | ||
107 | -// controller?.onStart(); | ||
108 | -// } | ||
109 | -// } | ||
110 | -// | ||
111 | -// @override | ||
112 | -// void dispose() { | ||
113 | -// super.dispose(); | ||
114 | -// if (widget.dispose != null) widget.dispose(this); | ||
115 | -// if (isCreator || widget.assignId) { | ||
116 | -// if (widget.autoRemove && | ||
117 | -// GetInstance().isRegistred<T>( | ||
118 | -// tag: widget.tag, | ||
119 | -// )) { | ||
120 | -// controller._updaters.remove(setState); | ||
121 | -// GetInstance().delete<T>(tag: widget.tag); | ||
122 | -// } | ||
123 | -// } else { | ||
124 | -// controller._updaters.remove(setState); | ||
125 | -// } | ||
126 | -// } | ||
127 | -// | ||
128 | -// @override | ||
129 | -// void didChangeDependencies() { | ||
130 | -// super.didChangeDependencies(); | ||
131 | -// if (widget.didChangeDependencies != null) { | ||
132 | -// widget.didChangeDependencies(this); | ||
133 | -// } | ||
134 | -// } | ||
135 | -// | ||
136 | -// @override | ||
137 | -// void didUpdateWidget(StateBuilder oldWidget) { | ||
138 | -// super.didUpdateWidget(oldWidget as StateBuilder<T>); | ||
139 | -// if (widget.didUpdateWidget != null) { | ||
140 | -// widget.didUpdateWidget(oldWidget, this); | ||
141 | -// } | ||
142 | -// } | ||
143 | -// | ||
144 | -// @override | ||
145 | -// Widget build(BuildContext context) { | ||
146 | -// return widget.builder(controller.state); | ||
147 | -// } | ||
148 | -//} |
-
Please register or login to post a comment