【问题标题】:Flutter - Navigating to specific screen and popping everything above itFlutter - 导航到特定屏幕并弹出其上方的所有内容
【发布时间】:2020-11-30 03:04:01
【问题描述】:

你好,我有这个课程

服务索引 服务创建 服务编辑

我想要做的是单击创建或编辑中的按钮我想弹出所有警报和页面并将替换推送到 ServicesIndex 我该怎么办?

将在服务创建和编辑中单击的按钮

showSuccessMessage(context) {
  var responsivenessController = ResponsivenessController(context);
  Alert(
    context: context,
    type: AlertType.success,
    style: AlertStyle(titleStyle: Theme
        .of(context)
        .textTheme
        .bodyText1
        , descStyle: Theme
            .of(context)
            .textTheme
            .bodyText1),
    title: translator.currentLanguage == 'ar' ? 'تم الحفظ'
        : 'Stored Successfully',
    desc: translator.currentLanguage == 'ar' ? 'تمت عملية الحفظ بنجاح'
        : 'Information Stored Successfully',
    buttons: [
      DialogButton(
          child: Text(
            translator.currentLanguage == 'ar' ?
            "المتابعة"
                : 'OK',
            style: TextStyle(color: Colors.white,
                fontSize: responsivenessController.bodyFont),
          ),
          onPressed: () {
            Navigator.pop(context);
          }

      )
    ],
  ).show();
}

它全部在按下事件你建议什么代码?

关于堆栈的元素

Index 
  ---Create
    ---SuccessMessage
  ---Edit
    ---SuccessMessage

所以动作应该弹出 2 次并推送替换 我当然可以像这样硬编码

          onPressed: () {
        Navigator.pop(context);
        Navigator.pop(context);
        Navigator.pop(context);
        Navigator.pushReplacement(context, MaterialPageRoute(
          builder: (BuildContext context) => className
        ));
      }

但我想要更灵活的东西,比如只传递类名,它会在弹出之前的所有内容时返回它

注意:命名路由将不起作用,因为每个页面都需要一些参数,这些参数将在稍后的过程中传递,我无法在 main.dart 中全部获取它们,我会导致性能下降

【问题讨论】:

标签: flutter navigation material-design


【解决方案1】:

如果您知道需要调用多少 pop,那么只需使用 popUntilcounter

代码

// In this case we want to pop 4 times
void onPressed() {
   int count = 0;
   Navigator.popUntil(context, (route) => count++ == 4);
}

【讨论】:

  • 其实这是一个很好的方法,非常感谢
猜你喜欢
  • 2019-03-12
  • 2021-03-22
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 2020-03-06
  • 2023-01-11
  • 2019-04-16
  • 1970-01-01
相关资源
最近更新 更多