Function / OnTap中的AlertDialog设置状态

时间:2019-03-01 15:48:13

标签: dart flutter alertdialog

扑朔迷离。我知道如何设置警报对话框的状态,但是需要点击以运行类似()=> _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;
    });
  }

我想设置状态/选择警报对话框时更改图像。有想法吗?

4 个答案:

答案 0 :(得分:1)

为AlertDialog创建一个单独的有状态小部件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;
    });
  }
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

您可以使用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"),
            ),
          ],
        );
      },
    );
  },
);

答案 3 :(得分:0)

最愚蠢,最快的修复方法是:

Navigator.of(context).pop();

然后再次调用showDialog()。 会有微小的延迟,但是可以。