It is more a beginners question about updating the properties of a widget. But I have a hard time finding an answer in the forum about the following: In an external class (b1) I am storing the properties of my widgets in a list (tileListe). The build method looks as follows:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: b1.tileList,
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
b1.tileList[12].text= 'abc'; // Does not update
b1.tileList[12].pos.y = 10.0; // Does not update
b1.tileList.removeAt(0); // Updates when Button is pushed
});
},
child: const Icon(Icons.remove),
),
);
}
The widgets in tileList are equipped with a ValueKey(<>), where the counting starts from 1. It is taken care that the keys and index of the list do not interfere each other.
I want to update the first (set text to abc) and second (set y-Position to 10 for my widget with index 12) property and see the change immediately. But this happens only after a "Hot Restart" from Andriod Studio.
In contrast the third command (remove the widget with index 0) is updated immediately. What is different from the first and second command to the third in regard to their visible update behavior? How can I get all three changes made visible directly?
Assuming the tileList
contains Widget
s, by calling setState()
in the build()
you won't rebuild these widgets from b1.titleList
. My solution is to write somethig like this:
(<your-type-of-titleList>
- create a possibility to refresh single objectFunction?() _setStateTemp;
void refresh() {
_setStateTemp?();
}
@override
Widget build(BuildContext context) {
_setStateTemp = setState((){}); //add this line
[the rest of your code...]
}
b1
- iterate on the whole list and rebuild titleLists childreen (flutter is high effective in such operations)void refreshList() {
for (<your-type-of-titleList> tl in titleList) {
tl.refresh();
}
}
build()
- call the created refreshList()
to apply all changes in listTitles childreen@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: b1.tileList,
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
b1.tileList[12].text= 'abc';
b1.tileList[12].pos.y = 10.0;
b1.tileList.removeAt(0);
b1.refreshList(); //add this line
});
},
child: const Icon(Icons.remove),
),
);
}
Hope it will help. I wrote this without IDE so I could make some mistakes. Ask is anything is not clear. Cheers!