上期咱們作了優化,主要針對ScrollView+GridView的使用場景,用了更加合適的組件,這期想作一個主題變動,爲何呢?ios
大屏淺色git
大屏暗黑 小屏淺色 小屏暗黑定義主題類AppTheme,用來配置不一樣的ThemeDatagithub
class AppTheme {
ThemeData themeData;
AppTheme(this.themeData);
// ignore: non_constant_identifier_names
static final AppTheme DARK_THEME = AppTheme(ThemeData.dark());
// ignore: non_constant_identifier_names
static final AppTheme LIGHT_THEME = AppTheme(
ThemeData(brightness: Brightness.light, primaryColor: Colors.grey[50]));
}
複製代碼
DARK_THEME暗黑主題 LIGHT_THEME淺色主題、淺色標題欄默認藍色,這裏我改爲淺灰色canvas
定義StreamController,用來動態變動數據bash
class ThemeBloc {
// ignore: close_sinks
final _themeStreamController = StreamController<AppTheme>();
/// 變動主題調用方法
get changeTheTheme => _themeStreamController.sink.add;
/// 主題數據
get themeData => _themeStreamController.stream;
}
final bloc = ThemeBloc();
複製代碼
給原來的MaterialApp包一層StreamBuilderapp
StreamBuilder<AppTheme>(
initialData: AppTheme.LIGHT_THEME,
stream: bloc.themeData,
builder: (context, AsyncSnapshot<AppTheme> snapshot) {
return MaterialApp(
title: 'Jetpack',
theme: snapshot.data.themeData,
home: PageHome(),
routes: <String, WidgetBuilder>{
"/qq": (context) => PageQQLink(),
"/pageChatGroup": (context) => PageChatGroup(),
},
);
})
複製代碼
initialData 默認數據 stream 將bloc.themeData賦值給它,用來監聽數據變化 snapshot 數據變化的快照,最終交給MaterialApp的theme,從而實現動態變動。如何觸發變動數據呢? 框架
如圖設置頁面添加了開關,代碼以下SwitchListTile(
secondary: Icon(_isEnabled ? Icons.brightness_4 : Icons.brightness_5),
title: Text("暗黑主題"),
subtitle: Text("將主題調成暗黑色"),
value: _isEnabled,
onChanged: (value) {
setState(() {
_isEnabled = value;
});
if (value) {
bloc.changeTheTheme(AppTheme.DARK_THEME);
} else {
bloc.changeTheTheme(AppTheme.LIGHT_THEME);
}
},
)
複製代碼
這裏調用bloc.changeTheTheme來變動主題。 對變動主題就是這麼簡單,你是否是有疑問,我如何修改其餘主題呢(如:文本,按鈕,對話框等)ide
再看下我得實現學習
static final AppTheme LIGHT_THEME = AppTheme(
ThemeData(brightness: Brightness.light, primaryColor: Colors.grey[50]));
複製代碼
我這裏修改了brightness、primaryColor,其實它還有不少,請看優化
ThemeData({
Brightness brightness,
VisualDensity visualDensity,
MaterialColor primarySwatch,
Color primaryColor,
Brightness primaryColorBrightness,
Color primaryColorLight,
Color primaryColorDark,
Color accentColor,
Brightness accentColorBrightness,
Color canvasColor,
Color scaffoldBackgroundColor,
Color bottomAppBarColor,
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,
NavigationRailThemeData navigationRailTheme,
Typography typography,
CupertinoThemeData cupertinoOverrideTheme,
SnackBarThemeData snackBarTheme,
BottomSheetThemeData bottomSheetTheme,
PopupMenuThemeData popupMenuTheme,
MaterialBannerThemeData bannerTheme,
DividerThemeData dividerTheme,
ButtonBarThemeData buttonBarTheme,
}
複製代碼
這裏面得主題你均可以修改,你是否是看到了 AppBarTheme、DialogTheme、TextTheme、BottomSheetThemeData等等,不用我解釋了吧,你應該知道是誰得樣式了吧。
20幾行新增得代碼就搞定了,爲何還要用別人得框架呢?是吧。
計劃真的是趕不上變化,不急,慢慢完善哈。
請轉github代碼查看完整實現
該部份內容後期慢慢給你們更新,請客觀不要着急,固然你能夠參與進來,私聊我就行哦。
網站jetpack.net.cn,歡迎常來,也但願能在你學習Flutter的道路上提供一丟丟的幫助。