1

I'm trying to show a showCupertinoModalPopup in flutter which contains Switch, It is display correctly but does not update the Switch,. Why does the Switch not keeps the new state after update on a CupertinoActionSheet?

https://github.com/flutter/flutter/issues/62264

  Future<int> countAsReturnVisit(CalendarEvent event) async {
        bool remember = false;
    
        var res = await showCupertinoModalPopup(
          context: context,
          builder: (BuildContext cnt) {
            return Material(
              color: Colors.transparent,
              child: CupertinoActionSheet(
                message: Wrap(
                  children: <Widget>[
                    Text(
                        'Do you want to count \'Completed\' events as part of Return Visits?'
                            .i18n),
                    Container(
                      height: 50,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text('Remember this selection'.i18n),
                          Switch(
                            value: remember,
                            onChanged: (value) {
                              setState(() {
                                remember = value;
                              });
                            },
                            activeTrackColor: Colors.lightBlue[200],
                            activeColor: Colors.blue,
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                actions: <Widget>[
                  CupertinoActionSheetAction(
                    child: Text('No'.i18n),
                    onPressed: () {
                      Navigator.pop(context, 0);
                    },
                  ),
                ],
                cancelButton: CupertinoActionSheetAction(
                  isDefaultAction: true,
                  child: Text('Yes'.i18n),
                  onPressed: () {
                    Navigator.pop(context, 1);
                  },
                ),
              ),
            );
          },
        ) as int;
    
        if (res != null && res == 1) {
          setCalendarAlwaysCountAsRrtuVist();
        }
    
        return res;
      }
emalware
  • 73
  • 1
  • 9

1 Answers1

2

setState will only update current StatefulWidget's Widget Build function.

You should use StatefulBuilder.

For your case just add StatefulBuilder as a parent of your Switch widget, and use StateSetter when you want to update the StatefulBuilder's children. It will only update the widget tree defined under StateFulBuilder builder function.


see this question answer: 63008037

For more information on StatefulBuilder head over to StateFulBuilder documentation page.

Darshan Rathod
  • 591
  • 4
  • 16