flutter-theming-apps by flutter/skills
npx skills add https://github.com/flutter/skills --skill flutter-theming-appsFlutter 按照严格的层次结构应用样式:应用于特定小部件的样式 -> 覆盖直接父主题的主题 -> 主应用主题。
MaterialApp 的 theme 属性并传入一个 ThemeData 实例来定义应用范围的主题。Theme 小部件中并使用 Theme.of(context).copyWith(...) 来覆盖其主题。ThemeData 属性:
accentColor 替换为 。广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
colorScheme.secondaryaccentTextTheme 替换为 textTheme(使用 colorScheme.onSecondary 确保对比度)。AppBarTheme.color 替换为 AppBarTheme.backgroundColor。Material 3 自 Flutter 3.16 起成为默认主题。
ColorScheme.fromSeed(seedColor: Colors.blue) 生成配色方案。这确保了可访问的对比度。ColorScheme.surfaceTint 来指示高度,而不仅仅是投影。要恢复 M2 的阴影行为,请设置 surfaceTint: Colors.transparent 并定义 shadowColor。TextStyle 上的 letterSpacing。BottomNavigationBar 替换为 NavigationBar。Drawer 替换为 NavigationDrawer。ToggleButtons 替换为 SegmentedButton。FilledButton 作为高强调度按钮,它没有 ElevatedButton 的高度效果。ThemeData 中的组件主题已规范化为使用 *ThemeData 类,而非 *Theme 小部件。
定义 ThemeData 时,请严格为以下属性使用 *ThemeData 后缀:
cardTheme: 使用 CardThemeData(而非 CardTheme)dialogTheme: 使用 DialogThemeData(而非 DialogTheme)tabBarTheme: 使用 TabBarThemeData(而非 TabBarTheme)appBarTheme: 使用 AppBarThemeData(而非 AppBarTheme)bottomAppBarTheme: 使用 BottomAppBarThemeData(而非 BottomAppBarTheme)inputDecorationTheme: 使用 InputDecorationThemeData(而非 InputDecorationTheme)旧版按钮类(FlatButton、RaisedButton、OutlineButton)已过时。
TextButton、ElevatedButton 和 OutlinedButton。ButtonStyle 对象配置按钮外观。TextButton.styleFrom(foregroundColor: Colors.blue)。MaterialStateProperty.resolveWith。构建自适应应用时,请遵循特定平台的规范,以减少认知负荷并建立用户信任。
Scrollbar 小部件上切换 thumbVisibility。SelectableText 或 SelectableText.rich 中。TextDirection.rtl 的 Row,对于其他平台使用 TextDirection.ltr。Tooltip,并使用上下文菜单包处理右键单击操作。在更新旧的 Flutter 代码库时使用此工作流程。
任务进度:
ThemeData 中移除 useMaterial3: false(默认情况下为 true)。ColorScheme 定义替换为 ColorScheme.fromSeed()。accentColor、accentColorBrightness、accentIconTheme 和 accentTextTheme。AppBarTheme(color: ...) 并将其替换为 backgroundColor。ThemeData 组件属性以使用 *ThemeData 类(例如,cardTheme: CardThemeData())。FlatButton -> TextButton,RaisedButton -> ElevatedButton,OutlineButton -> OutlinedButton)。BottomNavigationBar -> NavigationBar,Drawer -> NavigationDrawer)。在构建同时面向移动端和桌面/Web 的小部件时使用此工作流程。
任务进度:
Scrollbar 中,并设置 thumbVisibility: DeviceType.isDesktop。SelectableText 代替 Text。Row 上设置 TextDirection.rtl。Tooltip 小部件中以支持鼠标悬停状态。MaterialApp(
title: 'Adaptive App',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.deepPurple,
brightness: Brightness.light,
),
// 使用 *ThemeData 类进行组件规范化
appBarTheme: const AppBarThemeData(
backgroundColor: Colors.deepPurple, // 不要使用 'color'
elevation: 0,
),
cardTheme: const CardThemeData(
elevation: 2,
),
textTheme: const TextTheme(
bodyMedium: TextStyle(letterSpacing: 0.2),
),
),
home: const MyHomePage(),
);
TextButton(
style: ButtonStyle(
// 默认颜色
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
// 依赖于状态的覆盖颜色
overlayColor: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.hovered)) {
return Colors.blue.withOpacity(0.04);
}
if (states.contains(MaterialState.focused) || states.contains(MaterialState.pressed)) {
return Colors.blue.withOpacity(0.12);
}
return null; // 回退到小部件的默认值。
},
),
),
onPressed: () {},
child: const Text('Adaptive Button'),
)
Row(
// Windows 期望确认按钮在左侧(RTL 反转了标准的 LTR Row 顺序)
textDirection: Platform.isWindows ? TextDirection.rtl : TextDirection.ltr,
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Confirm'),
),
],
)
每周安装量
2.0K
代码仓库
GitHub 星标数
784
首次出现
11 天前
安全审计
安装于
codex1.9K
opencode1.9K
github-copilot1.9K
gemini-cli1.9K
cursor1.9K
kimi-cli1.9K
Flutter applies styling in a strict hierarchy: styles applied to the specific widget -> themes that override the immediate parent theme -> the main app theme.
theme property of MaterialApp with a ThemeData instance.Theme widget and using Theme.of(context).copyWith(...).ThemeData properties:
accentColor with colorScheme.secondary.accentTextTheme with textTheme (using colorScheme.onSecondary for contrast).AppBarTheme.color with AppBarTheme.backgroundColor.Material 3 is the default theme as of Flutter 3.16.
ColorScheme.fromSeed(seedColor: Colors.blue). This ensures accessible contrast ratios.ColorScheme.surfaceTint to indicate elevation instead of just drop shadows. To revert to M2 shadow behavior, set surfaceTint: Colors.transparent and define a shadowColor.letterSpacing on the specific TextStyle.BottomNavigationBar with NavigationBar.Component themes in ThemeData have been normalized to use *ThemeData classes rather than *Theme widgets.
When defining ThemeData, strictly use the *ThemeData suffix for the following properties:
cardTheme: Use CardThemeData (Not CardTheme)dialogTheme: Use DialogThemeData (Not DialogTheme)tabBarTheme: Use TabBarThemeData (Not TabBarTheme)appBarTheme: Use AppBarThemeData (Not )Legacy button classes (FlatButton, RaisedButton, OutlineButton) are obsolete.
TextButton, ElevatedButton, and OutlinedButton.ButtonStyle object.TextButton.styleFrom(foregroundColor: Colors.blue).MaterialStateProperty.resolveWith.When building adaptive apps, respect platform-specific norms to reduce cognitive load and build user trust.
thumbVisibility on the Scrollbar widget based on the platform.SelectableText or SelectableText.rich.Row with TextDirection.rtl for Windows and TextDirection.ltr for others.Tooltip for hover states and use context menu packages for right-click actions.Use this workflow when updating an older Flutter codebase.
Task Progress:
useMaterial3: false from ThemeData (it is true by default).ColorScheme definitions with ColorScheme.fromSeed().accentColor, accentColorBrightness, accentIconTheme, and accentTextTheme.AppBarTheme(color: ...) and replace with backgroundColor.Use this workflow when building a widget intended for both mobile and desktop/web.
Task Progress:
Scrollbar and set thumbVisibility: DeviceType.isDesktop.SelectableText instead of Text.TextDirection.rtl on the button Row.Tooltip widgets to support mouse hover states.MaterialApp(
title: 'Adaptive App',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.deepPurple,
brightness: Brightness.light,
),
// Use *ThemeData classes for component normalization
appBarTheme: const AppBarThemeData(
backgroundColor: Colors.deepPurple, // Do not use 'color'
elevation: 0,
),
cardTheme: const CardThemeData(
elevation: 2,
),
textTheme: const TextTheme(
bodyMedium: TextStyle(letterSpacing: 0.2),
),
),
home: const MyHomePage(),
);
TextButton(
style: ButtonStyle(
// Default color
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
// State-dependent overlay color
overlayColor: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.hovered)) {
return Colors.blue.withOpacity(0.04);
}
if (states.contains(MaterialState.focused) || states.contains(MaterialState.pressed)) {
return Colors.blue.withOpacity(0.12);
}
return null; // Defer to the widget's default.
},
),
),
onPressed: () {},
child: const Text('Adaptive Button'),
)
Row(
// Windows expects confirmation on the left (RTL reverses the standard LTR Row)
textDirection: Platform.isWindows ? TextDirection.rtl : TextDirection.ltr,
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Confirm'),
),
],
)
Weekly Installs
2.0K
Repository
GitHub Stars
784
First Seen
11 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex1.9K
opencode1.9K
github-copilot1.9K
gemini-cli1.9K
cursor1.9K
kimi-cli1.9K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
Drawer with NavigationDrawer.ToggleButtons with SegmentedButton.FilledButton for a high-emphasis button without the elevation of ElevatedButton.AppBarThemebottomAppBarTheme: Use BottomAppBarThemeData (Not BottomAppBarTheme)inputDecorationTheme: Use InputDecorationThemeData (Not InputDecorationTheme)ThemeData component properties to use *ThemeData classes (e.g., cardTheme: CardThemeData()).FlatButton -> TextButton, RaisedButton -> ElevatedButton, OutlineButton -> OutlinedButton).BottomNavigationBar -> NavigationBar, Drawer -> NavigationDrawer).