Flutter 的 Material Design 3 居然没自带 Monet 取色,这就比较难受
添加依赖包
需要用到的是 dynamic_color
1
| flutter pub add dynamic_color
|
根据官方说明,各个平台取色方式如下:
Android S+: 用户壁纸颜色
Linux: GTK+ 主题的 @theme_selected_bg_color
macOS: 应用程序的 accent color
Windows: accent color 或者 window/glass color
修改入口文件
修改你的 main.dart
,一个完整的示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import 'package:dynamic_color/dynamic_color.dart'; import 'package:flutter/material.dart';
void main() async { runApp(const MyApp()); }
class MyApp extends StatelessWidget { const MyApp({super.key});
static const _defaultColorSeed = Colors.blueAccent;
@override Widget build(BuildContext context) { Get.put(ProgressbarController()); return DynamicColorBuilder( builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) { ColorScheme lightColorScheme; ColorScheme darkColorScheme;
if (lightDynamic != null && darkDynamic != null) { lightColorScheme = lightDynamic.harmonized(); darkColorScheme = darkDynamic.harmonized(); } else { lightColorScheme = ColorScheme.fromSeed( seedColor: _defaultColorSeed, ); darkColorScheme = ColorScheme.fromSeed( seedColor: _defaultColorSeed, brightness: Brightness.dark, ); }
return GetMaterialApp( title: 'Monet Example', theme: ThemeData( useMaterial3: true, colorScheme: lightColorScheme, ), darkTheme: ThemeData( useMaterial3: true, brightness: Brightness.dark, colorScheme: darkColorScheme, ), themeMode: ThemeMode.system, ); }, ); } }
|
接下来,热重启应用程序即可看到效果~