Function/OnTap 中的 AlertDialog 设置状态

AlertDialog setstate in Function/OnTap

Flutter 新手。我知道如何设置警报对话框的状态,但是需要点击才能像 ()=> _createPlayer 这样的功能,它不想重建警报对话框。 我想知道当你需要点击它们时如何在警报对话框上设置状态。

 File _image;

    GestureDetector(
                onTap: () => _createPlayer(),

点击后会弹出如下提示对话框:

_createPlayer() {
    return showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(32.0))),
            content: Container(
              height: 400,
              width: 300,
              child: Column(
                children: <Widget>[
                  Text('Create Player', style: Theme
                      .of(context)
                      .textTheme
                      .body1),
                  GestureDetector(
                    onTap: _getImageCamera,
                    child: CircleAvatar(
                      radius: 100,
                      backgroundColor: Colors.white,
                      backgroundImage: _image != null ? FileImage(_image) : AssetImage('assets/images/undercover.png'),
                    ),
                  ),
                ],
              ),
            ),
          );
        });
  }

_getImageCamera() async{
    var image = await ImagePicker.pickImage(source: ImageSource.camera);

    setState(() {
      _image = image;
    });
  }

我想在选中时设置state/change 警告对话框中的图像。有什么想法吗?

为 AlertDialog 创建一个单独的 Stateful Widget CustomDialog,并像这样在其中移动 _getImageCamera 函数 _image 变量

_createPlayer() {
    return CustomDialog();
}
class CustomDialog extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return CustomDialogState();
  }

}

class CustomDialogState extends State<CustomDialog> {
ImageProvider _image;
@override
  void initState() {
    super.initState();
}
@override
  Widget build(BuildContext context) {
    return AlertDialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(32.0))),
            content: Container(
              height: 400,
              width: 300,
              child: Column(
                children: <Widget>[
                  Text('Create Player', style: Theme
                      .of(context)
                      .textTheme
                      .body1),
                  GestureDetector(
                    onTap: _getImageCamera,
                    child: CircleAvatar(
                      radius: 100,
                      backgroundColor: Colors.white,
                      backgroundImage: _image != null ? FileImage(_image) : AssetImage('assets/images/undercover.png'),
                    ),
                  ),
                ],
              ),
            ),
          );
        });

}

_getImageCamera() async{
    var image = await ImagePicker.pickImage(source: ImageSource.camera);

    setState(() {
      _image = image;
    });
  }
}

要查看 UI 对 showDialog 的更改,您必须创建一个新的 StatefulWidget,然后使用该 class 中的对话框。 Here is the example/sample code

您可以使用 StatefulBuilder 在对话框中进行更改

showDialog(
  context: context,
  builder: (context) {
    String contentText = "Content of Dialog";

    // add StatefulBuilder to return value

    return StatefulBuilder(
      builder: (context, setState) {
        return AlertDialog(
          title: Text("Title of Dialog"),
          content: Text(contentText),
          actions: <Widget>[
            FlatButton(
              onPressed: () => Navigator.pop(context),
              child: Text("Cancel"),
            ),
            FlatButton(
              onPressed: () {
                setState(() {
                  contentText = "Changed Content of Dialog";
                });
              },
              child: Text("Change"),
            ),
          ],
        );
      },
    );
  },
);

最愚蠢和最快的解决方法是:

Navigator.of(context).pop();

然后再次调用showDialog()。 会有一点延迟,但有效。