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

PrefPage does not refresh on setState() #41

Open
sori1987 opened this issue Apr 6, 2024 · 1 comment
Open

PrefPage does not refresh on setState() #41

sori1987 opened this issue Apr 6, 2024 · 1 comment

Comments

@sori1987
Copy link

sori1987 commented Apr 6, 2024

First off, thank you for the good work!

I am not completely sure that this is a bug (but it seems to me that it is).

I am using v2.7.1 of the library (2.8.x throws some error not working at all, I will look that up and might open an issue for that as well) to create a settings page within my flutter app. In particular, in the minimum code sample below, I am using the PrefPage widget (which so far seems to be the only widget with this specific problem).

Here is the state part of my minimal app:

class _MyAppState extends State<MyApp> {

  int currentChoice = 0;

  void _updateChoice(int? value) {
    setState(() {
      currentChoice = value!;
    });
  }

  @override
  Widget build(BuildContext context) {
    return PrefService(
      service: widget.service,
      child: MaterialApp(
        home: Scaffold(
            appBar: AppBar(title: const Text('Choice')),
            body: PrefPage(
            children: [PrefPageButton(
                key: UniqueKey(),
                page: PrefPage(cache: false, key: UniqueKey(), children: [
                  PrefDropdown(
                      onChange: _updateChoice,
                      items: const [DropdownMenuItem(value: 0, child: Text("Item 1")), DropdownMenuItem(value: 1, child: Text("Item 2"))],
                      pref: 'choice',
                      title: const Text("Title"),
                      key: UniqueKey()),
                  ((currentChoice ?? 1) == 0) ? PrefText(key: UniqueKey(), pref: "myText") : PrefTitle(key: UniqueKey(), title: const Text("myTitle"))
                ]))])),
      ),
    );
  }
}

Now, when I change the choice in my dropdown item within that widget, the page is not refreshed even though I call an additional setState() on an extra variable currentChoice and the build method gets called. If I go back to the main screen and then back to the PrefPage however, it is updated.

I think it would be best if the widget was updated without using any additional setState() logic on variables apart from the corresponding pref variable, as the currentChoice variable is redundant as it is already stored in the pref preferences.

Interestingly, if I change the PrefDropdown part to a normal DropdownButton by using the following code, it gets even worse as the chosen item is not even shown in the dropdown button (if I choose Item 2 instead of Item 1, the screen still shows Item 1).

DropdownButton(
              onChanged: _updateChoice,
              items: const [DropdownMenuItem(value: 0, child: Text("Item 1")), DropdownMenuItem(value: 1, child: Text("Item 2"))],
              value: currentChoice,
              key: UniqueKey()),
              ((currentChoice ?? 1) == 0) ? PrefText(key: UniqueKey(), pref: "myText") : PrefTitle(key: UniqueKey(), title: const Text("myTitle"))
            ]))])),

I can post the full code of the minimal app if that helps.

Thanks!

@sori1987
Copy link
Author

sori1987 commented Apr 6, 2024

Hi again,
I am quite sorry, when I was looking through the other posts, I realized that this might be strongly related to #30, so it might have been better to post it there...
Cheers

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

1 participant