Looking up a deactivated widget's ancestor is unsafe

前端 未结 5 2082
鱼传尺愫
鱼传尺愫 2020-12-29 03:46

I am new in Flutter and I am trying receive data with a Dialog. When a click in textField the error of image2 appear...

5条回答
  •  醉酒成梦
    2020-12-29 04:20

    Declare a global variable

        final GlobalKey _scaffoldKey = GlobalKey();
    

    then register the key on your widget build's scaffold eg

        @override
        Widget build(BuildContext context) {
         return Scaffold(
           key: _scaffoldKey,
           ...
    

    then on the dialog

    show(BuildContext context){
    
    var dialog = Dialog(
      child: Container(
        margin: EdgeInsets.all(8.0),
        child: Form(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextFormField(
                decoration: InputDecoration(
                    labelText: "Insira o número de telefone",
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(2.0)))),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  FlatButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text("Cancelar")),
                  FlatButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text("Aceitar"))
                ],
              )
            ],
          ),
        ),
      ),
    );
    

    Pass that scaffold context to the showDialog method

    showDialog(context: _scaffoldKey.currentContext ,builder: (context){
      return dialog;
     });
    }
    

提交回复
热议问题