【问题标题】:Close modal bottom sheet after go back from other page从其他页面返回后关闭模态底页
【发布时间】:2020-09-25 13:46:45
【问题描述】:

我对模态底页有疑问。 在我使用 Navigator.push 到另一个页面后,当我返回时,模态底部表仍然存在。 我已经尝试使用FocusScope.of(context).focusedChild.unfocus();Navigator.pop(context); 仍然没有帮助。

模态底片代码:

floatingActionButton: FloatingActionButton(
            backgroundColor: Colors.blueAccent,
            child: Icon(
              Icons.add,
              color: Colors.white,
            ),
            onPressed: () {
              showModalBottomSheet(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8.0),
                ),
                context: context,
                isScrollControlled: true,
                builder: (context) => SingleChildScrollView(
                  child: Container(
                    padding: EdgeInsets.only(
                        bottom: MediaQuery.of(context).viewInsets.bottom),
                    child: AddTask(), //open statefull widget
                  ),
                ),
              );
            }),

添加任务代码:

     Row(
      children: <Widget>[
        InkWell(
          borderRadius: BorderRadius.circular(8),
          onTap: () async {
            FocusScope.of(context).focusedChild.unfocus();
            await DatabaseService(uid: user.uid, taskId: taskId)
                .updateTask(false, _titleValue, _notesValue,
                    UpdateChecklist().checklistInput, '', '', '');
            Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => TaskDetail(
                        uid: user.uid,
                        taskId: taskId,
                      )),
            );
          },
          child: Container(
            width: 40,
            height: 40,
            decoration: BoxDecoration(
                border: Border.all(color: Colors.blueAccent),
                borderRadius: BorderRadius.circular(8)),
            child: Icon(
              Icons.add,
              color: Colors.blueAccent,
            ),
          ),
        ),

当我返回页面时,有什么方法可以让模态底部工作表回到不聚焦状态?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    以编程方式关闭 showModalBottomSheet 是通过

    Navigator.pop(context);
    

    【讨论】:

    • 我已经尝试过使用 Navigator.pop(context);但是,它不会进入 TaskDetail 页面。屏幕停留在模态底页上。
    • @Alamsyah:您是从底部工作表启动新屏幕吗?如果是的话,你当时想隐藏底部的工作表吗?
    • @Alamsyah:你能把DatabaseService和Navigator.push()代码注释掉,只保留Navigator.pop()吗?然后告诉我它是否隐藏了底页
    • 它有效,但是,我还需要推送到另一个页面。我不能将 2 个导航器放在同一个功能上。结果不会去任何地方
    【解决方案2】:

    通过添加then解决。

    Row(
              children: <Widget>[
                InkWell(
                  borderRadius: BorderRadius.circular(8),
                  onTap: () async {
                    await DatabaseService(uid: user.uid, taskId: taskId)
                        .updateTask(false, _titleValue, _notesValue,
                            UpdateChecklist().checklistInput, '', '', '');
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => TaskDetail(
                                uid: user.uid,
                                taskId: taskId,
                              )),
                    ).then((value) => Navigator.pop(context));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 2020-04-05
      • 1970-01-01
      相关资源
      最近更新 更多