3

new to flutter. I know how to set state the alert dialog, but with the need of tap to function like ()=> _createPlayer, It does not want to rebuild the alert dialog. I wonder how to set state on alert dialog when you need to tap them.

 File _image;

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

After tap, it will display an alert dialog like this:

_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;
    });
  }

I want to set state/change the image in alert dialog when selected. Any idea?

willy wijaya
  • 692
  • 3
  • 8
  • 18

4 Answers4

10

You can use StatefulBuilder for change inside dialog

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"),
            ),
          ],
        );
      },
    );
  },
);
Mehmet Ali Bayram
  • 7,222
  • 2
  • 22
  • 27
5

Create a separate Stateful Widget CustomDialog for the AlertDialog and move the _getImageCamera function _image variable inside it like this

_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;
    });
  }
}
Hussein Abdallah
  • 1,452
  • 11
  • 13
1

In order to see UI changes on showDialog, you have to create a new StatefulWidget and then work with dialog in that class. Here is the example/sample code

Harsha pulikollu
  • 2,386
  • 15
  • 28
0

The most stupidest and quickest fix is:

Navigator.of(context).pop();

Then call the showDialog() again. There will be a micro delay but works.

iknow
  • 8,358
  • 12
  • 41
  • 68
insaneray
  • 75
  • 1
  • 11