Show dialog on widget

前端 未结 4 1301
谎友^
谎友^ 2020-12-16 02:47

In flutter, we want to overlay a dialog above the widget.

We were able to display the dialog after pushing the button.

However, we want to display that dialo

4条回答
  •  悲&欢浪女
    2020-12-16 03:43

    some of this is ambiguous, but you can show an alert dialog in a widgets init method like this

    Future showDialog() async {
    
     WidgetsBinding.instance.addPostFrameCallback((_) async {
      await showDialog(
        context: context,
        builder: (BuildContext context) => AlertDialog(
              title: const Text('Title'),
              content: const Text('Content')
              actions: [
                FlatButton(
                  child: const Text('OK'),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              ],
            ),
      );
    });
    }
    

    this will wait for the first frame to be drawn before showing the dialog so you can call it from init

    @override
      void initState() {
       super.initState();
    
       showDialog();
    }
    

提交回复
热议问题