From afd2b3061672f3452c19e8a07631a328ee2b8d6f Mon Sep 17 00:00:00 2001 From: jyuesong <425698907@qq.com> Date: Mon, 17 Jan 2022 17:13:53 +0800 Subject: [PATCH] add setting --- lib/base/sp_const.dart | 3 +- lib/base/theme.dart | 46 +++++++----- lib/module/home/home_page.dart | 47 +++++-------- lib/module/others/other_page.dart | 112 ++++++++++++++++++++++++++++-- 4 files changed, 151 insertions(+), 57 deletions(-) diff --git a/lib/base/sp_const.dart b/lib/base/sp_const.dart index bdc6be9..322510d 100644 --- a/lib/base/sp_const.dart +++ b/lib/base/sp_const.dart @@ -1,2 +1,3 @@ String sp_UserINfo = "userinfo"; -String sp_Host = "host"; \ No newline at end of file +String sp_Host = "host"; +String sp_Theme = "dart_mode"; \ No newline at end of file diff --git a/lib/base/theme.dart b/lib/base/theme.dart index c46ce80..a24b653 100644 --- a/lib/base/theme.dart +++ b/lib/base/theme.dart @@ -1,7 +1,9 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:qinglong_app/base/sp_const.dart'; import 'package:qinglong_app/utils/codeeditor_theme.dart'; +import 'package:qinglong_app/utils/sp_utils.dart'; var themeProvider = ChangeNotifierProvider((ref) => ThemeViewModel()); const Color _primaryColor = Color(0xFF299343); @@ -11,15 +13,31 @@ class ThemeViewModel extends ChangeNotifier { ThemeColors themeColor = LightThemeColors(); - void changeTheme() { - if (currentTheme == darkTheme) { + ThemeViewModel() { + bool dartMode = SpUtil.getBool(sp_Theme, defValue: false); + changeThemeReal(dartMode, false); + } + + bool isInDartMode() { + return SpUtil.getBool(sp_Theme, defValue: false); + } + + void changeThemeReal(bool dark, [bool notify = true]) { + SpUtil.putBool(sp_Theme, dark); + if (!dark) { currentTheme = lightTheme; themeColor = LightThemeColors(); } else { currentTheme = darkTheme; themeColor = DartThemeColors(); } - notifyListeners(); + if (notify) { + notifyListeners(); + } + } + + void changeTheme() { + changeThemeReal(!SpUtil.getBool(sp_Theme, defValue: false), true); } } @@ -57,7 +75,7 @@ ThemeData lightTheme = ThemeData.light().copyWith( secondary: _primaryColor, primary: _primaryColor, ), - scaffoldBackgroundColor: Colors.white, + scaffoldBackgroundColor: const Color(0xfff5f5f5), inputDecorationTheme: const InputDecorationTheme( labelStyle: TextStyle(color: _primaryColor), focusedBorder: UnderlineInputBorder( @@ -94,12 +112,12 @@ ThemeData lightTheme = ThemeData.light().copyWith( ); abstract class ThemeColors { + Color settingBgColor(); + Color taskTitleColor(); Color descColor(); - Color backGround(); - Color pinColor(); Map codeEditorTheme(); @@ -111,11 +129,6 @@ class LightThemeColors extends ThemeColors { return Color(0xff333333); } - @override - Color searchBarBg() { - return const Color(0xffF7F7F7); - } - @override Color pinColor() { return const Color(0xffF7F7F7); @@ -132,8 +145,8 @@ class LightThemeColors extends ThemeColors { } @override - Color backGround() { - return Color(0xffF5F5F5); + Color settingBgColor() { + return Colors.white; } } @@ -143,11 +156,6 @@ class DartThemeColors extends ThemeColors { return Colors.white; } - @override - Color searchBarBg() { - return const Color(0xff2E312E); - } - @override Color pinColor() { return Colors.black12; @@ -164,7 +172,7 @@ class DartThemeColors extends ThemeColors { } @override - Color backGround() { + Color settingBgColor() { return Colors.black; } } diff --git a/lib/module/home/home_page.dart b/lib/module/home/home_page.dart index 76735d5..3ea0bf4 100644 --- a/lib/module/home/home_page.dart +++ b/lib/module/home/home_page.dart @@ -70,43 +70,30 @@ class _HomePageState extends ConsumerState { )); } - actions.add( - Consumer(builder: (context, ref, child) { - return InkWell( - onTap: () { - ref.read(themeProvider).changeTheme(); - }, - child: const Center(child: Text("改变主题")), - ); - }), - ); return Scaffold( appBar: QlAppBar( canBack: false, title: _title, actions: actions, ), - body: ColoredBox( - color: ref.watch(themeProvider).themeColor.backGround(), - child: IndexedStack( - index: _index, - children: [ - const Positioned.fill( - child: TaskPage(), + body: IndexedStack( + index: _index, + children: [ + const Positioned.fill( + child: TaskPage(), + ), + const Positioned.fill( + child: EnvPage(), + ), + Positioned.fill( + child: ConfigPage( + key: configKey, ), - const Positioned.fill( - child: EnvPage(), - ), - Positioned.fill( - child: ConfigPage( - key: configKey, - ), - ), - const Positioned.fill( - child: OtherPage(), - ), - ], - ), + ), + const Positioned.fill( + child: OtherPage(), + ), + ], ), bottomNavigationBar: BottomNavigationBar( items: titles diff --git a/lib/module/others/other_page.dart b/lib/module/others/other_page.dart index 28ab248..529e947 100644 --- a/lib/module/others/other_page.dart +++ b/lib/module/others/other_page.dart @@ -21,10 +21,13 @@ class _OtherPageState extends ConsumerState { crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( - margin: const EdgeInsets.symmetric(horizontal: 15,vertical: 15,), - decoration: const BoxDecoration( - color: Colors.white, - borderRadius: BorderRadius.all( + margin: const EdgeInsets.symmetric( + horizontal: 15, + vertical: 15, + ), + decoration: BoxDecoration( + color: ref.watch(themeProvider).themeColor.settingBgColor(), + borderRadius: const BorderRadius.all( Radius.circular(15), ), ), @@ -34,7 +37,7 @@ class _OtherPageState extends ConsumerState { children: [ Padding( padding: const EdgeInsets.symmetric( - vertical: 10, + vertical: 8, horizontal: 15, ), child: Row( @@ -59,7 +62,7 @@ class _OtherPageState extends ConsumerState { ), Padding( padding: const EdgeInsets.symmetric( - vertical: 10, + vertical: 8, horizontal: 15, ), child: Row( @@ -84,7 +87,102 @@ class _OtherPageState extends ConsumerState { ), Padding( padding: const EdgeInsets.symmetric( - vertical: 10, + vertical: 8, + horizontal: 15, + ), + child: Row( + children: [ + Text( + "查看日志", + style: TextStyle( + color: ref.watch(themeProvider).themeColor.taskTitleColor(), + fontSize: 16, + ), + ), + const Spacer(), + const Icon( + CupertinoIcons.right_chevron, + size: 16, + ), + ], + ), + ), + ], + ), + ), + Container( + margin: const EdgeInsets.symmetric( + horizontal: 15, + vertical: 15, + ), + decoration: BoxDecoration( + color: ref.watch(themeProvider).themeColor.settingBgColor(), + borderRadius: const BorderRadius.all( + Radius.circular(15), + ), + ), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.only( + top: 5, + bottom: 0, + left: 15, + right: 15, + ), + child: Row( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + "夜间模式", + style: TextStyle( + color: ref.watch(themeProvider).themeColor.taskTitleColor(), + fontSize: 16, + ), + ), + const Spacer(), + CupertinoSwitch( + value: ref.watch(themeProvider).isInDartMode(), + onChanged: (open) { + ref.watch(themeProvider).changeThemeReal(open); + }), + ], + ), + ), + const Divider( + indent: 15, + ), + Padding( + padding: const EdgeInsets.symmetric( + vertical: 8, + horizontal: 15, + ), + child: Row( + children: [ + Text( + "查看日志", + style: TextStyle( + color: ref.watch(themeProvider).themeColor.taskTitleColor(), + fontSize: 16, + ), + ), + const Spacer(), + const Icon( + CupertinoIcons.right_chevron, + size: 16, + ), + ], + ), + ), + Divider( + indent: 15, + ), + Padding( + padding: const EdgeInsets.symmetric( + vertical: 8, horizontal: 15, ), child: Row(