2

I am using an AlertDialog for my application and inside i build an counter. Value of counter does not get updated automatically. i used "setState({})", but it does not help, because it only rebuilds the build() function and not the int _value inside Dialog.

Anyone familiar to this problem and help me out? Thank you

Adrian
  • 100
  • 1
  • 9

3 Answers3

1

The code sample below solved my problem. As @anmol.majhail nicely referred, a wrote my AlertDialog inside a new class, which was then called by the build() function. To call the class and show AlertDialog i used to do this.

Widget createItem() {
    return new FloatingActionButton(
      elevation: 4.0,
      child: const Icon(Icons.create),
      onPressed: () {
        showDialog(
          context: context,
          child: new ItemAlertView()
        );
      },
    );
  }

This example helped me out (compare to line 59). https://gist.github.com/harshapulikollu/5461844368e95c6d3a38fffe72f03eee

Adrian
  • 100
  • 1
  • 9
0

User StatefulBuilder to return AlertDialog.

showDialog(
  context: context,
  builder: (context) {
    String contentText = "Initial Content";
    return StatefulBuilder(
      builder: (context, setState) {
        return AlertDialog(
          title: Text("Title Here"),
          content: Text(contentText),
          actions: <Widget>[
            FlatButton(
              onPressed: () => Navigator.pop(context),
              child: Text("Cancel"),
            ),
            FlatButton(
              onPressed: () {
                setState(() {
                  contentText = "Changed!";
                });
              },
              child: Text("Change Now"),
            ),
          ],
        );
      },
    );
  },
);
Sharad Paghadal
  • 2,089
  • 15
  • 29
  • 1
    thank you very much for your help. Unfortunately it did not work for me. i found another solution, check it out :) – Adrian Nov 07 '19 at 21:29
0

Cleanest way, in my opinion, would be to use a Stream with a StreamBuilder.

Andrei
  • 330
  • 1
  • 3
  • 12