0
void showSimpleCustomDialog(BuildContext context, String listName) async{
if(randList.isEmpty){
    randList = await theDb.queryWhere("$listName");
  }else{
    randList.clear();
    randList = await theDb.queryWhere("$listName");
  }
setState((){
});
String randValue = "Click generate new to get a random value";
Dialog simpleDialog = Dialog(
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(12.0),
  ),
  child: Container(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Padding(
          child: Text(
            "$randValue"
          ),
        ),
        Padding(
          child: Row(
            children: <Widget>[
              RaisedButton(
                color: Colors.blue,
                onPressed: () 
                  String lol = randList[0 + rng.nextInt(randList.length - 0)].getItem();                        
                  print(randValue);
                  setState(() {
                    randValue = lol;
                  });
                },
                child: Text(
                  'generate new',
                  style: TextStyle(fontSize: 18.0, color: Colors.white),
                ),
              )
            ],
          ),
        ),
      ],
    ),
  ),
);
showDialog(
    context: context, builder: (BuildContext context) => simpleDialog);}

When I am clicking on "generate new" button I want to update my randomValue and to display that randomValue as Text Widget. To do that dynamically Im using setState, but it is not working, and i dont understand why. Please help me.

0

1 Answer 1

1

You should use StatefulBuilder with dialogue.

 showDialog(
      context: context,
      builder: (context) {
        String contentText = "Content of Dialog";
        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"),
                ),
              ],
            );
          },
        );
      },
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.