Flutter適配深色模式的方法(DarkMode)
1.瞎叨叨
也不知道寫點什么,本來想寫寫Flutter的集成測試。因為前一陣子給flutter_deer寫了一套,不過感覺也沒啥內(nèi)容,寫不了幾句話就放棄了。(其實本篇內(nèi)容也不多。。。)
那就寫寫最近在做的事情。沒錯,就是文章標(biāo)題提到的適配深色模式(DarkMode),也可以說是實現(xiàn)夜間模式的功能。相信許多iOS的同學(xué)最近都比較關(guān)注,畢竟iOS 13上個月推送更新了。
說適配的原因是因為在iOS 13 和 Android 10系統(tǒng)上它都屬于新特性。適配的目的是為了達(dá)到應(yīng)用的主題隨著系統(tǒng)主題模式的切換而變化,給用戶更好的一致性體驗。與它類似的就是系統(tǒng)語言的設(shè)置,當(dāng)系統(tǒng)設(shè)置某種語言時,應(yīng)用內(nèi)的文字也相應(yīng)變化。
好在Flutter也提供了適配的入口,使得我們可以一次適配兩個平臺。我手上的小米mix2s雖然是Android 9 的,沒想到也能適配。
2.準(zhǔn)備工作
下面我就說說我在適配 flutter_deer 中的經(jīng)驗, Flutter版本1.9.1。
首先是規(guī)范問題,標(biāo)題、副標(biāo)題、分割線、各種背景等顏色,以及深色模式下相對應(yīng)的顏色一定要先規(guī)范起來。否則你自己不僅被這些顏色搞得眼冒金星,同時應(yīng)用也沒有一個統(tǒng)一的風(fēng)格。
3.適配開始
1.全局調(diào)整
Flutter 在 MaterialApp 中提供了 theme 與 darkTheme 兩個入口讓我們設(shè)置兩種模式下的顏色及文字樣式。接收的 ThemeData 中近乎涵蓋了所有Material Widget中所使用的顏色及主題。( Cupertino 系列組件官方還在適配中,所以Flutter版本1.9.1暫不支持。)
通過配置 theme 與 darkTheme 可以讓我們省去很多的判斷代碼,比如我的分割線在不同模式下是兩種不同顏色,我不可能每使用一次,就在使用的地方去判斷一次。通過配置全局 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具體的使用地方需要自己去翻看源碼及注釋才能知道,所以這是一個比較費力的過程。
其實這里你也可以利用某些“坑位”,比如應(yīng)用內(nèi)的另外一種功能文字在字號、顏色上都與主文字不一樣,使用的地方還很多,每次使用再判斷也很麻煩,這樣就可以設(shè)置到未使用的屬性上,比如上面代碼中的 subtitle 。這樣使用時就可以通過調(diào)用 Theme.of(context).textTheme.subtitle 來實現(xiàn)。
Text( "文字", style: Theme.of(context).textTheme.subtitle )
需要注意的是: 畢竟是全局配置,盡量保持通用,不要影響其他widget也是要考慮的地方。
這部分配置完成后,你需要的是"去同存異"。
比如你指定的文字樣式與全局配置相同時,就需要刪除它。
如果文字顏色相同,但是字號不同。那就刪除顏色配置信息,保留字號設(shè)置:
Text( "僅保留不同信息", style: const TextStyle( fontSize: 12.0, ) )
因為Text的源碼中就是通過 merge 方法來合并全局配置與局部配置。 merge 中其實就是調(diào)用 copyWith 來實現(xiàn)的。所以也可以這樣寫:
Text( "僅保留不同信息", style: Theme.of(context).textTheme.body1.copyWith(fontSize: 12.0) )
顏色不同。因為深色模式主要就是顏色變化,所以可以考慮上面的“subtitle”方案。如果僅有幾處,可以封裝一些方法統(tǒng)一判斷處理。
2.局部調(diào)整
在經(jīng)過全局的配置后,大多數(shù)適配問題得到了解決。但可能還有一些細(xì)節(jié)要調(diào)整,比如圖標(biāo)、個別的文字顏色、背景色。這時需要的就是如何判斷深色模式:
bool isDarkMode(BuildContext context){
return Theme.of(context).brightness == Brightness.dark;
}
這里的 brightness 就是上面在全局配置 ThemeData 中指定的 brightness 。
Tips:
- 有些純色的小圖標(biāo)可以直接使用
Image.asset的color來修改。 Button的textColor屬性最好還是局部處理,因為源碼中“非黑即白”,我很痛苦??!
/// 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.功能拓展
如果你適配好了深色模式,其實可以稍微拓展一下這個功能。我想到了微信中的多語言功能,在多語言這類功能中,默認(rèn)選項是“跟隨系統(tǒng)”,當(dāng)然你也可以指定某種語言。
按照這個思路我在設(shè)置中添加了“夜間模式”的功能,默認(rèn)也是跟隨系統(tǒng),當(dāng)然你也可以手動的開啟和關(guān)閉。

這里暫時有個問題,在iOS手機(jī)上開啟深色模式,當(dāng)我應(yīng)用內(nèi)關(guān)閉深色模式后,狀態(tài)欄文字無法變?yōu)楹谏?/a>。
問題主要還是Flutter 1.9.1的版本并沒有適配iOS 13 Status Bar增的UIStatusBarStyleDarkContent 。

這里暫時有個問題,在iOS手機(jī)上開啟深色模式,當(dāng)我應(yīng)用內(nèi)關(guān)閉深色模式后, 狀態(tài)欄無法變?yōu)楹谏?。這個問題Flutter的issues中也有人反饋了,期待官方的適配修復(fù)吧。
上述這些,基本就是適配深色模式主要內(nèi)容了。本身沒有什么復(fù)雜的,主是是個細(xì)心活。
說了這么多,最后放幾張適配的效果圖給大家看看:








詳細(xì)的代碼以及實現(xiàn)細(xì)節(jié),可以參看flutter_deer 的代碼。深色模式相關(guān)的設(shè)計圖也已經(jīng)同步更新了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Jetpack?Compose重寫TopAppBar實現(xiàn)標(biāo)題多行折疊詳解
這篇文章主要為大家介紹了Jetpack?Compose重寫TopAppBar實現(xiàn)標(biāo)題多行折疊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android設(shè)備上非root的抓包實現(xiàn)方法(Tcpdump方法)
通常我們在Android應(yīng)用中執(zhí)行某個命令時會使用“Runtime.getRuntime().exec("命令路徑")”這種方式,但是當(dāng)我們執(zhí)行抓包操作時,使用這條命令無論如何都不行,通過下面代碼打印結(jié)果發(fā)現(xiàn),該命令一定要在root權(quán)限下才能執(zhí)行,具體實現(xiàn)思路,請參考本教程2016-11-11
Android5.0以上實現(xiàn)全透明的狀態(tài)欄方法(仿網(wǎng)易云界面)
下面小編就為大家分享一篇Android5.0以上實現(xiàn)全透明的狀態(tài)欄方法(仿網(wǎng)易云界面),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
OnSharedPreferenceChangeListener詳解及出現(xiàn)不觸發(fā)解決辦法
本文主要介紹 Android OnSharedPreferenceChangeListener的知識,在Android應(yīng)用開發(fā)過程中會遇到監(jiān)聽器不觸發(fā)事件問題,這里介紹了相應(yīng)的解決辦法2016-08-08
android UI進(jìn)階之a(chǎn)ndroid中隱藏的layout 抽屜的使用方法
android UI進(jìn)階之a(chǎn)ndroid中隱藏的layout 抽屜的使用方法,需要的朋友可以參考一下2013-05-05
Android連接MySQL數(shù)據(jù)庫并進(jìn)行增刪改查操作示例講解
這篇文章主要介紹了Android 連接MySQL數(shù)據(jù)庫并進(jìn)行增刪改查操作示例講解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

