Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch not switch the bool #110

Open
visign3d opened this issue Feb 3, 2022 · 9 comments
Open

Switch not switch the bool #110

visign3d opened this issue Feb 3, 2022 · 9 comments
Assignees
Labels
bug Something isn't working

Comments

@visign3d
Copy link

visign3d commented Feb 3, 2022

tried a simple switch, it stays always in initial value

image

@giovanni256
Copy link

Same error here

@fredecodes
Copy link

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.

@giovanni256
Copy link

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

@visign3d
Copy link
Author

visign3d commented Feb 4, 2022

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

@giovanni256
Copy link

giovanni256 commented Feb 4, 2022

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'),

@visign3d
Copy link
Author

visign3d commented Feb 5, 2022

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),
            ),

@HayatoYagi
Copy link

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();
}

@motasimfuad
Copy link

They haven't solved this issue yet.

@yadaniyil yadaniyil added the bug Something isn't working label Dec 1, 2023
@Q-Tran
Copy link

Q-Tran commented Apr 10, 2024

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'),
                              )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants