Showing
1 changed file
with
0 additions
and
119 deletions
lib/src/state_manager/rx/rx_obx.dart
deleted
100644 → 0
1 | -import 'dart:async'; | ||
2 | -import 'package:flutter/widgets.dart'; | ||
3 | -import 'package:get/src/state_manager/rx/rx_core/rx_interface.dart'; | ||
4 | -import '../rx_core/rx_impl.dart'; | ||
5 | - | ||
6 | -typedef WidgetCallback = Widget Function(); | ||
7 | - | ||
8 | -/// The simplest reactive widget in GetX. | ||
9 | -/// | ||
10 | -/// Just pass your Rx variable in the root scope of the callback to have it | ||
11 | -/// automatically registered for changes. | ||
12 | -/// | ||
13 | -/// final _name = "GetX".obs; | ||
14 | -/// Obx(() => Text( _name.value )),... ; | ||
15 | -class Obx extends StatefulWidget { | ||
16 | - final WidgetCallback builder; | ||
17 | - | ||
18 | - const Obx(this.builder); | ||
19 | - _ObxState createState() => _ObxState(); | ||
20 | -} | ||
21 | - | ||
22 | -class _ObxState extends State<Obx> { | ||
23 | - RxInterface _observer; | ||
24 | - StreamSubscription subs; | ||
25 | - | ||
26 | - _ObxState() { | ||
27 | - _observer = Rx(); | ||
28 | - } | ||
29 | - | ||
30 | - @override | ||
31 | - void initState() { | ||
32 | - subs = _observer.subject.stream.listen((data) => setState(() {})); | ||
33 | - super.initState(); | ||
34 | - } | ||
35 | - | ||
36 | - @override | ||
37 | - void dispose() { | ||
38 | - subs.cancel(); | ||
39 | - _observer.close(); | ||
40 | - super.dispose(); | ||
41 | - } | ||
42 | - | ||
43 | - Widget get notifyChilds { | ||
44 | - final observer = getObs; | ||
45 | - getObs = _observer; | ||
46 | - final result = widget.builder(); | ||
47 | - if (!_observer.canUpdate) { | ||
48 | - throw """ | ||
49 | - [Get] the improper use of a GetX has been detected. | ||
50 | - You should only use GetX or Obx for the specific widget that will be updated. | ||
51 | - If you are seeing this error, you probably did not insert any observable variables into GetX/Obx | ||
52 | - or insert them outside the scope that GetX considers suitable for an update | ||
53 | - (example: GetX => HeavyWidget => variableObservable). | ||
54 | - If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX. | ||
55 | - """; | ||
56 | - } | ||
57 | - getObs = observer; | ||
58 | - return result; | ||
59 | - } | ||
60 | - | ||
61 | - @override | ||
62 | - Widget build(BuildContext context) => notifyChilds; | ||
63 | -} | ||
64 | - | ||
65 | -/// Similar to Obx, but manages a local state. | ||
66 | -/// Pass the initial data in constructor. | ||
67 | -/// Useful for simple local states, like toggles, visibility, themes, | ||
68 | -/// button states, etc. | ||
69 | -/// Sample: | ||
70 | -/// ObxValue((data) => Switch( | ||
71 | -/// value: data.value, | ||
72 | -/// onChanged: (flag) => data.value = flag, | ||
73 | -/// ), | ||
74 | -/// false.obs, | ||
75 | -/// ), | ||
76 | - | ||
77 | -// TODO: change T to a proper Rx interface, that includes the accessor for ::value | ||
78 | -class ObxValue<T extends RxInterface> extends StatefulWidget { | ||
79 | - final Widget Function(T) builder; | ||
80 | - final T data; | ||
81 | - | ||
82 | - const ObxValue(this.builder, this.data, {Key key}) : super(key: key); | ||
83 | - | ||
84 | - _ObxValueState createState() => _ObxValueState(); | ||
85 | -} | ||
86 | - | ||
87 | -class _ObxValueState extends State<ObxValue> { | ||
88 | - RxInterface _observer; | ||
89 | - StreamSubscription subs; | ||
90 | - | ||
91 | - _ObxValueState() { | ||
92 | - _observer = Rx(); | ||
93 | - } | ||
94 | - | ||
95 | - @override | ||
96 | - void initState() { | ||
97 | - subs = _observer.subject.stream.listen((data) => setState(() {})); | ||
98 | - super.initState(); | ||
99 | - } | ||
100 | - | ||
101 | - @override | ||
102 | - void dispose() { | ||
103 | - subs.cancel(); | ||
104 | - _observer.close(); | ||
105 | - super.dispose(); | ||
106 | - } | ||
107 | - | ||
108 | - Widget get notifyChilds { | ||
109 | - final observer = getObs; | ||
110 | - getObs = _observer; | ||
111 | - // observable is implicity taken from the constructor. | ||
112 | - final result = widget.builder(widget.data); | ||
113 | - getObs = observer; | ||
114 | - return result; | ||
115 | - } | ||
116 | - | ||
117 | - @override | ||
118 | - Widget build(BuildContext context) => notifyChilds; | ||
119 | -} |
-
Please register or login to post a comment