欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter適配深色模式的方法(DarkMode)

 更新時(shí)間:2019年10月14日 14:29:55   作者:唯鹿  
這篇文章主要介紹了Flutter適配深色模式的方法(DarkMode),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.瞎叨叨

也不知道寫點(diǎn)什么,本來想寫寫Flutter的集成測(cè)試。因?yàn)榍耙魂囎咏oflutter_deer寫了一套,不過感覺也沒啥內(nèi)容,寫不了幾句話就放棄了。(其實(shí)本篇內(nèi)容也不多。。。)

那就寫寫最近在做的事情。沒錯(cuò),就是文章標(biāo)題提到的適配深色模式(DarkMode),也可以說是實(shí)現(xiàn)夜間模式的功能。相信許多iOS的同學(xué)最近都比較關(guān)注,畢竟iOS 13上個(gè)月推送更新了。

說適配的原因是因?yàn)樵趇OS 13 和 Android 10系統(tǒng)上它都屬于新特性。適配的目的是為了達(dá)到應(yīng)用的主題隨著系統(tǒng)主題模式的切換而變化,給用戶更好的一致性體驗(yàn)。與它類似的就是系統(tǒng)語言的設(shè)置,當(dāng)系統(tǒng)設(shè)置某種語言時(shí),應(yīng)用內(nèi)的文字也相應(yīng)變化。

好在Flutter也提供了適配的入口,使得我們可以一次適配兩個(gè)平臺(tái)。我手上的小米mix2s雖然是Android 9 的,沒想到也能適配。

2.準(zhǔn)備工作

下面我就說說我在適配 flutter_deer 中的經(jīng)驗(yàn), Flutter版本1.9.1。

首先是規(guī)范問題,標(biāo)題、副標(biāo)題、分割線、各種背景等顏色,以及深色模式下相對(duì)應(yīng)的顏色一定要先規(guī)范起來。否則你自己不僅被這些顏色搞得眼冒金星,同時(shí)應(yīng)用也沒有一個(gè)統(tǒng)一的風(fēng)格。

3.適配開始

1.全局調(diào)整

Flutter 在 MaterialApp 中提供了 themedarkTheme 兩個(gè)入口讓我們?cè)O(shè)置兩種模式下的顏色及文字樣式。接收的 ThemeData 中近乎涵蓋了所有Material Widget中所使用的顏色及主題。( Cupertino 系列組件官方還在適配中,所以Flutter版本1.9.1暫不支持。)

通過配置 themedarkTheme 可以讓我們省去很多的判斷代碼,比如我的分割線在不同模式下是兩種不同顏色,我不可能每使用一次,就在使用的地方去判斷一次。通過配置全局 dividerTheme ,我們就可以直接使用 Divider() 或者 BorderSide 。

ThemeData(
  dividerTheme: DividerThemeData(
  color: isDarkMode ? Colours.dark_line : Colours.line,
  space: 0.6,
  thickness: 0.6
  )
 );

同樣我們的頁面背景色、文字樣式都可以這樣配置。以下就是deer中最終整理的配置。

ThemeData(
  errorColor: isDarkMode ? Colours.dark_red : Colours.red,
  brightness: isDarkMode ? Brightness.dark : Brightness.light,
  primaryColor: isDarkMode ? Colours.dark_app_main : Colours.app_main,
  accentColor: isDarkMode ? Colours.dark_app_main : Colours.app_main,
  // Tab指示器顏色
  indicatorColor: isDarkMode ? Colours.dark_app_main : Colours.app_main,
  // 頁面背景色
  scaffoldBackgroundColor: isDarkMode ? Colours.dark_bg_color : Colors.white,
  // 主要用于Material背景色
  canvasColor: isDarkMode ? Colours.dark_material_bg : Colors.white,
  // 文字選擇色(輸入框復(fù)制粘貼菜單)
  textSelectionColor: Colours.app_main.withAlpha(70),
  textSelectionHandleColor: Colours.app_main,
  textTheme: TextTheme(
  // TextField輸入文字顏色
  subhead: isDarkMode ? TextStyles.textDark : TextStyles.text,
  // Text默認(rèn)文字樣式
  body1: isDarkMode ? TextStyles.textDark : TextStyles.text,
  // 這里用于小文字樣式
  subtitle: isDarkMode ? TextStyles.textDarkGray12 : TextStyles.textGray12,
  ),
  inputDecorationTheme: InputDecorationTheme(
  hintStyle: isDarkMode ? TextStyles.textHint14 : TextStyles.textDarkGray14,
  ),
  appBarTheme: AppBarTheme(
  elevation: 0.0,
  color: isDarkMode ? Colours.dark_bg_color : Colors.white,
  brightness: isDarkMode ? Brightness.dark : Brightness.light,
  ),
  dividerTheme: DividerThemeData(
  color: isDarkMode ? Colours.dark_line : Colours.line,
  space: 0.6,
  thickness: 0.6
  )
 );

使用:

MaterialApp (
  title: 'Flutter Deer',
  theme: getTheme(),
  darkTheme: getTheme(isDarkMode: true),
  home: TestPage()
 );		

當(dāng)然有些Widget沒有使用到,所以也就沒有去適配。以上這些color、theme具體的使用地方需要自己去翻看源碼及注釋才能知道,所以這是一個(gè)比較費(fèi)力的過程。

其實(shí)這里你也可以利用某些“坑位”,比如應(yīng)用內(nèi)的另外一種功能文字在字號(hào)、顏色上都與主文字不一樣,使用的地方還很多,每次使用再判斷也很麻煩,這樣就可以設(shè)置到未使用的屬性上,比如上面代碼中的 subtitle 。這樣使用時(shí)就可以通過調(diào)用 Theme.of(context).textTheme.subtitle 來實(shí)現(xiàn)。

Text(
 "文字", 
 style: Theme.of(context).textTheme.subtitle
)

需要注意的是: 畢竟是全局配置,盡量保持通用,不要影響其他widget也是要考慮的地方。

這部分配置完成后,你需要的是"去同存異"。

比如你指定的文字樣式與全局配置相同時(shí),就需要?jiǎng)h除它。

如果文字顏色相同,但是字號(hào)不同。那就刪除顏色配置信息,保留字號(hào)設(shè)置:

Text(
 "僅保留不同信息",
 style: const TextStyle(
 fontSize: 12.0,
 )
)

因?yàn)門ext的源碼中就是通過 merge 方法來合并全局配置與局部配置。 merge 中其實(shí)就是調(diào)用 copyWith 來實(shí)現(xiàn)的。所以也可以這樣寫:

Text(
 "僅保留不同信息",
 style: Theme.of(context).textTheme.body1.copyWith(fontSize: 12.0)
)

顏色不同。因?yàn)樯钌J街饕褪穷伾兓?,所以可以考慮上面的“subtitle”方案。如果僅有幾處,可以封裝一些方法統(tǒng)一判斷處理。

2.局部調(diào)整

在經(jīng)過全局的配置后,大多數(shù)適配問題得到了解決。但可能還有一些細(xì)節(jié)要調(diào)整,比如圖標(biāo)、個(gè)別的文字顏色、背景色。這時(shí)需要的就是如何判斷深色模式:

 bool isDarkMode(BuildContext context){
 return Theme.of(context).brightness == Brightness.dark;
 }

這里的 brightness 就是上面在全局配置 ThemeData 中指定的 brightness 。

Tips:

  1. 有些純色的小圖標(biāo)可以直接使用 Image.assetcolor 來修改。
  2. ButtontextColor 屬性最好還是局部處理,因?yàn)樵创a中“非黑即白”,我很痛苦啊!
 /// The foreground color of the [button]'s text and icon.
 ///
 /// If [button] is not [MaterialButton.enabled], the value of
 /// [getDisabledTextColor] is returned. If the button is enabled and
 /// [buttonTextColor] is non-null, then [buttonTextColor] is returned.
 ///
 /// Otherwise the text color depends on the value of [getTextTheme]
 /// and [getBrightness].
 ///
 /// * [ButtonTextTheme.normal]: [Colors.white] is used if [getBrightness]
 /// resolves to [Brightness.dark]. [Colors.black87] is used if
 /// [getBrightness] resolves to [Brightness.light].
 /// * [ButtonTextTheme.accent]: [colorScheme.secondary].
 /// * [ButtonTextTheme.primary]: If [getFillColor] is dark then [Colors.white],
 /// otherwise if [button] is a [FlatButton] or an [OutlineButton] then
 /// [colorScheme.primary], otherwise [Colors.black].
 Color getTextColor(MaterialButton button) {
 if (!button.enabled)
  return getDisabledTextColor(button);

 if (button.textColor != null)
  return button.textColor;

 switch (getTextTheme(button)) {
  case ButtonTextTheme.normal:
  return getBrightness(button) == Brightness.dark ? Colors.white : Colors.black87;

  case ButtonTextTheme.accent:
  return colorScheme.secondary;

  case ButtonTextTheme.primary: {
  final Color fillColor = getFillColor(button);
  final bool fillIsDark = fillColor != null
   ? ThemeData.estimateBrightnessForColor(fillColor) == Brightness.dark
   : getBrightness(button) == Brightness.dark;
  if (fillIsDark)
   return Colors.white;
  if (button is FlatButton || button is OutlineButton)
   return colorScheme.primary;
  return Colors.black;
  }
 }

 assert(false);
 return null;
 }

3.功能拓展

如果你適配好了深色模式,其實(shí)可以稍微拓展一下這個(gè)功能。我想到了微信中的多語言功能,在多語言這類功能中,默認(rèn)選項(xiàng)是“跟隨系統(tǒng)”,當(dāng)然你也可以指定某種語言。

按照這個(gè)思路我在設(shè)置中添加了“夜間模式”的功能,默認(rèn)也是跟隨系統(tǒng),當(dāng)然你也可以手動(dòng)的開啟和關(guān)閉。

這里暫時(shí)有個(gè)問題,在iOS手機(jī)上開啟深色模式,當(dāng)我應(yīng)用內(nèi)關(guān)閉深色模式后,狀態(tài)欄文字無法變?yōu)楹谏?/a>。

問題主要還是Flutter 1.9.1的版本并沒有適配iOS 13 Status Bar增的UIStatusBarStyleDarkContent 。

這里暫時(shí)有個(gè)問題,在iOS手機(jī)上開啟深色模式,當(dāng)我應(yīng)用內(nèi)關(guān)閉深色模式后, 狀態(tài)欄無法變?yōu)楹谏?。這個(gè)問題Flutter的issues中也有人反饋了,期待官方的適配修復(fù)吧。

上述這些,基本就是適配深色模式主要內(nèi)容了。本身沒有什么復(fù)雜的,主是是個(gè)細(xì)心活。

說了這么多,最后放幾張適配的效果圖給大家看看:

詳細(xì)的代碼以及實(shí)現(xiàn)細(xì)節(jié),可以參看flutter_deer 的代碼。深色模式相關(guān)的設(shè)計(jì)圖也已經(jīng)同步更新了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論