【问题标题】:How to implement event listener or delegate on flutter如何在 Flutter 上实现事件监听器或委托
【发布时间】:2019-05-06 15:04:30
【问题描述】:

我有一个主 dart 类,应用栏位于其中,并且应用栏包含一个刷新按钮。我正在使用导航抽屉来填充另外两个视图 f1 和 f2。

如何从我的 main.dart 将刷新按钮点击传递给 f1.dart 的子片段类型,以便我可以刷新 f1.dart 上的内容

// State of Main
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: new Column(
          children: <Widget>[

////////////////////////////////////////////////////////////
                new FirstFragment(),
                new SecondFragment()
/////////////////////////////////////////////////////////////
          ],
        ),
      ),
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.refresh),
            onPressed: () {
              print("refresh pressed");
/////////////////////////
         How to send this refresh pressed event to my FirstFragment class??
/////////////////////////
            },
            color: Colors.white,
          )
        ],
      ),
      body: _getDrawerItemWidget(_selectedDrawerIndex),
    );
  }

}

在 Android 中,我一直在使用事件侦听器,而在 iOS 中,我可以为此目的使用委托。我怎样才能在颤振/飞镖上实现这一点。 ?

【问题讨论】:

标签: events dart delegates flutter


【解决方案1】:

您可以传递回调,使用 VoidCallback 并在 Main 小部件上接收事件。

        class MainPage extends StatelessWidget {
          _onTapButton() {
            print("your event here");
          }

          @override
          Widget build(BuildContext context) {
            return Container(
              child: ChildPage(
                onTap: _onTapButton,
              ),
            );
          }
        }

        class ChildPage extends StatelessWidget {
          final VoidCallback onTap;

          const ChildPage({Key key, this.onTap}) : super(key: key);

          @override
          Widget build(BuildContext context) {
            return Container(
              child: RaisedButton(
                child: Text("Click Me"),
                onPressed: () {
                  //call to your callback  here
                  onTap();
                },
              ),
            );
          }
        } 

如果您想要相反的情况,您可以刷新父窗口小部件的状态并更改您传递给片段的参数,或者您也可以使用 GlobalKey,如下例所示:

        class MainPage extends StatelessWidget {

          final GlobalKey<ChildPageState> _key = GlobalKey();

          _onTapButton() {
            _key.currentState.myMethod();
          }

          @override
          Widget build(BuildContext context) {
            return Container(
              child: Column(
                children: [
                  ChildPage(
                    key: _key,
                  ),
                  RaisedButton(
                    child: Text("Click me"),
                    onPressed: _onTapButton,
                  )
                ],
              )
            );
          }
        }

        class ChildPage extends StatefulWidget {
          const ChildPage({Key key}) : super(key: key);

          @override
          ChildPageState createState() {
            return new ChildPageState();
          }
        }

        class ChildPageState extends State<ChildPage> {

          myMethod(){
            print("called from parent");
          }

          @override
          Widget build(BuildContext context) {
            return Container(
              child: Text("Click Me"),
            );
          }
        }

【讨论】:

  • 我想做相反的事情。让我的孩子参加活动。请使用示例代码查找已编辑的问题。
  • @diegoveloper 我第一个得到了NoSuchMethodError: The method 'call' was called on null
  • 或者我建议你在 pub.dev 上使用event_bus 库或基本的providers 库。使用这个callback 会花费很多时间来管理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-25
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多