Flutter知识点: Notification

作者: jzhu085 | 来源:发表于2018-07-02 23:35 被阅读257次

数据传输一般处理方式:自上而下的一层一层传递数据。

场景: WidgetB数据改变后,WidgetA也随之作出响应。

解决方案1: 参考Flutter知识点: InheritedWidget

解决方案2:Notification,子节点状态变更,发送通知上报。

1.自定义notification

class TestNotification extends Notification {
TestNotification({
  @required this.count,
});

final int count;
}

2.子节点发送通知

new RaisedButton(
       textColor: Colors.black,
       child: new Center(
         child: new Text('点击传递随机数给上层Widget'),
       ),
       onPressed: () {
         new TestNotification(count: new Random().nextInt(100)).dispatch(key.currentContext);
       })

3.父节点使用NotificationListener进行监听子节点发出的通知,并作出响应

new NotificationListener(
           onNotification: (TestNotification n) {
             scaffoldStateKey.currentState.showSnackBar(new SnackBar(content: new Text('随机数:${n.count}')));
             return true;
           },
           child: new TestAPage(
             key: key,
           ))

效果图

11.jpeg

知乎日报Flutter版代码已经上传到我的GITHUB ZhihuDailyPurifyByFlutter

基础学习过程中的代码都放在GITHUB Flutter_Study

每天学一点,学到Flutter发布正式版!

相关文章

网友评论

  • fangkyi03:flutter结构是真乱
    jzhu085:@fangkyi03 可以看看国外大神的架构,还行吧
  • 49916e521798:感谢作者的文章和例子!受益匪浅.我对例子程序做了简化,不再使用key和scaffoldStateKey:
    <code>
    import 'package:flutter/material.dart';
    import 'dart:math';

    void main() => runApp(MyApp());

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    title: 'Notification Usage',
    home: Scaffold(
    appBar: AppBar(
    title: Text('Notification Usage 2'),
    ),
    body: NotificationPage(),
    ),
    );
    }
    }

    class TestNotification extends Notification {
    TestNotification({
    @required this.count,
    });

    final int count;
    }

    //HomePage
    class NotificationPage extends StatefulWidget {
    @override
    State<StatefulWidget> createState() {
    return _NotificationPageState();
    }
    }

    class _NotificationPageState extends State<NotificationPage> {
    @override
    Widget build(BuildContext context) {
    return Column(
    children: <Widget>[
    NotificationListener(
    onNotification: (TestNotification n) {
    Scaffold
    .of(context)
    .showSnackBar(SnackBar(content: Text('随机数:${n.count}')));
    return true;
    },
    child: TestAPage(),
    ) //也是一个器件
    ],
    );
    }
    }

    class TestAPage extends StatefulWidget {
    @override
    State<StatefulWidget> createState() {
    return _TestAPageState();
    }
    }

    class _TestAPageState extends State<TestAPage> {
    @override
    void initState() {
    super.initState();
    }

    @override
    Widget build(BuildContext context) {
    return Padding(
    padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
    child: RaisedButton(
    textColor: Colors.black,
    child: Center(
    child: Text('点击传递随机数给上层Widget 2'),
    ),
    onPressed: () {
    TestNotification(count: Random().nextInt(100))
    .dispatch(context); //发送通知,类似Ext.fireEvent
    }),
    );
    }
    }
    //EOP

    </code>
    jzhu085:@JonahFang_e528 谢谢啊,我瞅瞅
  • 燃烧的鱼丸:除了父子组件的通信吧,两个相隔很远的组件也可以这么玩不?
    jzhu085:@燃烧的鱼丸 不知道是我没掌握好还是咋的,确实不行。。
    燃烧的鱼丸:@老实巴交的读书人 :+1:
    jzhu085:@燃烧的鱼丸 我明天测试下...这个还是因为有人问我,我才想起来去解决的一个问题..

本文标题:Flutter知识点: Notification

本文链接:https://www.haomeiwen.com/subject/scpruftx.html