Showing
1 changed file
with
29 additions
and
47 deletions
@@ -4,22 +4,21 @@ import '../../../get_rx/get_rx.dart'; | @@ -4,22 +4,21 @@ import '../../../get_rx/get_rx.dart'; | ||
4 | 4 | ||
5 | typedef WidgetCallback = Widget Function(); | 5 | typedef WidgetCallback = Widget Function(); |
6 | 6 | ||
7 | -/// The simplest reactive widget in GetX. | ||
8 | -/// | ||
9 | -/// Just pass your Rx variable in the root scope of the callback to have it | ||
10 | -/// automatically registered for changes. | 7 | +/// The [ObxWidget] is the base for all GetX reactive widgets |
11 | /// | 8 | /// |
12 | -/// final _name = "GetX".obs; | ||
13 | -/// Obx(() => Text( _name.value )),... ; | ||
14 | -class Obx extends StatefulWidget { | ||
15 | - final WidgetCallback builder; | ||
16 | - | ||
17 | - const Obx(this.builder); | 9 | +/// See also: |
10 | +/// - [Obx] | ||
11 | +/// - [ObxValue] | ||
12 | +abstract class ObxWidget extends StatefulWidget { | ||
13 | + const ObxWidget({Key key}) : super(key: key); | ||
18 | 14 | ||
19 | _ObxState createState() => _ObxState(); | 15 | _ObxState createState() => _ObxState(); |
16 | + | ||
17 | + @protected | ||
18 | + Widget build(); | ||
20 | } | 19 | } |
21 | 20 | ||
22 | -class _ObxState extends State<Obx> { | 21 | +class _ObxState extends State<ObxWidget> { |
23 | RxInterface _observer; | 22 | RxInterface _observer; |
24 | StreamSubscription subs; | 23 | StreamSubscription subs; |
25 | 24 | ||
@@ -43,7 +42,7 @@ class _ObxState extends State<Obx> { | @@ -43,7 +42,7 @@ class _ObxState extends State<Obx> { | ||
43 | Widget get notifyChilds { | 42 | Widget get notifyChilds { |
44 | final observer = getObs; | 43 | final observer = getObs; |
45 | getObs = _observer; | 44 | getObs = _observer; |
46 | - final result = widget.builder(); | 45 | + final result = widget.build(); |
47 | if (!_observer.canUpdate) { | 46 | if (!_observer.canUpdate) { |
48 | throw """ | 47 | throw """ |
49 | [Get] the improper use of a GetX has been detected. | 48 | [Get] the improper use of a GetX has been detected. |
@@ -62,6 +61,22 @@ class _ObxState extends State<Obx> { | @@ -62,6 +61,22 @@ class _ObxState extends State<Obx> { | ||
62 | Widget build(BuildContext context) => notifyChilds; | 61 | Widget build(BuildContext context) => notifyChilds; |
63 | } | 62 | } |
64 | 63 | ||
64 | +/// The simplest reactive widget in GetX. | ||
65 | +/// | ||
66 | +/// Just pass your Rx variable in the root scope of the callback to have it | ||
67 | +/// automatically registered for changes. | ||
68 | +/// | ||
69 | +/// final _name = "GetX".obs; | ||
70 | +/// Obx(() => Text( _name.value )),... ; | ||
71 | +class Obx extends ObxWidget { | ||
72 | + final WidgetCallback builder; | ||
73 | + | ||
74 | + const Obx(this.builder); | ||
75 | + | ||
76 | + @override | ||
77 | + Widget build() => builder(); | ||
78 | +} | ||
79 | + | ||
65 | /// Similar to Obx, but manages a local state. | 80 | /// Similar to Obx, but manages a local state. |
66 | /// Pass the initial data in constructor. | 81 | /// Pass the initial data in constructor. |
67 | /// Useful for simple local states, like toggles, visibility, themes, | 82 | /// Useful for simple local states, like toggles, visibility, themes, |
@@ -76,45 +91,12 @@ class _ObxState extends State<Obx> { | @@ -76,45 +91,12 @@ class _ObxState extends State<Obx> { | ||
76 | 91 | ||
77 | // TODO: change T to a proper Rx interface, that includes the accessor | 92 | // TODO: change T to a proper Rx interface, that includes the accessor |
78 | // for ::value | 93 | // for ::value |
79 | -class ObxValue<T extends RxInterface> extends StatefulWidget { | 94 | +class ObxValue<T extends RxInterface> extends ObxWidget { |
80 | final Widget Function(T) builder; | 95 | final Widget Function(T) builder; |
81 | final T data; | 96 | final T data; |
82 | 97 | ||
83 | const ObxValue(this.builder, this.data, {Key key}) : super(key: key); | 98 | const ObxValue(this.builder, this.data, {Key key}) : super(key: key); |
84 | 99 | ||
85 | - _ObxValueState createState() => _ObxValueState(); | ||
86 | -} | ||
87 | - | ||
88 | -class _ObxValueState extends State<ObxValue> { | ||
89 | - RxInterface _observer; | ||
90 | - StreamSubscription subs; | ||
91 | - | ||
92 | - _ObxValueState() { | ||
93 | - _observer = Rx(); | ||
94 | - } | ||
95 | - | ||
96 | - @override | ||
97 | - void initState() { | ||
98 | - subs = _observer.subject.stream.listen((data) => setState(() {})); | ||
99 | - super.initState(); | ||
100 | - } | ||
101 | - | ||
102 | - @override | ||
103 | - void dispose() { | ||
104 | - subs.cancel(); | ||
105 | - _observer.close(); | ||
106 | - super.dispose(); | ||
107 | - } | ||
108 | - | ||
109 | - Widget get notifyChilds { | ||
110 | - final observer = getObs; | ||
111 | - getObs = _observer; | ||
112 | - // observable is implicity taken from the constructor. | ||
113 | - final result = widget.builder(widget.data); | ||
114 | - getObs = observer; | ||
115 | - return result; | ||
116 | - } | ||
117 | - | ||
118 | @override | 100 | @override |
119 | - Widget build(BuildContext context) => notifyChilds; | 101 | + Widget build() => builder(data); |
120 | } | 102 | } |
-
Please register or login to post a comment