flutter-settings-ui icon indicating copy to clipboard operation
flutter-settings-ui copied to clipboard

Switch not switch the bool

Open visign3d opened this issue 2 years ago • 9 comments

tried a simple switch, it stays always in initial value

image

visign3d avatar Feb 03 '22 18:02 visign3d

Same error here

giovanni256 avatar Feb 03 '22 20:02 giovanni256

Probably just not doing state management correctly. Is this code inside a StatefulWidget or StatelessWidget? If Stateful, you need to call setState when you change a value so that the widget gets rebuilt.

fredecodes avatar Feb 03 '22 20:02 fredecodes

Probably just not doing state management correctly. Is this code inside a StatefulWidget or StatelessWidget? If Stateful, you need to call setState when you change a value so that the widget gets rebuilt.

Not solving the issue

giovanni256 avatar Feb 03 '22 21:02 giovanni256

ok, i just miss understood the naming of the switchtile widgets. The name initialvalue , is the actual switch value. SettingsTile.switchTile( initialValue: controller.darkTheme.value, onToggle: (s) => controller.darkTheme.value = s, you should change the initial value to actually get the switch animation

visign3d avatar Feb 04 '22 07:02 visign3d

ok, i just miss understood the naming of the switchtile widgets. The name initialvalue , is the actual switch value. SettingsTile.switchTile( initialValue: controller.darkTheme.value, onToggle: (s) => controller.darkTheme.value = s, you should change the initial value to actually get the switch animation

Could you explain your code? I have this, the switch works but it stays in the initial value:

bool darktheme= UserPreferences.getDarkMode();

SettingsTile.switchTile(
              onToggle: (value) {
                print("Dark mode changed" + value.toString());
                setState(() {
                  darktheme= value;
                });
              },
              initialValue: darktheme,
              leading: Icon(Icons.format_paint),
              title: Text('Enable custom theme'),

giovanni256 avatar Feb 04 '22 12:02 giovanni256

Not sure what is wrong in your, code , seems good to me. I am using GetX package, which don't use statefull class to change states.

try to move setState otside the widget tree:

void switchMode(bool s) {
    setState(() {
      darkmode = s;
    });
  }


SwitchListTile(
              initialvalue: darkmode,
              onChanged: (s) => switchMode(s),
            ),

visign3d avatar Feb 05 '22 10:02 visign3d

Same here. Worked correctly with StatefulWidget, but not with Provider package. The build method doesn't seem to be called after notifyChanges().

SettingsTile.switchTile(
  initialValue: context.select((SettingsViewModel viewModel) => viewModel.enableNotification),
  onToggle: (value) {
    context.read<SettingsViewModel>().setEnableNotification(value);
  },
  title: const Text("通知オン"),
),
void setEnableNotification(bool value) async {
  final prefs = await SharedPreferences.getInstance();
  prefs.setBool(_enableNotificationKey, value);
  notifyListeners();
}

HayatoYagi avatar May 03 '22 17:05 HayatoYagi

They haven't solved this issue yet.

motasimfuad avatar Jun 10 '22 21:06 motasimfuad

I am also having problems with this. The below code does not toggle the widget. However, the print statement works and changes the variable once.

SettingsTile.switchTile(
                                onToggle: (bool value) {
                                  setState(() {
                                    print(surrender);
                                    surrender = value;
                                  });
                                },
                                initialValue: surrender,
                                leading: Icon(Icons.format_paint),
                                title: Text('S17'),
                              )

Q-Tran avatar Apr 10 '24 22:04 Q-Tran