GetBuilder
StatefullWidget
1、StatefullWidget
void initState() {
// _GetBuilderState._currentState = this;
super.initState();
widget.initState?.call(this);
var isRegistered = GetInstance().isRegistered<T>(tag: widget.tag); //获取GetxController
if (widget.global) {
if (isRegistered) {
if (GetInstance().isPrepared<T>(tag: widget.tag)) {
_isCreator = true;
} else {
_isCreator = false;
}
controller = GetInstance().find<T>(tag: widget.tag); //获取GetxController
} else {
controller = widget.init;
_isCreator = true;
GetInstance().put<T>(controller!, tag: widget.tag);//获取GetxController
}
} else {
controller = widget.init; //创建GetxController
_isCreator = true;
controller?.onStart();
}
if (widget.filter != null) {
_filter = widget.filter!(controller!);
}
_subscribeToController(); // 进入第2步
}
2、 GetxController 绑定
void _subscribeToController() {
_remove?.call();
_remove = (widget.id == null)
? controller?.addListener( //添加update
_filter != null ? _filterUpdate : getUpdate,
)
: controller?.addListenerId(
widget.id,
_filter != null ? _filterUpdate : getUpdate,
);
}
// update,调用setState 刷新界面
void getUpdate() {
if (mounted) setState(() {});
}
//GetXcontroller 添加update
Disposer addListener(GetStateUpdate listener) {
assert(_debugAssertNotDisposed());
_updaters!.add(listener); // 添加update
return () => _updaters!.remove(listener);
}
3、 GetXcontroller 调用 update 刷新statefullWidget
//在logic 中调用update
void update([List<Object>? ids, bool condition = true]) {
if (!condition) {
return;
}
if (ids == null) {
refresh(); //继续下一步
} else {
for (final id in ids) {
refreshGroup(id);
}
}
}
void refresh() {
assert(_debugAssertNotDisposed());
/// This debounce the call to update.
/// It prevent errors and duplicates builds
// if (_microtask == _version) {
// _microtask++;
// scheduleMicrotask(() {
// _version++;
// _microtask = _version;
_notifyUpdate(); //继续下一步
// });
// }
}
void _notifyUpdate() { //到这里刷新
for (var element in _updaters!) { //看到这个 _updaters 了没,对应就是 StateufullWidget的getUpdate 方法
element!();
}
}
网友评论