I use the Theme.of(context)
to stylize a box decoration in the drawer widget of my app. But this causes my drawer widget and its parent to rebuild itself a few more times when I toggle the theme switcher (which shows a fade-in/fade-out effect while switching to the light/dark theme) via Provider.of
on the button press.
Why is this so? Isn't it enough to rebuild the widget one time, or two times at most? It doesn't happen when I use a static color like Colors.green
. I need to avoid these rebuilds because it may affect the app in the future as the code gets grow. In fact, it's affecting at the moment.
My app tree is like this:
- MyApp (StatelessWidget)
--- Provider
----- Consumer
------- MaterialApp
--------- HomeScreen
----------- Scaffold
------------- Drawer <- which is using the Theme.of(context)
and gets built a few more times unnecessarily when I toggled the theme, and causes the HomeScreen also rebuild itself
An issue was filed on Flutter's Github repo.
Google employees have confirmed the issue but are not investigating at the moment.
Personally I'm avoiding the static Inherited Widget method Theme.of(context)
until this is fixed.
Instead I use a State management solution to hold ThemeData, making it accessible throughout the app. I refer to it when theme items are needed.