简体   繁体   English

Flutter - 使用通知向其他小部件发送消息

[英]Flutter - Use Notification to message other widgets

I would like to use a flutter Notification in a StatefulWidget to communicate with another StatefulWidget widget. 我想在StatefulWidget中使用flutter Notification与另一个StatefulWidget小部件进行通信。 I have posted an example below that I feel should be working but it does not. 我在下面发布了一个我觉得应该工作的例子,但事实并非如此。 When you click the "+" icon button, it should send out the notification to the subPage widget. 当您单击“+”图标按钮时,它应该将通知发送到子页面小部件。 Currently, when you click the button, nothing seems to happen. 目前,当您单击按钮时,似乎没有任何事情发生。 I would expect the onTitlePush() function to be executed. 我希望执行onTitlePush()函数。 This is my first time trying Notifications and I must have something set up incorrectly. 这是我第一次尝试通知,我必须设置错误。 I am using this in a larger app but the code below is just a sample of the implementation. 我在一个更大的应用程序中使用它,但下面的代码只是实现的一个示例。 Could you let me know where I went wrong? 你能告诉我哪里出错吗?

import 'package:flutter/material.dart';

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

class TestApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Notificaton Test',
      home: MainPage(),
    );
  }
}

class MyNotification extends Notification {
  final String title;

  const MyNotification({this.title});
}

class MainPage extends StatefulWidget {
  @override
  MainPageState createState() {
    return new MainPageState();
  }
}

class MainPageState extends State<MainPage> {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Basic AppBar'),
          actions: <Widget>[
            // action button
            IconButton(
              icon: new Icon(Icons.add),
              onPressed: () {
                MyNotification(title: "Updated Text!")..dispatch(context);
              },
            ),
            // action button
          ],
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: new SubPage(),
        ),
      ),
    );
  }
}

class SubPage extends StatefulWidget {
  @override
  SubPageState createState() {
    return new SubPageState();
  }
}

class SubPageState extends State<SubPage> {
  String _text = "Click the Plus Icon";
  @override
  Widget build(BuildContext context) {
    return NotificationListener<MyNotification>(
      onNotification: onTitlePush,
      child: new Center(
          child: new Text(_text, style: new TextStyle(fontSize: 40.0))
      ),
    );
  }
  bool onTitlePush(MyNotification notification) {
    print("New item ${notification.title}");

    setState(() { _text = notification.title; });
    return true;
  }
}

Notifications are the opposite of BuildContext. 通知与BuildContext相反。 With notifications, it is children that sends value to their parents. 通过通知,孩子们可以向父母发送价值。

Notifications are not global . 通知不是全球性的 Widgets that are not parent of the dispatcher cannot listen to the said notification. 不是调度程序父级的小部件无法收听所述通知。

Therefore, what you are trying to achieve in fact cannot be done with notifications. 因此,您实际上要实现的目标无法通过通知完成。

There are a few alternatives: 有几种选择:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM