老师能否讲一下PageController是什么东西?

来源:8-3 APP首页框架搭建-项目实践【搭了个框架】

咕噜咕噜冒泡

2019-03-13

我看这个PageController继承于ScrollController,ScrollController又继承于ChangeNotifier,老师能否讲一下ChangeNotifier的用法

写回答

1回答

CrazyCodeBoy

2019-03-13

不错,学习能钻研到这种程度,老师为你点赞!

分享下我对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等

了解一个类或组件的最便捷的方法就是攻读源码,加油哦

2
1
咕噜咕噜冒泡
非常感谢!
2019-03-20
共1条回复

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3277 问题

查看课程