Skip to content

Commit

Permalink
调整外观设置
Browse files Browse the repository at this point in the history
  • Loading branch information
imhansai committed Nov 2, 2020
1 parent 54b5f34 commit 49fe91c
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 103 deletions.
11 changes: 11 additions & 0 deletions lib/util/appearance.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// 外观
enum Appearance {
/// 浅色模式
light,

/// 暗黑模式
dark,

/// 跟随系统
auto
}
15 changes: 15 additions & 0 deletions lib/util/sp_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:shared_preferences/shared_preferences.dart';

/// 数据存储类
class SharedPreferencesUtil {
static SharedPreferences prefs;

static Future<SharedPreferences> getPrefs() async {
if (prefs == null) {
SharedPreferences sharedPreferences =
await SharedPreferences.getInstance();
prefs = sharedPreferences;
}
return prefs;
}
}
53 changes: 24 additions & 29 deletions lib/util/theme_util.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
import 'package:shared_preferences/shared_preferences.dart';
import 'package:weifangbus/util/appearance.dart';
import 'package:weifangbus/util/sp_util.dart';

/// 外观工具类
class ThemeUtil {
static var prefs;
class AppearanceUtil {
/// 存储的key
static var key = 'appearance';

static getPrefs() async {
if (prefs == null) {
SharedPreferences sharedPreferences =
await SharedPreferences.getInstance();
prefs = sharedPreferences;
}
return prefs;
}

/// 获取选择的外观值
static getModelValue() async {
var prefs = await getPrefs();
var modelValue = prefs.getInt('model_value') ?? 2;
return modelValue;
/// 获取选择的外观
static Future<Appearance> getAppearance() async {
var prefs = await SharedPreferencesUtil.getPrefs();
var value = prefs.getInt(key);
return Appearance.values
.where((element) => element.index == (value ?? Appearance.auto.index))
.single;
}

/// 持久化选择的外观值
static saveModelValue(int modelValue) async {
var prefs = await getPrefs();
prefs.setInt('model_value', modelValue);
/// 持久化选择的外观
static void saveAppearance(Appearance appearance) async {
var prefs = await SharedPreferencesUtil.getPrefs();
prefs.setInt(key, appearance.index);
}

/// 展示外观值
static modelValue2String(int modelValue) {
/// 展示外观
static String appearanceStr(Appearance appearance) {
var str;
switch (modelValue) {
case 0:
str = '浅色外观';
switch (appearance) {
case Appearance.light:
str = '浅色模式';
break;
case 1:
str = '深色外观';
case Appearance.dark:
str = '暗黑模式';
break;
default:
case Appearance.auto:
str = '跟随系统';
}
return str;
Expand Down
43 changes: 21 additions & 22 deletions lib/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:weifangbus/util/appearance.dart';
import 'package:weifangbus/util/theme_util.dart';
import 'package:weifangbus/view/home.dart';
import 'package:weifangbus/view/store/appearance_provider.dart';
import 'package:weifangbus/view/store/news_model.dart';
import 'package:weifangbus/view/store/theme_provider.dart';

/// 设置 Provider
class WeiFangBusApp extends StatefulWidget {
Expand All @@ -16,27 +17,23 @@ class WeiFangBusApp extends StatefulWidget {
}

class _WeiFangBusApp extends State<WeiFangBusApp> {
/// 0/浅色 1/深色 2/跟随系统
int _modelValue;
/// 外观
Appearance _appearance = Appearance.auto;

/// 获取选择的外观值
_getModelValue() async {
var modelValue = await ThemeUtil.getModelValue();
// print('app 持久化获取外观: $modelValue');
if (_modelValue == null || _modelValue != modelValue) {
// print('app 持久化设置外观: $modelValue 原外观: $_modelValue');
_getAppearance() async {
var appearance = await AppearanceUtil.getAppearance();
if (appearance != null && _appearance != appearance) {
setState(() {
_modelValue = modelValue;
_appearance = appearance;
});
}
}

/// 更改外观
_changeTheme(int modelValue) {
// print('app 监听获取外观: $modelValue');
if (modelValue != null && _modelValue != modelValue) {
// print('app 监听设置外观: $modelValue 原外观: $_modelValue');
_modelValue = modelValue;
_changeAppearance(Appearance appearance) {
if (appearance != null && _appearance != appearance) {
_appearance = appearance;
}
}

Expand All @@ -45,7 +42,7 @@ class _WeiFangBusApp extends State<WeiFangBusApp> {
super.initState();
// 强制竖屏
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
_getModelValue();
_getAppearance();
}

@override
Expand All @@ -57,16 +54,16 @@ class _WeiFangBusApp extends State<WeiFangBusApp> {
create: (_) => NewsModel(),
),
// 外观
ChangeNotifierProvider<ThemeProvider>(
create: (_) => ThemeProvider(),
ChangeNotifierProvider<AppearanceProvider>(
create: (_) => AppearanceProvider(),
),
],
child: Builder(
builder: (context) {
int modelValue = context.watch<ThemeProvider>().modelValue;
_changeTheme(modelValue);
// 0/浅色 1/深色 2/跟随系统
return _modelValue == 2
Appearance appearance =
context.watch<AppearanceProvider>().appearance;
_changeAppearance(appearance);
return _appearance == Appearance.auto
? MaterialApp(
title: "潍坊公交",
theme: ThemeData(),
Expand All @@ -83,7 +80,9 @@ class _WeiFangBusApp extends State<WeiFangBusApp> {
)
: MaterialApp(
title: "潍坊公交",
theme: _modelValue == 1 ? ThemeData.dark() : ThemeData(),
theme: _appearance == Appearance.dark
? ThemeData.dark()
: ThemeData(),
home: Home(),
localizationsDelegates: [
// 本地化的代理类
Expand Down
28 changes: 13 additions & 15 deletions lib/view/more/settings/settings.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:weifangbus/util/appearance.dart';
import 'package:weifangbus/util/theme_util.dart';
import 'package:weifangbus/view/more/settings/theme_manager.dart';
import 'package:weifangbus/view/store/theme_provider.dart';
import 'package:weifangbus/view/store/appearance_provider.dart';

/// 设置页
class Settings extends StatefulWidget {
Expand All @@ -12,15 +13,13 @@ class Settings extends StatefulWidget {

class _SettingsState extends State<Settings> {
/// 选择的外观
String _modelValueStr = '';
Appearance _appearance = Appearance.auto;

@override
Widget build(BuildContext context) {
var modelValue = context.watch<ThemeProvider>().modelValue;
// print('监听外观: $modelValue');
if (modelValue != null) {
// print('外观改变: $modelValue');
_modelValueStr = ThemeUtil.modelValue2String(modelValue);
var appearance = context.watch<AppearanceProvider>().appearance;
if (appearance != null) {
_appearance = appearance;
}
return Scaffold(
appBar: AppBar(
Expand All @@ -32,7 +31,7 @@ class _SettingsState extends State<Settings> {
tiles: [
ListTile(
leading: Icon(
Icons.nightlight_round,
Icons.nights_stay,
color: Colors.blue,
),
title: Text('外观管理'),
Expand All @@ -41,31 +40,30 @@ class _SettingsState extends State<Settings> {
context,
MaterialPageRoute(
builder: (BuildContext context) {
return ThemeManager();
return AppearanceManager();
},
),
);
},
trailing: Text(_modelValueStr),
trailing: Text(AppearanceUtil.appearanceStr(_appearance)),
),
],
).toList(),
),
);
}

_getModelValueStr() async {
_getAppearance() async {
// print('外观设置 初始化');
var modelValue = await ThemeUtil.getModelValue();
var modelValue2String = ThemeUtil.modelValue2String(modelValue);
var appearance = await AppearanceUtil.getAppearance();
setState(() {
_modelValueStr = modelValue2String;
_appearance = appearance;
});
}

@override
void initState() {
super.initState();
_getModelValueStr();
_getAppearance();
}
}
43 changes: 20 additions & 23 deletions lib/view/more/settings/theme_manager.dart
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:weifangbus/util/appearance.dart';
import 'package:weifangbus/util/theme_util.dart';
import 'package:weifangbus/view/store/theme_provider.dart';
import 'package:weifangbus/view/store/appearance_provider.dart';

/// 外观管理
class ThemeManager extends StatefulWidget {
class AppearanceManager extends StatefulWidget {
@override
_ThemeManagerState createState() => _ThemeManagerState();
_AppearanceManagerState createState() => _AppearanceManagerState();
}

class _ThemeManagerState extends State<ThemeManager> {
/// 0/浅色 1/深色 2/跟随系统
int _modelValue;
class _AppearanceManagerState extends State<AppearanceManager> {
Appearance _appearance = Appearance.auto;

/// 获取选择的外观值
_getModelValue() async {
var modelValue = await ThemeUtil.getModelValue();
/// 获取选择的外观
_getAppearance() async {
var appearance = await AppearanceUtil.getAppearance();
setState(() {
_modelValue = modelValue;
_appearance = appearance;
});
}

@override
void initState() {
super.initState();
_getModelValue();
_getAppearance();
}

/// 展示和设置外观
_showAndSetTheme(BuildContext context, int modelValue) {
print('外观切换至: ${ThemeUtil.modelValue2String(modelValue)}');
ThemeUtil.saveModelValue(modelValue);
_showAndSetAppearance(BuildContext context, Appearance appearance) {
print('外观切换至: ${AppearanceUtil.appearanceStr(appearance)}');
AppearanceUtil.saveAppearance(appearance);
setState(() {
_modelValue = modelValue;
context.read<ThemeProvider>().changeModel(modelValue);
_appearance = appearance;
context.read<AppearanceProvider>().changeModel(appearance);
});
}

@override
Widget build(BuildContext context) {
var tiles = ThemeAppearance.values
var tiles = Appearance.values
.map((e) => ListTile(
title: Text(
ThemeUtil.modelValue2String(e.index),
AppearanceUtil.appearanceStr(e),
),
onTap: () {
_showAndSetTheme(context, e.index);
_showAndSetAppearance(context, e);
},
trailing: _modelValue == e.index ? Text('已选择') : SizedBox(),
trailing: _appearance == e ? Text('已选择') : SizedBox(),
))
.toList();
return Scaffold(
Expand All @@ -63,6 +63,3 @@ class _ThemeManagerState extends State<ThemeManager> {
);
}
}

/// 主题外观
enum ThemeAppearance { light, dark, auto }
14 changes: 14 additions & 0 deletions lib/view/store/appearance_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:flutter/material.dart';
import 'package:weifangbus/util/appearance.dart';

/// 外观
class AppearanceProvider with ChangeNotifier {
Appearance _appearance;

Appearance get appearance => _appearance;

changeModel(Appearance appearance) {
_appearance = appearance;
notifyListeners();
}
}
14 changes: 0 additions & 14 deletions lib/view/store/theme_provider.dart

This file was deleted.

0 comments on commit 49fe91c

Please sign in to comment.