老师能否讲一下PageController是什么东西?
来源:8-3 APP首页框架搭建-项目实践【搭了个框架】
咕噜咕噜冒泡
2019-03-13
我看这个PageController继承于ScrollController,ScrollController又继承于ChangeNotifier,老师能否讲一下ChangeNotifier的用法
写回答
1回答
-
不错,学习能钻研到这种程度,老师为你点赞!
分享下我对ChangeNotifier的理解:
ChangeNotifier是flutter的foundation中的一个工具里,它提供addListener,removeListener等方法来为实现类提供添加监听器和移除监听器的功能,它内部维护了一个_listeners的成员变量:
ObserverList<VoidCallback> _listeners = ObserverList<VoidCallback>();
关键核心实现是它的notifyListeners方法:
@protected @visibleForTesting void notifyListeners() { assert(_debugAssertNotDisposed()); if (_listeners != null) { final List<VoidCallback> localListeners = List<VoidCallback>.from(_listeners); for (VoidCallback listener in localListeners) { try { if (_listeners.contains(listener)) listener(); } catch (exception, stack) { FlutterError.reportError(FlutterErrorDetails( exception: exception, stack: stack, library: 'foundation library', context: 'while dispatching notifications for $runtimeType', informationCollector: (StringBuffer information) { information.writeln('The $runtimeType sending notification was:'); information.write(' $this'); } )); } } } } }该方法会遍历_listeners list中所有的元素并发送通知。
它的实现类主要有:TabController,ScrollController,FocusNode、ValueNotifier等
了解一个类或组件的最便捷的方法就是攻读源码,加油哦
212019-03-20
相似问题