FlutterFlutter教学Flutter

Flutter(83):NotificationListener

2020-11-24  本文已影响0人  starryxp

Flutter教学目录持续更新中

Github源代码持续更新中

1.NotificationListener介绍

widget通知监听

2.NotificationListener属性

3.ScrollNotification

ScrollNotification是一个抽象类,它的子类还有:

4.Notification.ScrollMetrics属性

4.使用

  _myChild() {
    return ListView.builder(
      itemBuilder: (context, index) => ListTile(title: Text('item $index')),
      itemCount: 30,
      reverse: true,
      // physics: BouncingScrollPhysics(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('NotificationListener'),
      ),
      body: NotificationListener<ScrollNotification>(
        onNotification: (notification) {
          ScrollMetrics metrics = notification.metrics;
          print('ScrollNotification####################');
          print('pixels = ${metrics.pixels}');
          print('atEdge = ${metrics.atEdge}');
          print('axis = ${metrics.axis}');
          print('axisDirection = ${metrics.axisDirection}');
          print('extentAfter = ${metrics.extentAfter}');
          print('extentBefore = ${metrics.extentBefore}');
          print('extentInside = ${metrics.extentInside}');
          print('maxScrollExtent = ${metrics.maxScrollExtent}');
          print('minScrollExtent = ${metrics.minScrollExtent}');
          print('viewportDimension = ${metrics.viewportDimension}');
          print('outOfRange = ${metrics.outOfRange}');
          print('ScrollNotification####################');
          return false;
        },
        child: NotificationListener<ScrollUpdateNotification>(
          onNotification: (notification) {
            print('ScrollUpdateNotification####################');
            return false;
          },
          child: NotificationListener<OverscrollNotification>(
            onNotification: (notification) {
              print('OverscrollNotification####################');
              return false;
            },
            child: NotificationListener<ScrollStartNotification>(
              onNotification: (notification) {
                print('ScrollStartNotification####################');
                return false;
              },
              child: NotificationListener<ScrollEndNotification>(
                onNotification: (notification) {
                  print('ScrollEndNotification####################');
                  return false;
                },
                child: _myChild(),
              ),
            ),
          ),
        ),
      ),
    );
  }
image.png

5.系统提供的其他Notification

Notification是所有通知监听的顶级父类,它的子类有:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Scrollbar'),
      ),
      body: NotificationListener<OverscrollIndicatorNotification>(
        onNotification: (notification) {
          //滑动指示器是否在头部 true在前端,false在末端
          print('${notification.leading}');
          return true;
        },
        child: Scrollbar(
          radius: Radius.circular(10),
          thickness: 10,
          child: ListView.builder(
            itemBuilder: (context, index) {
              return ListTile(
                title: Text('item $index'),
              );
            },
            itemCount: 30,
          ),
        ),
      ),
    );
  }

6.自定义Notification

这里我们做个简单的演示

      body: NotificationListener<_CustomNotification>(
        onNotification: (notification) {
          print('1 ${notification.msg}');
          return true;
        },
        child: NotificationListener<_CustomNotification>(
          onNotification: (notification) {
            print('2 ${notification.msg}');
            return true;
          },
          child: Builder(builder: (context) {
            return RaisedButton(
              onPressed: () {
                _CustomNotification('点击了Button').dispatch(context);
              },
              child: Text('RaisedButton'),
            );
          }),
        ),
      ),

class _CustomNotification extends Notification {
  _CustomNotification(this.msg);

  final String msg;
}
image.png

这里在2级NotificationListener处消费掉当前的通知了,所以不再向1级NotificationListener传递通知了,如果将2级NotificationListener处设置为false,则会传递

image.png

下一节:DraggableScrollableSheet、DraggableScrollableNotification

Flutter(84):DraggableScrollableSheet、DraggableScrollableNotification

Flutter教学目录持续更新中

Github源代码持续更新中

上一篇 下一篇

猜你喜欢

热点阅读