Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

flutter - 如何在离开闪屏之前显示确认对话框(How can I show confirm dialog before leave flutter screen)

I want to show alert dialog before dispose or leave screen or at latest show warning snakbar , How can I do that?

(我想在处置或离开屏幕之前显示警告对话框,或者最晚显示警告提示框,该怎么办?)

I know how to show dialog and snackbar but I don't know where I do that or when , I tried do it in dispose life hook but it's make error.

(我知道如何显示对话框和小吃栏,但是我不知道该在哪里或何时进行,我曾尝试在设置生活挂钩时这样做,但这是错误的。)

due to context is dispose before showing dialog.

(由于上下文是在显示对话框之前配置的。)

  ask by Mahmoud Niypoo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use WillPopScope widget:

(您可以使用WillPopScope小部件:)

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        final value = await showDialog<bool>(
          context: context,
          builder: (context) {
            return AlertDialog(
              content: Text('Are you sure you want to exit?'),
              actions: <Widget>[
                FlatButton(
                  child: Text('No'),
                  onPressed: () {
                    Navigator.of(context).pop(false);
                  },
                ),
                FlatButton(
                  child: Text('Yes, exit'),
                  onPressed: () {
                    Navigator.of(context).pop(true);
                  },
                ),
              ],
            );
          }
        );

        return value == true;
      },
      child: Scaffold(
        appBar: AppBar(),
        body: SafeArea(
          child: Container()
        ),
      ),
    );
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...