Created
June 22, 2024 15:53
-
-
Save fredgrott/7742623cb762d14be1b240f5a674c973 to your computer and use it in GitHub Desktop.
snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@override | |
Widget build(BuildContext context) { | |
// Wrap MaterialApp with a DynamicColorBuilder. | |
return DynamicColorBuilder( | |
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) { | |
ColorScheme lightColorScheme; | |
ColorScheme darkColorScheme; | |
if (lightDynamic != null && darkDynamic != null) { | |
// On Android S+ devices, use the provided dynamic color scheme. | |
// (Recommended) Harmonize the dynamic color scheme' built-in semantic colors. | |
lightColorScheme = lightDynamic.harmonized(); | |
// (Optional) Customize the scheme as desired. For example, one might | |
// want to use a brand color to override the dynamic [ColorScheme.secondary]. | |
lightColorScheme = lightColorScheme.copyWith(secondary: _brandBlue); | |
// (Optional) If applicable, harmonize custom colors. | |
lightCustomColors = lightCustomColors.harmonized(lightColorScheme); | |
// Repeat for the dark color scheme. | |
darkColorScheme = darkDynamic.harmonized(); | |
darkColorScheme = darkColorScheme.copyWith(secondary: _brandBlue); | |
darkCustomColors = darkCustomColors.harmonized(darkColorScheme); | |
_isDemoUsingDynamicColors = true; // ignore, only for demo purposes | |
} else { | |
// Otherwise, use fallback schemes. | |
lightColorScheme = ColorScheme.fromSeed( | |
seedColor: _brandBlue, | |
); | |
darkColorScheme = ColorScheme.fromSeed( | |
seedColor: _brandBlue, | |
brightness: Brightness.dark, | |
); | |
} | |
return MaterialApp( | |
theme: ThemeData( | |
colorScheme: lightColorScheme, | |
extensions: [lightCustomColors], | |
), | |
darkTheme: ThemeData( | |
colorScheme: darkColorScheme, | |
extensions: [darkCustomColors], | |
), | |
home: const Home(), | |
debugShowCheckedModeBanner: false, | |
); | |
}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment