PrefPage does not refresh on setState()
Opened this issue · 1 comments
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!