
Theme Widget 的功能是为 MaterialApp 定义主题数据,如导航栏颜色、标题字体、Icon样式。
ThemeData
ThemeData用于保存是Material 组件库的主题数据,Material组件需要遵守相应的设计规范,而这些规范可自定义部分都定义在ThemeData中了,所以我们可以通过ThemeData来自定义应用主题。在子组件中,我们可以通过Theme.of方法来获取当前的ThemeData。
构造函数
ThemeData({Brightness brightness, //黑暗模式,深色或者浅色. 默认是浅色MaterialColor primarySwatch, //主题颜色样本Color primaryColor, //主色,决定导航栏颜色Brightness primaryColorBrightness, //主色亮度.深色或浅色Color primaryColorLight, //light下主色Color primaryColorDark, //dark下主色Color accentColor, //次级色,决定大多数Widget的颜色,如进度条、开关等。Brightness accentColorBrightness, //次级色亮度. 分深色和浅色状态Color canvasColor,Color scaffoldBackgroundColor, //scaffold背景色Color bottomAppBarColor, //tabbar颜色Color cardColor, //卡片颜色Color dividerColor, //分割线颜色Color focusColor, //光标焦点颜色Color hoverColor,Color highlightColor, //高亮颜色Color splashColor,InteractiveInkFeatureFactory splashFactory,Color selectedRowColor,Color unselectedWidgetColor,Color disabledColor,Color buttonColor,ButtonThemeData buttonTheme, //按钮主题ToggleButtonsThemeData toggleButtonsTheme,Color secondaryHeaderColor,Color textSelectionColor,Color cursorColor,Color textSelectionHandleColor,Color backgroundColor,Color dialogBackgroundColor,Color indicatorColor,Color hintColor,Color errorColor,Color toggleableActiveColor,String fontFamily,TextTheme textTheme,TextTheme primaryTextTheme,TextTheme accentTextTheme,InputDecorationTheme inputDecorationTheme,IconThemeData iconTheme,IconThemeData primaryIconTheme,IconThemeData accentIconTheme,SliderThemeData sliderTheme,TabBarTheme tabBarTheme,TooltipThemeData tooltipTheme,CardTheme cardTheme,ChipThemeData chipTheme,TargetPlatform platform,MaterialTapTargetSize materialTapTargetSize,bool applyElevationOverlayColor,PageTransitionsTheme pageTransitionsTheme,AppBarTheme appBarTheme,BottomAppBarTheme bottomAppBarTheme,ColorScheme colorScheme,DialogTheme dialogTheme,FloatingActionButtonThemeData floatingActionButtonTheme,Typography typography,CupertinoThemeData cupertinoOverrideTheme,SnackBarThemeData snackBarTheme,BottomSheetThemeData bottomSheetTheme,PopupMenuThemeData popupMenuTheme,MaterialBannerThemeData bannerTheme,DividerThemeData dividerTheme,ButtonBarThemeData buttonBarTheme,})
属性有点多,找几个常用的来说下
去源码中可以看到很多默认色值都是用isDark来判断的,也许你也想到了什么…
brightness ??= Brightness.light; //默认浅色final bool isDark = brightness == Brightness.dark;splashFactory ??= InkSplash.splashFactory;selectedRowColor ??= Colors.grey[100];unselectedWidgetColor ??= isDark ? Colors.white70 : Colors.black54;// Spec doesn't specify a dark theme secondaryHeaderColor, this is a guess.secondaryHeaderColor ??= isDark ? Colors.grey[700] : primarySwatch[50];textSelectionColor ??= isDark ? accentColor : primarySwatch[200];// TODO(sandrasandeep): change to color provided by Material Design teamcursorColor = cursorColor ?? const Color.fromRGBO(66, 133, 244, 1.0);textSelectionHandleColor ??= isDark ? Colors.tealAccent[400] : primarySwatch[300];backgroundColor ??= isDark ? Colors.grey[700] : primarySwatch[200];dialogBackgroundColor ??= isDark ? Colors.grey[800] : Colors.white;indicatorColor ??= accentColor == primaryColor ? Colors.white : accentColor;hintColor ??= isDark ? const Color(0x80FFFFFF) : const Color(0x8A000000);errorColor ??= Colors.red[700];inputDecorationTheme ??= const InputDecorationTheme();pageTransitionsTheme ??= const PageTransitionsTheme();primaryIconTheme ??= primaryIsDark ? const IconThemeData(color: Colors.white) : const IconThemeData(color: Colors.black);accentIconTheme ??= accentIsDark ? const IconThemeData(color: Colors.white) : const IconThemeData(color: Colors.black);iconTheme ??= isDark ? const IconThemeData(color: Colors.white) : const IconThemeData(color: Colors.black87);platform ??= defaultTargetPlatform;typography ??= Typography(platform: platform);TextTheme defaultTextTheme = isDark ? typography.white : typography.black;TextTheme defaultPrimaryTextTheme = primaryIsDark ? typography.white : typography.black;TextTheme defaultAccentTextTheme = accentIsDark ? typography.white : typography.black;if (fontFamily != null) {defaultTextTheme = defaultTextTheme.apply(fontFamily: fontFamily);defaultPrimaryTextTheme = defaultPrimaryTextTheme.apply(fontFamily: fontFamily);defaultAccentTextTheme = defaultAccentTextTheme.apply(fontFamily: fontFamily);}textTheme = defaultTextTheme.merge(textTheme);primaryTextTheme = defaultPrimaryTextTheme.merge(primaryTextTheme);accentTextTheme = defaultAccentTextTheme.merge(accentTextTheme);materialTapTargetSize ??= MaterialTapTargetSize.padded;applyElevationOverlayColor ??= false;// Used as the default color (fill color) for RaisedButtons. Computing the// default for ButtonThemeData for the sake of backwards compatibility.buttonColor ??= isDark ? primarySwatch[600] : Colors.grey[300];focusColor ??= isDark ? Colors.white.withOpacity(0.12) : Colors.black.withOpacity(0.12);hoverColor ??= isDark ? Colors.white.withOpacity(0.04) : Colors.black.withOpacity(0.04);
使用
ThemeData 的数据是通过 MaterialApp 的 theme 参数来使用的,如:
MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.red,),
