Search code examples
flutterdialogstatebloc

Flutter: Set global variable using OK button on AlertDialog in StatefulBuilder


How to set global variable using OK button on AlertDialog in StatefulBuilder?

String stringMain = "My String";

Future<String?> dialogChangeMainContent(BuildContext context) {
    return showDialog<String>(
        context: context,
        builder: (BuildContext context) {
          return StatefulBuilder(builder: (context, setState) {
            return AlertDialog(
              title: const Text('AlertDialog Title'),
              content: const Text('AlertDialog description'),
              actions: <Widget>[
                TextButton(
                  onPressed: () {
                    Navigator.pop(context, 'OK');
                    setState(() {
                      // setState2();
                      stringMain = "New String";
                    });
                  },
                  child: const Text('OK'),
                ),
              ],
            );
          });
        });
  }

I changed StatefulWidget from StatelessWidget, but nothing changes. What I know is the problem is because the button is in the StatefulBuilder. If using BLoC etc, it's too wasteful for only small applications.


Solution

  • I found a way to solve my problem, redrawing (refreshing) the Text (or other widget) in a Stateless Widget from an AlertDialog in a StatefulBuilder. By creating a setState with a different name, for example setStateTarget. Without having to rebuild the entire page, or without state management like BLoC. My code:

    String stringMain = "My String";

    @override
      Widget build(BuildContext context) {
        return Column(
          // ....
          children: [
            StatefulBuilder(builder: (BuildContext context, setStateTarget) {
              return Column(children: [
                Text(stringMain), //  <--- target
                TextButton(
                    onPressed: () => dialogChangeMainContent(context, setStateTarget),
                    child: const Text('Show Dialog'))
              ]);
            })
          ],
        );
      }
    
      Future<String?> dialogChangeMainContent(BuildContext context, StateSetter setStateTarget) {
        return showDialog<String>(
                      context: context,
                      builder: (BuildContext context) {
                        return StatefulBuilder(builder: (context, setState) {
                          return AlertDialog(
                            title: const Text('AlertDialog Title'),
                            content: const Text('AlertDialog description'),
                            actions: <Widget>[
                              TextButton(
                                onPressed: () {
                                  Navigator.pop(context, 'OK');
                                  setStateTarget(() {
                                    // setState2();
                                    stringMain = "New String";
                                  });
                                },
                                child: const Text('OK'),
                              ),
                            ],
                          );
                        });
                      });
      }