How to refresh an AlertDialog in Flutter?

前端 未结 5 2400
萌比男神i
萌比男神i 2020-11-27 13:54

Currently, I have an AlertDialog with an IconButton. The user can click on the IconButton, I have two colors for each click. The problem is that I

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 14:41

    First you need to use StatefulBuilder. Then i am setting _setState variable, which even could be used outside StatefulBuilder, to set new state.

    StateSetter _setState;
    String _demoText = "test";
    
    showDialog(
      context: context,
      builder: (BuildContext context) {
    
        return AlertDialog( 
          content: StatefulBuilder(  // You need this, notice the parameters below:
            builder: (BuildContext context, StateSetter setState) {
              _setState = setState;
              return Text(_demoText);
            },
          ),
        );
      },
    );
    

    _setState is used same way as setState method. For example like this:

    _setState(() {
        _demoText = "new test text";
    });
    

提交回复
热议问题