get_state.dart
4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import 'dart:collection';
import 'package:flutter/widgets.dart';
import 'package:get/src/instance/get_instance.dart';
import 'package:get/src/root/smart_management.dart';
import 'package:get/src/rx/rx_interface.dart';
class GetxController extends DisposableInterface {
final HashSet<UpdaterBuilder> _updaters = HashSet<UpdaterBuilder>();
/// Update GetBuilder with update();
void update([List<String> ids, bool condition = true]) {
if (!condition) return;
(ids == null)
? _updaters.forEach((rs) {
rs().updater(() {});
})
: _updaters
.where((element) => ids.contains(element().id))
.forEach((rs) => rs().updater(() {}));
}
@override
void onInit() async {}
@override
void onReady() async {}
@override
void onClose() async {}
}
class GetBuilder<T extends GetxController> extends StatefulWidget {
final Widget Function(T) builder;
final bool global;
final String id;
final String tag;
final bool autoRemove;
final bool assignId;
final void Function(State state) initState, dispose, didChangeDependencies;
final void Function(GetBuilder oldWidget, State state) didUpdateWidget;
final T init;
const GetBuilder({
Key key,
this.init,
this.global = true,
@required this.builder,
this.autoRemove = true,
this.assignId = false,
this.initState,
this.tag,
this.dispose,
this.id,
this.didChangeDependencies,
this.didUpdateWidget,
}) : assert(builder != null),
super(key: key);
@override
_GetBuilderState<T> createState() => _GetBuilderState<T>();
}
class _GetBuilderState<T extends GetxController> extends State<GetBuilder<T>> {
T controller;
UpdaterBuilder real;
bool isCreator = false;
@override
void initState() {
super.initState();
if (widget.global) {
final isPrepared = GetInstance().isPrepared<T>(tag: widget.tag);
final isRegistred = GetInstance().isRegistred<T>(tag: widget.tag);
if (isPrepared) {
if (GetConfig.smartManagement != SmartManagement.keepFactory) {
isCreator = true;
}
controller = GetInstance().find<T>(tag: widget.tag);
real = () => Updater(updater: setState, id: widget.id);
controller._updaters.add(real);
} else if (isRegistred) {
controller = GetInstance().find<T>(tag: widget.tag);
isCreator = false;
real = () => Updater(updater: setState, id: widget.id);
controller._updaters.add(real);
} else {
controller = widget.init;
isCreator = true;
real = () => Updater(updater: setState, id: widget.id);
controller._updaters.add(real);
GetInstance().put<T>(controller, tag: widget.tag);
}
} else {
controller = widget.init;
isCreator = true;
real = () => Updater(updater: setState, id: widget.id);
controller._updaters.add(real);
controller?.onStart();
}
if (widget.initState != null) widget.initState(this);
if (isCreator && GetConfig.smartManagement == SmartManagement.onlyBuilder) {
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(real);
GetInstance().delete<T>(tag: widget.tag);
}
} else {
controller._updaters.remove(real);
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (widget.didChangeDependencies != null) {
widget.didChangeDependencies(this);
}
}
@override
void didUpdateWidget(GetBuilder oldWidget) {
super.didUpdateWidget(oldWidget as GetBuilder<T>);
if (widget.didUpdateWidget != null) widget.didUpdateWidget(oldWidget, this);
}
@override
Widget build(BuildContext context) {
return widget.builder(controller);
}
}
class Updater {
final StateSetter updater;
final String id;
const Updater({this.updater, this.id});
}
typedef UpdaterBuilder = Updater Function();