新建项目-5.png

Theme Widget 的功能是为 MaterialApp 定义主题数据,如导航栏颜色、标题字体、Icon样式。

ThemeData

ThemeData用于保存是Material 组件库的主题数据,Material组件需要遵守相应的设计规范,而这些规范可自定义部分都定义在ThemeData中了,所以我们可以通过ThemeData来自定义应用主题。在子组件中,我们可以通过Theme.of方法来获取当前的ThemeData

构造函数

  1. ThemeData({
  2. Brightness brightness, //黑暗模式,深色或者浅色. 默认是浅色
  3. MaterialColor primarySwatch, //主题颜色样本
  4. Color primaryColor, //主色,决定导航栏颜色
  5. Brightness primaryColorBrightness, //主色亮度.深色或浅色
  6. Color primaryColorLight, //light下主色
  7. Color primaryColorDark, //dark下主色
  8. Color accentColor, //次级色,决定大多数Widget的颜色,如进度条、开关等。
  9. Brightness accentColorBrightness, //次级色亮度. 分深色和浅色状态
  10. Color canvasColor,
  11. Color scaffoldBackgroundColor, //scaffold背景色
  12. Color bottomAppBarColor, //tabbar颜色
  13. Color cardColor, //卡片颜色
  14. Color dividerColor, //分割线颜色
  15. Color focusColor, //光标焦点颜色
  16. Color hoverColor,
  17. Color highlightColor, //高亮颜色
  18. Color splashColor,
  19. InteractiveInkFeatureFactory splashFactory,
  20. Color selectedRowColor,
  21. Color unselectedWidgetColor,
  22. Color disabledColor,
  23. Color buttonColor,
  24. ButtonThemeData buttonTheme, //按钮主题
  25. ToggleButtonsThemeData toggleButtonsTheme,
  26. Color secondaryHeaderColor,
  27. Color textSelectionColor,
  28. Color cursorColor,
  29. Color textSelectionHandleColor,
  30. Color backgroundColor,
  31. Color dialogBackgroundColor,
  32. Color indicatorColor,
  33. Color hintColor,
  34. Color errorColor,
  35. Color toggleableActiveColor,
  36. String fontFamily,
  37. TextTheme textTheme,
  38. TextTheme primaryTextTheme,
  39. TextTheme accentTextTheme,
  40. InputDecorationTheme inputDecorationTheme,
  41. IconThemeData iconTheme,
  42. IconThemeData primaryIconTheme,
  43. IconThemeData accentIconTheme,
  44. SliderThemeData sliderTheme,
  45. TabBarTheme tabBarTheme,
  46. TooltipThemeData tooltipTheme,
  47. CardTheme cardTheme,
  48. ChipThemeData chipTheme,
  49. TargetPlatform platform,
  50. MaterialTapTargetSize materialTapTargetSize,
  51. bool applyElevationOverlayColor,
  52. PageTransitionsTheme pageTransitionsTheme,
  53. AppBarTheme appBarTheme,
  54. BottomAppBarTheme bottomAppBarTheme,
  55. ColorScheme colorScheme,
  56. DialogTheme dialogTheme,
  57. FloatingActionButtonThemeData floatingActionButtonTheme,
  58. Typography typography,
  59. CupertinoThemeData cupertinoOverrideTheme,
  60. SnackBarThemeData snackBarTheme,
  61. BottomSheetThemeData bottomSheetTheme,
  62. PopupMenuThemeData popupMenuTheme,
  63. MaterialBannerThemeData bannerTheme,
  64. DividerThemeData dividerTheme,
  65. ButtonBarThemeData buttonBarTheme,
  66. })

属性有点多,找几个常用的来说下

去源码中可以看到很多默认色值都是用isDark来判断的,也许你也想到了什么…

  1. brightness ??= Brightness.light; //默认浅色
  2. final bool isDark = brightness == Brightness.dark;
  3. splashFactory ??= InkSplash.splashFactory;
  4. selectedRowColor ??= Colors.grey[100];
  5. unselectedWidgetColor ??= isDark ? Colors.white70 : Colors.black54;
  6. // Spec doesn't specify a dark theme secondaryHeaderColor, this is a guess.
  7. secondaryHeaderColor ??= isDark ? Colors.grey[700] : primarySwatch[50];
  8. textSelectionColor ??= isDark ? accentColor : primarySwatch[200];
  9. // TODO(sandrasandeep): change to color provided by Material Design team
  10. cursorColor = cursorColor ?? const Color.fromRGBO(66, 133, 244, 1.0);
  11. textSelectionHandleColor ??= isDark ? Colors.tealAccent[400] : primarySwatch[300];
  12. backgroundColor ??= isDark ? Colors.grey[700] : primarySwatch[200];
  13. dialogBackgroundColor ??= isDark ? Colors.grey[800] : Colors.white;
  14. indicatorColor ??= accentColor == primaryColor ? Colors.white : accentColor;
  15. hintColor ??= isDark ? const Color(0x80FFFFFF) : const Color(0x8A000000);
  16. errorColor ??= Colors.red[700];
  17. inputDecorationTheme ??= const InputDecorationTheme();
  18. pageTransitionsTheme ??= const PageTransitionsTheme();
  19. primaryIconTheme ??= primaryIsDark ? const IconThemeData(color: Colors.white) : const IconThemeData(color: Colors.black);
  20. accentIconTheme ??= accentIsDark ? const IconThemeData(color: Colors.white) : const IconThemeData(color: Colors.black);
  21. iconTheme ??= isDark ? const IconThemeData(color: Colors.white) : const IconThemeData(color: Colors.black87);
  22. platform ??= defaultTargetPlatform;
  23. typography ??= Typography(platform: platform);
  24. TextTheme defaultTextTheme = isDark ? typography.white : typography.black;
  25. TextTheme defaultPrimaryTextTheme = primaryIsDark ? typography.white : typography.black;
  26. TextTheme defaultAccentTextTheme = accentIsDark ? typography.white : typography.black;
  27. if (fontFamily != null) {
  28. defaultTextTheme = defaultTextTheme.apply(fontFamily: fontFamily);
  29. defaultPrimaryTextTheme = defaultPrimaryTextTheme.apply(fontFamily: fontFamily);
  30. defaultAccentTextTheme = defaultAccentTextTheme.apply(fontFamily: fontFamily);
  31. }
  32. textTheme = defaultTextTheme.merge(textTheme);
  33. primaryTextTheme = defaultPrimaryTextTheme.merge(primaryTextTheme);
  34. accentTextTheme = defaultAccentTextTheme.merge(accentTextTheme);
  35. materialTapTargetSize ??= MaterialTapTargetSize.padded;
  36. applyElevationOverlayColor ??= false;
  37. // Used as the default color (fill color) for RaisedButtons. Computing the
  38. // default for ButtonThemeData for the sake of backwards compatibility.
  39. buttonColor ??= isDark ? primarySwatch[600] : Colors.grey[300];
  40. focusColor ??= isDark ? Colors.white.withOpacity(0.12) : Colors.black.withOpacity(0.12);
  41. hoverColor ??= isDark ? Colors.white.withOpacity(0.04) : Colors.black.withOpacity(0.04);

使用

ThemeData 的数据是通过 MaterialApp 的 theme 参数来使用的,如:

  1. MaterialApp(
  2. title: 'Flutter Demo',
  3. theme: ThemeData(
  4. primarySwatch: Colors.red,
  5. ),

列子