i have implemented a Dark/Light-Mode in my app using Streams. My problem is, that the change of the colors is very laggy.
Everywhere in my code are snippets like this:
Provider.of<ThemeMode>(context) == ThemeMode.light
? Colors.black : Colors.white
That's why i decided to write a small ChangeNotifierProvider: ColorState
class ColorState with ChangeNotifier {
ThemeModel _themeModel = ThemeModel();
Color _textColor = Colors.black;
Color get textColor => _textColor;
Color _backgroundColor = Colors.white;
Color get backgroundColor => _backgroundColor;
listenToModeChange() {
_themeModel.themeStream.listen((newMode) {
if (newMode == ThemeMode.light) {
_textColor = Colors.black;
_backgroundColor = Colors.white;
notifyListeners();
} else {
_textColor = Colors.white;
_backgroundColor = Color(0xff121212);
notifyListeners();
}
});
}
}
Now i replaced a lot of these small Snippets with newer ones looking like this:
backgroundcolor: Provider.of<ColorState>(context).backgroundColor,
or
color: Provider.of<ColorState>(context).textColor
Now my whole UI is laggy. I though i was being clever by getting rid of these Ternary-situations. Seems like listening to a ChangeNotifierProvider so many times through my App is much slower? I dont know, i expected it to be smoother. Seems to be the opposite now.
Also: I know there is a possibility to use and customize ThemeData for Dark/Light modes but i dont always need the same color in my app. Thats why i have to set the color manually in different locations.
My question is: Is there any better solution for this? Should i stay using ternary-operators? Is listening to a provider for maybe 100 times really that slow?