perf(theme): memoize themes to stop AnimatedTheme re-lerp storm
This commit is contained in:
+41
-20
@@ -260,6 +260,43 @@ class KometAppState extends State<KometApp> {
|
|||||||
_profileUpdateController.add(null);
|
_profileUpdateController.add(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String? _themeCacheFontId;
|
||||||
|
ColorScheme? _themeCacheLight;
|
||||||
|
ColorScheme? _themeCacheDark;
|
||||||
|
ThemeData? _lightTheme;
|
||||||
|
ThemeData? _darkTheme;
|
||||||
|
|
||||||
|
void _rebuildThemesIfNeeded(ColorScheme light, ColorScheme dark) {
|
||||||
|
if (_themeCacheFontId == _fontId &&
|
||||||
|
_themeCacheLight == light &&
|
||||||
|
_themeCacheDark == dark) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_themeCacheFontId = _fontId;
|
||||||
|
_themeCacheLight = light;
|
||||||
|
_themeCacheDark = dark;
|
||||||
|
_lightTheme = withM3ETheme(
|
||||||
|
ThemeData(
|
||||||
|
useMaterial3: true,
|
||||||
|
colorScheme: light,
|
||||||
|
textTheme: AppFonts.textTheme(
|
||||||
|
_fontId,
|
||||||
|
ThemeData(brightness: Brightness.light).textTheme,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
_darkTheme = withM3ETheme(
|
||||||
|
ThemeData(
|
||||||
|
useMaterial3: true,
|
||||||
|
colorScheme: dark,
|
||||||
|
textTheme: AppFonts.textTheme(
|
||||||
|
_fontId,
|
||||||
|
ThemeData(brightness: Brightness.dark).textTheme,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
ColorScheme _adjustDarkScheme(ColorScheme base) {
|
ColorScheme _adjustDarkScheme(ColorScheme base) {
|
||||||
return base.copyWith(
|
return base.copyWith(
|
||||||
surface: Color.alphaBlend(
|
surface: Color.alphaBlend(
|
||||||
@@ -314,6 +351,8 @@ class KometAppState extends State<KometApp> {
|
|||||||
final lightScheme = _adjustLightScheme(lightBase);
|
final lightScheme = _adjustLightScheme(lightBase);
|
||||||
final darkScheme = _adjustDarkScheme(darkBase);
|
final darkScheme = _adjustDarkScheme(darkBase);
|
||||||
|
|
||||||
|
_rebuildThemesIfNeeded(lightScheme, darkScheme);
|
||||||
|
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: 'Komet',
|
title: 'Komet',
|
||||||
debugShowCheckedModeBanner: false,
|
debugShowCheckedModeBanner: false,
|
||||||
@@ -321,26 +360,8 @@ class KometAppState extends State<KometApp> {
|
|||||||
themeMode: ThemeMode.system,
|
themeMode: ThemeMode.system,
|
||||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||||
supportedLocales: AppLocalizations.supportedLocales,
|
supportedLocales: AppLocalizations.supportedLocales,
|
||||||
theme: withM3ETheme(
|
theme: _lightTheme,
|
||||||
ThemeData(
|
darkTheme: _darkTheme,
|
||||||
useMaterial3: true,
|
|
||||||
colorScheme: lightScheme,
|
|
||||||
textTheme: AppFonts.textTheme(
|
|
||||||
_fontId,
|
|
||||||
ThemeData(brightness: Brightness.light).textTheme,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
darkTheme: withM3ETheme(
|
|
||||||
ThemeData(
|
|
||||||
useMaterial3: true,
|
|
||||||
colorScheme: darkScheme,
|
|
||||||
textTheme: AppFonts.textTheme(
|
|
||||||
_fontId,
|
|
||||||
ThemeData(brightness: Brightness.dark).textTheme,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
navigatorKey: KometApp.navigatorKey,
|
navigatorKey: KometApp.navigatorKey,
|
||||||
builder: (context, child) {
|
builder: (context, child) {
|
||||||
final scaledChild = MediaQuery.withClampedTextScaling(
|
final scaledChild = MediaQuery.withClampedTextScaling(
|
||||||
|
|||||||
Reference in New Issue
Block a user