add setting

This commit is contained in:
jyuesong
2022-01-17 17:13:53 +08:00
parent 352d4e162c
commit afd2b30616
4 changed files with 151 additions and 57 deletions

View File

@@ -1,2 +1,3 @@
String sp_UserINfo = "userinfo"; String sp_UserINfo = "userinfo";
String sp_Host = "host"; String sp_Host = "host";
String sp_Theme = "dart_mode";

View File

@@ -1,7 +1,9 @@
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.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/codeeditor_theme.dart';
import 'package:qinglong_app/utils/sp_utils.dart';
var themeProvider = ChangeNotifierProvider((ref) => ThemeViewModel()); var themeProvider = ChangeNotifierProvider((ref) => ThemeViewModel());
const Color _primaryColor = Color(0xFF299343); const Color _primaryColor = Color(0xFF299343);
@@ -11,16 +13,32 @@ class ThemeViewModel extends ChangeNotifier {
ThemeColors themeColor = LightThemeColors(); ThemeColors themeColor = LightThemeColors();
void changeTheme() { ThemeViewModel() {
if (currentTheme == darkTheme) { 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; currentTheme = lightTheme;
themeColor = LightThemeColors(); themeColor = LightThemeColors();
} else { } else {
currentTheme = darkTheme; currentTheme = darkTheme;
themeColor = DartThemeColors(); themeColor = DartThemeColors();
} }
if (notify) {
notifyListeners(); notifyListeners();
} }
}
void changeTheme() {
changeThemeReal(!SpUtil.getBool(sp_Theme, defValue: false), true);
}
} }
ThemeData darkTheme = ThemeData.dark().copyWith( ThemeData darkTheme = ThemeData.dark().copyWith(
@@ -57,7 +75,7 @@ ThemeData lightTheme = ThemeData.light().copyWith(
secondary: _primaryColor, secondary: _primaryColor,
primary: _primaryColor, primary: _primaryColor,
), ),
scaffoldBackgroundColor: Colors.white, scaffoldBackgroundColor: const Color(0xfff5f5f5),
inputDecorationTheme: const InputDecorationTheme( inputDecorationTheme: const InputDecorationTheme(
labelStyle: TextStyle(color: _primaryColor), labelStyle: TextStyle(color: _primaryColor),
focusedBorder: UnderlineInputBorder( focusedBorder: UnderlineInputBorder(
@@ -94,12 +112,12 @@ ThemeData lightTheme = ThemeData.light().copyWith(
); );
abstract class ThemeColors { abstract class ThemeColors {
Color settingBgColor();
Color taskTitleColor(); Color taskTitleColor();
Color descColor(); Color descColor();
Color backGround();
Color pinColor(); Color pinColor();
Map<String, TextStyle> codeEditorTheme(); Map<String, TextStyle> codeEditorTheme();
@@ -111,11 +129,6 @@ class LightThemeColors extends ThemeColors {
return Color(0xff333333); return Color(0xff333333);
} }
@override
Color searchBarBg() {
return const Color(0xffF7F7F7);
}
@override @override
Color pinColor() { Color pinColor() {
return const Color(0xffF7F7F7); return const Color(0xffF7F7F7);
@@ -132,8 +145,8 @@ class LightThemeColors extends ThemeColors {
} }
@override @override
Color backGround() { Color settingBgColor() {
return Color(0xffF5F5F5); return Colors.white;
} }
} }
@@ -143,11 +156,6 @@ class DartThemeColors extends ThemeColors {
return Colors.white; return Colors.white;
} }
@override
Color searchBarBg() {
return const Color(0xff2E312E);
}
@override @override
Color pinColor() { Color pinColor() {
return Colors.black12; return Colors.black12;
@@ -164,7 +172,7 @@ class DartThemeColors extends ThemeColors {
} }
@override @override
Color backGround() { Color settingBgColor() {
return Colors.black; return Colors.black;
} }
} }

View File

@@ -70,25 +70,13 @@ class _HomePageState extends ConsumerState<HomePage> {
)); ));
} }
actions.add(
Consumer(builder: (context, ref, child) {
return InkWell(
onTap: () {
ref.read(themeProvider).changeTheme();
},
child: const Center(child: Text("改变主题")),
);
}),
);
return Scaffold( return Scaffold(
appBar: QlAppBar( appBar: QlAppBar(
canBack: false, canBack: false,
title: _title, title: _title,
actions: actions, actions: actions,
), ),
body: ColoredBox( body: IndexedStack(
color: ref.watch(themeProvider).themeColor.backGround(),
child: IndexedStack(
index: _index, index: _index,
children: [ children: [
const Positioned.fill( const Positioned.fill(
@@ -107,7 +95,6 @@ class _HomePageState extends ConsumerState<HomePage> {
), ),
], ],
), ),
),
bottomNavigationBar: BottomNavigationBar( bottomNavigationBar: BottomNavigationBar(
items: titles items: titles
.map( .map(

View File

@@ -21,10 +21,13 @@ class _OtherPageState extends ConsumerState<OtherPage> {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Container( Container(
margin: const EdgeInsets.symmetric(horizontal: 15,vertical: 15,), margin: const EdgeInsets.symmetric(
decoration: const BoxDecoration( horizontal: 15,
color: Colors.white, vertical: 15,
borderRadius: BorderRadius.all( ),
decoration: BoxDecoration(
color: ref.watch(themeProvider).themeColor.settingBgColor(),
borderRadius: const BorderRadius.all(
Radius.circular(15), Radius.circular(15),
), ),
), ),
@@ -34,7 +37,7 @@ class _OtherPageState extends ConsumerState<OtherPage> {
children: [ children: [
Padding( Padding(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
vertical: 10, vertical: 8,
horizontal: 15, horizontal: 15,
), ),
child: Row( child: Row(
@@ -59,7 +62,7 @@ class _OtherPageState extends ConsumerState<OtherPage> {
), ),
Padding( Padding(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
vertical: 10, vertical: 8,
horizontal: 15, horizontal: 15,
), ),
child: Row( child: Row(
@@ -84,7 +87,102 @@ class _OtherPageState extends ConsumerState<OtherPage> {
), ),
Padding( Padding(
padding: const EdgeInsets.symmetric( 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, horizontal: 15,
), ),
child: Row( child: Row(