Open flutter dialog after navigation

后端 未结 3 1225

I call Navigator pushReplacement to show a new view within my flutter app and want to immediately pop up a simple dialog to introduce the page to the user. (I want the user

相关标签:
3条回答
  • 2020-12-29 04:33

    If anyone after a solution to display dialog based on the widget value update.

    For example, let users know the screen they are trying to view, has been deleted.

    if (xxx.timestamps.deleted == null) {
      return DetailView(xxx: xxx);
    } else {
      return FutureBuilder(
        future: Future.delayed(
          Duration.zero,
          () => showDialog(
              context: context,
              builder: (_) => ActionDialog(
                  title: Text('xxx Deleted'),
                  content: Text('xxx deleted'),
                  confirmActions: [DialogConfirm.Ok])).then(
            (value) => Navigator.pop(context),
          ),
        ),
        builder: (context, _) => DetailView(xxx: xxx),
      );
    }
    

    kudos to https://stackoverflow.com/a/64017240/2641128

    0 讨论(0)
  • 2020-12-29 04:35

    You can call the dialog from inside 'initState()' dalaying its appearance after the first frame has been drawn.

      @override
      void initState() {
        super.initState();
        WidgetsBinding.instance.addPostFrameCallback((_) async {
          await showDialog<String>(
            context: context,
            builder: (BuildContext context) => new AlertDialog(
                  title: new Text("title"),
                  content: new Text("Message"),
                  actions: <Widget>[
                    new FlatButton(
                      child: new Text("OK"),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    ),
                  ],
                ),
          );
        });
      }
    
    0 讨论(0)
  • 2020-12-29 04:49

    Another way of doing it is using Timer.run(...)

    @override
    void initState() {
      super.initState();
    
      // simply use this
      Timer.run(() {
        showDialog(
          context: context,
          builder: (_) => AlertDialog(title: Text("Dialog title")),
        );
      });
    }
    
    0 讨论(0)
提交回复
热议问题