Switch not switch the bool
tried a simple switch, it stays always in initial value

Same error here
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.
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
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
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'),
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),
),
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();
}
They haven't solved this issue yet.
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'),
)