get_state.dart
3.97 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
146
147
148
149
150
151
152
153
154
155
import 'package:flutter/material.dart';
import '../get_main.dart';
class RealState {
final State state;
final String id;
final bool isCreator;
const RealState({this.state, this.id, this.isCreator = false});
}
class GetController extends State {
List<RealState> _allStates = [];
/// Update GetBuilder with update(this)
void update(GetController controller,
[List<String> ids, bool condition = true]) {
if (controller == null || !condition) return;
if (ids == null) {
// _allStates[controller.hashCode];
_allStates.forEach((rs) {
if (rs.state != null && rs.state.mounted) rs.state.setState(() {});
});
} else {
ids.forEach(
(s) {
// var all = _allStates[controller.hashCode];
_allStates.forEach((rs) {
if (rs.state != null && rs.state.mounted && rs.id == s)
rs.state.setState(() {});
});
},
);
}
}
void onClose() {}
void onInit() {}
@override
Widget build(_) => throw ("build method can't be called");
}
class GetBuilder<T extends GetController> extends StatefulWidget {
@required
final Widget Function(T) builder;
final bool global;
final String id;
final bool autoRemove;
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,
this.builder,
this.autoRemove = true,
this.initState,
this.dispose,
this.id,
this.didChangeDependencies,
this.didUpdateWidget,
}) : assert(builder != null),
super(key: key);
@override
_GetBuilderState<T> createState() => _GetBuilderState<T>();
}
class _GetBuilderState<T extends GetController> extends State<GetBuilder<T>> {
T controller;
RealState real;
bool isCreator = false;
@override
void initState() {
super.initState();
if (widget.global) {
if (Get.isPrepared<T>()) {
isCreator = true;
controller = Get.find<T>();
real = RealState(state: this, id: widget.id, isCreator: isCreator);
controller._allStates.add(real);
} else if (Get.isRegistred<T>() && !Get.isPrepared<T>()) {
controller = Get.find<T>();
isCreator = false;
real = RealState(state: this, id: widget.id, isCreator: isCreator);
controller._allStates.add(real);
} else {
controller = widget.init;
isCreator = true;
real = RealState(state: this, id: widget.id, isCreator: isCreator);
controller._allStates.add(real);
Get.put<T>(controller);
}
} else {
controller = widget.init;
isCreator = true;
real = RealState(state: this, id: widget.id, isCreator: isCreator);
controller._allStates.add(real);
}
if (widget.initState != null) widget.initState(this);
if (isCreator) {
try {
controller?.onInit();
} catch (e) {
if (Get.isLogEnable) print("Controller is not attach");
}
}
}
@override
void dispose() async {
super.dispose();
if (widget.dispose != null) widget.dispose(this);
if (isCreator) {
if (widget.autoRemove && Get.isRegistred<T>()) {
controller.onClose();
controller._allStates.remove(real);
Get.delete<T>();
}
} else {
// controller._allStates[controller].remove(this);
controller._allStates.remove(real);
}
/// force GC remove this
controller = null;
real = null;
isCreator = null;
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (widget.didChangeDependencies != null)
widget.didChangeDependencies(this);
}
@override
void didUpdateWidget(GetBuilder oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.didUpdateWidget != null) widget.didUpdateWidget(oldWidget, this);
}
@override
Widget build(BuildContext context) {
return widget.builder(controller);
}
}