rx_getbuilder.dart
2.56 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
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:get/src/get_main.dart';
import 'rx_impl.dart';
import 'rx_interface.dart';
class GetX<T extends DisposableInterface> extends StatefulWidget {
final Widget Function(T) builder;
final bool global;
// final Stream Function(T) stream;
// final StreamController Function(T) streamController;
final bool autoRemove;
final bool assignId;
final void Function(State state) initState, dispose, didChangeDependencies;
final T init;
const GetX({
this.builder,
this.global = true,
this.autoRemove = true,
this.initState,
this.assignId = false,
// this.stream,
this.dispose,
this.didChangeDependencies,
this.init,
// this.streamController
});
_GetXState<T> createState() => _GetXState<T>();
}
class _GetXState<T extends DisposableInterface> extends State<GetX<T>> {
RxInterface _observer;
StreamSubscription _listenSubscription;
T controller;
bool isCreator = false;
_GetXState() {
_observer = Rx();
}
@override
void initState() {
bool isPrepared = Get.isPrepared<T>();
bool isRegistred = Get.isRegistred<T>();
if (widget.global) {
// if (Get().smartManagement == SmartManagement.full) {
// Get.isDependencyInit<T>();
// }
if (isPrepared) {
isCreator = true;
controller = Get.find<T>();
} else if (isRegistred) {
controller = Get.find<T>();
isCreator = false;
} else {
controller = widget.init;
isCreator = true;
Get.put<T>(controller);
}
} else {
controller = widget.init;
isCreator = true;
controller?.onInit();
}
if (widget.initState != null) widget.initState(this);
// if (isCreator) {
// controller?.onInit();
// }
_listenSubscription = _observer.subject.stream.listen((data) {
setState(() {});
});
super.initState();
}
@override
void dispose() {
if (widget.dispose != null) widget.dispose(this);
if (isCreator || widget.assignId) {
if (widget.autoRemove && Get.isRegistred<T>()) {
// controller.onClose();
Get.delete<T>();
}
// } else {
// controller.onClose();
}
// controller.onClose();
_observer.close();
_listenSubscription?.cancel();
controller = null;
isCreator = null;
super.dispose();
}
@override
Widget build(BuildContext context) {
final observer = Get.obs;
Get.obs = this._observer;
final result = widget.builder(controller);
Get.obs = observer;
return result;
}
}