Is there a way to force Flutter to redraw all widgets (e.g. after locale change)?
Reload your development server to load your application. Your application should be as responsive to the new changes. Every time you click the icons, Flutter will refresh the keys and force the widget to rebuild.
setState is the most common way that we use to rebuild our widget tree to reflect the changes. But, using setState to rebuild the widget tree can be costlier sometimes. Hence there are different state management concepts like provider, BLoC, GetX, etc. that are used to rebuild the UI.
How can I force the parent to rebuild? Since your parent is a Stateless Widget, you cannot change it's color. You must use a StatefulWidget instead. Otherwise, you can use a Callback function from the child to the parent.
Your Widget
should have a setState()
method, everytime this method is called, the widget is redrawn.
Documentation :
Widget setState()
This type of use case, where you have data that children can read but you don't want to explicitly pass the data to the constructor arguments of all your children, usually calls for an InheritedWidget
. Flutter will automatically track which widgets depend on the data and rebuild the parts of your tree that have changed. There is a LocaleQuery
widget that is designed to handle locale changes, and you can see how it's used in the Stocks example app.
Briefly, here's what Stocks is doing:
StocksApp
) for handling locale changes. This callback does some work and then returns a customized instance of LocaleQueryData
onLocaleChanged
argument to the MaterialApp
constructorLocaleQuery.of(context)
.If you want to track something other than locale changes, you can make your own class that extends InheritedWidget
, and include it in the hierarchy near the root of your app. Its parent should be a StatefulWidget
with key set to a GlobalKey
that accessible to the children. The State
of the StatefulWidget
should own the data you want to distribute and expose methods for changing it that call setState
. If child widgets want change the State
's data, they can use the global key to get a pointer to the State
(key.currentState
) and call methods on it. If they want to read the data, they can call the static of(context)
method of your subclass of InheritedWidget
and that will tell Flutter that these widgets need to rebuilt whenever your State
calls setState
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With