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

Update SettingsTile programatically #18

Closed
zanesc opened this issue Jul 17, 2020 · 6 comments
Closed

Update SettingsTile programatically #18

zanesc opened this issue Jul 17, 2020 · 6 comments

Comments

@zanesc
Copy link

zanesc commented Jul 17, 2020

Hey, I have built an options screen that has a "Reset to Defaults" option on it but I can't get the SettingsTiles to update after resetting the settings to the defaults by using Settings.setValue(). I have the SettingsContainer inside a ChangeNotifier/Consumer which listens to changes to my cacheProvider. Setting the values triggers an update to the Consumer however the SettingsTiles do not change to the new values.

Is there another way to set the values programatically to update the UI?

@GAM3RG33K
Copy link
Owner

Sorry for the very late response.
Can you post the code for the same? It'll help me understand the issue that you are facing.

Make sure that you call setState when resetting settings with your logic as A settings tile value should update only on the rebuild of the widget, in most cases it is handled for only setting at a time.

@GAM3RG33K
Copy link
Owner

@zanesc can you provide any update?
Also, If this issue is no longer reproducible, please inform me so that I can close this.

@GAM3RG33K
Copy link
Owner

closing due to no response.

@ansshkki
Copy link

ansshkki commented Mar 1, 2021

@GAM3RG33K I have the same issue, this is the code I have:

              SettingsGroup(title: "Notifications", children: [
                SwitchSettingsTile(
                  title: "Enable notifications",
                  settingKey: "notifications_switch",
                  onChange: (value) async {
                    await Settings.setValue("notifications_start", value);
                    await Settings.setValue("notifications_end", value);
                    setState(() {});
                  },
                  childrenIfEnabled: [
                    CheckboxSettingsTile(
                      defaultValue: true,
                      title: "Periods start notifications",
                      settingKey: "notifications_start",
                      onChange: (value) async {
                        if (value) {
                          enable(forStart: true);
                        } else {
                          disable(forStart: true);
                          if (!Settings.getValue("notifications_end", false)) {
                            await Settings.setValue("notifications_switch", false);
                            setState(() {});
                          }
                        }
                      },
                    ),
                    CheckboxSettingsTile(
                      title: "Periods end notifications",
                      settingKey: "notifications_end",
                      onChange: (value) async {
                        if (value) {
                          enable(forStart: false);
                        } else {
                          disable(forStart: false);
                          if (!Settings.getValue("notifications_start", false)) {
                            await Settings.setValue("notifications_switch", false);
                            setState(() {});
                          }
                        }
                      },
                    ),
                  ],
                ),
              ])

I want the switch tile to be false when both notifications_start and notifications_end are false, but the UI doesn't change when setState() is called.

@msthoma
Copy link
Contributor

msthoma commented Mar 22, 2021

@ansshkki if you still have the issue, try wrapping the SettingsGroup or SettingsScreen with a Scaffold or Container with a unique key, like this:

Container(
      key: UniqueKey(),
      child: SettingsScreen(...),
)

It solved the issue for me, now when setState() is called the tiles are updated.

@ansshkki
Copy link

@msthoma Thank you, that's working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants