// 备注下 GetView 继承StatelessWidget ,Obx继承ObxWidget继承自StatefuleWIdget ,class GetBuilder<T extends GetxController> extends StatefulWidget
abstract class GetView<T> extends StatelessWidget {
const GetView({Key? key}) : super(key: key);
final String? tag = null;
T get controller => GetInstance().find<T>(tag: tag)!;
@override
Widget build(BuildContext context);
}
//GetxController是什么?abstract class GetxController extends DisposableInterface with ListenableMixin, ListNotifierMixin
//abstract class DisposableInterface extends GetLifeCycle 里面主要重写了onInit onReady onClose 方法
class GetBuilder<T extends GetxController> extends StatefulWidget{
final GetControllerBuilder<T> builder;
final Object? id;
final String? tag;
}
class GetBuilderState<T extends GetxController> extends State<GetBuilder<T>>
with GetStateUpdaterMixin {
T? controller;
bool? _isCreator = false;
VoidCallback? _remove;
Object? _filter;
@override
void initState() {
// _GetBuilderState._currentState = this;
super.initState();
widget.initState?.call(this);
//判断是否已经注册
var isRegistered = GetInstance().isRegistered<T>(tag: widget.tag);
if (widget.global) {
if (isRegistered) {
//判断是否已经初始化
if (GetInstance().isPrepared<T>(tag: widget.tag)) {
_isCreator = true;
} else {
_isCreator = false;
}
//获取controller
controller = GetInstance().find<T>(tag: widget.tag);
} else {
//如果未注册过 就重新初始化一个controller并注册到内存中
controller = widget.init;
_isCreator = true;
GetInstance().put<T>(controller!, tag: widget.tag);
}
} else {
controller = widget.init;
_isCreator = true;
controller?.onStart();
}
_subscribeToController();
}
/// Register to listen Controller's events.
/// It gets a reference to the remove() callback, to delete the
/// setState "link" from the Controller.
void _subscribeToController() {
_remove?.call();
//addListener 是混入ListNotifierMixin里面的方法 将getUpdate 或者filterUpdate加入到_updaters数组中
_remove = (widget.id == null)
? controller?.addListener(
_filter != null ? _filterUpdate : getUpdate,
)
: controller?.addListenerId(
widget.id,
_filter != null ? _filterUpdate : getUpdate,
);
}
void _filterUpdate() {
var newFilter = widget.filter!(controller!);
if (newFilter != _filter) {
_filter = newFilter;
getUpdate();
}
}
//这里是 mixin GetStateUpdaterMixin<T extends StatefulWidget> on State<T> { void getUpdate() { if (mounted) setState(() {}); }}
void getUpdate() {
if (mounted) setState(() {});
}
@override
void dispose() {
super.dispose();
widget.dispose?.call(this);
if (_isCreator! || widget.assignId) {
if (widget.autoRemove && GetInstance().isRegistered<T>(tag: widget.tag)) {
GetInstance().delete<T>(tag: widget.tag);
}
}
_remove?.call();
controller = null;
_isCreator = null;
_remove = null;
_filter = null;
}
@override
Widget build(BuildContext context) {
return widget.builder(controller!);
}
}
mixin ListNotifierMixin on ListenableMixin {
List<GetStateUpdate?>? _updaters = <GetStateUpdate?>[];
HashMap<Object?, List<GetStateUpdate>>? _updatersGroupIds =
HashMap<Object?, List<GetStateUpdate>>();
@protected
void refresh() {
_notifyUpdate();
}
void _notifyUpdate() {
for (var element in _updaters!) {
element!();
}
}
void _notifyIdUpdate(Object id) {
if (_updatersGroupIds!.containsKey(id)) {
final listGroup = _updatersGroupIds![id]!;
for (var item in listGroup) {
item();
}
}
}
@protected
void refreshGroup(Object id) {
_notifyIdUpdate(id);
}
@protected
void notifyChildrens() {
TaskManager.instance.notify(_updaters);
}
@override
void removeListener(VoidCallback listener) {
assert(_debugAssertNotDisposed());
_updaters!.remove(listener);
}
void removeListenerId(Object id, VoidCallback listener) {
assert(_debugAssertNotDisposed());
if (_updatersGroupIds!.containsKey(id)) {
_updatersGroupIds![id]!.remove(listener);
}
_updaters!.remove(listener);
}
@mustCallSuper
void dispose() {
assert(_debugAssertNotDisposed());
_updaters = null;
_updatersGroupIds = null;
}
@override
Disposer addListener(GetStateUpdate listener) {
_updaters!.add(listener);
return () => _updaters!.remove(listener);
}
Disposer addListenerId(Object? key, GetStateUpdate listener) {
_updatersGroupIds![key] ??= <GetStateUpdate>[];
_updatersGroupIds![key]!.add(listener);
return () => _updatersGroupIds![key]!.remove(listener);
}
void disposeId(Object id) {
_updatersGroupIds!.remove(id);
}
}
我们在调用update()方法的时候 会执行什么
abstract class GetxController extends DisposableInterface
with ListenableMixin, ListNotifierMixin {
/// Rebuilds `GetBuilder` each time you call `update()`;
/// Can take a List of [ids], that will only update the matching
/// `GetBuilder( id: )`,
/// [ids] can be reused among `GetBuilders` like group tags.
/// The update will only notify the Widgets, if [condition] is true.
void update([List<Object>? ids, bool condition = true]) {
if (!condition) {
return;
}
if (ids == null) {
refresh();
} else {
for (final id in ids) {
refreshGroup(id);
}
}
}
}
总结:
GetBuilder继承statefulWidget,在initState方法里面 判断是否已经注册 (GetInstace().isRegistered(tag:widget.tag)) 如果已经注册 判断是否初始化(GetInstace().isPrepared(tag:widget.tag))如果已经初始化就通过GetInstace().find(tag:widget.tag)获取controller,如果上面没有注册 就直接初始化并注册controller,GetInstace().put(controller!,tag:widget.tag)。
然后调用_subscribletoController(),在这里面controller.addListener(刷新方法) 或者controller.addListenerId(刷新方法) ,然后再ListNotifierMixin中将更新方法添加到updates数组中或者updatesGroupIds对象中(每个id 对应的还是_updates数组)
当我们在controller调用update或者update([''])的时候,如果传入id数组 就会遍历调用refreshGroupId(id),否则调用refresh(),
refresh调用_notifyUpdate就直接遍历_updates 调用刷新方法 执行setState({})更新
refreshGroupId 调用_notifyIdUpdate根据id,获取对应的listGroup,然后再遍历listGroup 调用刷新方法 执行setState({})更新