Flutter回拨到后退按钮上的原始小部件

问题描述 投票:0回答:1

我有一个构建ListView.builder的Future Builder,构建器ListTiles由另一个具有ontap功能的小部件构建。我已经弄清楚如何使用后退按钮在最终小部件上运行某些东西,但是无法弄清楚如何在后退按钮上调用原始小部件上的内容。例如,我需要在用户单击后退按钮时刷新顶级数据,而不仅仅是已经工作的辅助窗口小部件中的数据。

我希望这是有道理的,任何帮助都会很棒。

更新这是代码。我正在简化我所展示的内容,因为制作一个简单的例子将失去上下文。下面你看到我有一个FutureBuilder返回一个返回一个新ChatWidget的ListBuilder。这是最高级别,单击后退按钮时需要刷新此Future。然而,用于捕获回调的onTap位于ChatWidget中。

new Expanded(
      child: new RefreshIndicator(
          child: new FutureBuilder<List<Map>>(
              future: chatlist,
              builder: (BuildContext context, AsyncSnapshot<List<Map>> snapshot) {
                switch (snapshot.connectionState) {
                  case ConnectionState.none: return new Text('Waiting to start');
                  case ConnectionState.waiting: return new Text('Loading...');
                  default:
                    if (snapshot.hasError) {
                      return new Text('Error: ${snapshot.error}');
                    } else {
                      return new ListView.builder(
                        itemBuilder: (context, index) {
                          ChatServerList mychat = new ChatServerList(snapshot.data[index]['msgkey'],snapshot.data[index]['refid'],snapshot.data[index]['referralname'], snapshot.data[index]['oid'],snapshot.data[index]['sendname'],snapshot.data[index]['pid'],snapshot.data[index]['receivename'],snapshot.data[index]['pgrpid'],snapshot.data[index]['prid'],snapshot.data[index]['message'],);
                          bool isgroup = true;
                          if (mychat.grpid == 0) {
                            isgroup = false;
                          }
                          return new ChatWidget(mychat: mychat,threaded: threaded, isgroup: isgroup);
                        },
                        itemCount: snapshot.data.length,
                      );
                    }
                }
              },
          ),
          onRefresh: _onRefresh
      ),
  )

这是在ChatWidget中构建的,你注意到_onTap:

new MyListTile(
              leading: new CircleAvatar(
                child: _chatAvatar(),
                //child: !isgroup ? _indMsg() : _grpMsg(), radius: 18.0,
              ),
              //subtitle: new Text(widget.mychat.oname),
              title: new Text(widget.mychat.referralname),
              trailing: new Text(widget.mychat.oname, textAlign: TextAlign.right,),
              //isThreeLine: true,
              //onTap: doTap(threaded),
              onTap: _onTap,
              onLongPress: _doArchive,
            ),
            new MyListTile(
              leading: new Text('        '),
              title: new Text(submymessage, textAlign: TextAlign.left,
                style: new TextStyle(fontSize: 15.0,
                    color: Colors.grey, fontStyle: FontStyle.italic),),
              trailing: _unreadBabdge(),
              onTap: _onTap,
              onLongPress: _doArchive,
            ),

_onTap位于下方并具有处理后退按钮的代码。

_onTap() async {
    ChatDB.instance.updateRead(widget.mychat.msgkey);
    if (threaded) {
      //TODO
    } else {
      Route route = new MaterialPageRoute(
        settings: new RouteSettings(name: "/ChatServerDivided"),
        builder: (BuildContext context) => new ChatServerDivided(mychat: widget.mychat, title: "Chat Messages",),
      );
      //Navigator.of(context).push(route);
      var nav = await Navigator.of(context).push(route);
      if(nav==true||nav==null){
        unread = ChatDB.instance.getUnread(widget.mychat.msgkey);
      }
    }


  }

所以我想要找到的是这个代码是否可以以某种方式与原始小部件进行通信,以便我可以再次运行原始的Future。我希望这更有意义,更容易理解。

flutter
1个回答
2
投票

是的,你可以这样做。无法确切地看到它在您的代码中的确切位置,但我将为您提供处理此问题的方法。导航器调用都是Futures,这意味着您可以在呼叫方面等待他们。看起来你只是缺少将值传递给.pop调用。以下是一个例子。

您导航的位置可以等待您的结果

var navigationResult = await Navigator.push(
        context,
        new MaterialPageRoute(
            builder: (context) => Page2()));

然后你可以用一个简单的if检查navigationResult。

if(navigationResult == 'rerun_future') {
    uploadFiles(); // Perform your custom functionality here.
 }

您传回该信息的方式是,当您进行弹出式调用(向后导航)时,您将在其中传递值“rerun_future”。

Navigator.of(context).pop('rerun_future') 

此外,如果您还想将此功能添加到后退按钮,您应该使用WillPopScope围绕您的脚手架,将false返回到onWillPop并将一个主要项目提供给您执行自定义弹出调用的appBar。以下来自this post的例子

@override
Widget build(BuildContext context) {
  return new WillPopScope(
    onWillPop: () async => false,
    child: new Scaffold(
      appBar: new AppBar(
        title: new Text("data"),
        leading: new IconButton(
          icon: new Icon(Icons.ac_unit),
          onPressed: () => Navigator.of(context).pop('rerun_future'), //<----- pass value here too
        ),
      ),
    ),
  );
}
© www.soinside.com 2019 - 2024. All rights reserved.