Skip to content

Commit edf22b8

Browse files
committed
feat:完成主題切換
feat:完成保存設置語言
1 parent bcbd230 commit edf22b8

File tree

9 files changed

+138
-82
lines changed

9 files changed

+138
-82
lines changed

lib/config/color.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
Map materialColor = {
3+
'yellow':0xFFFFEB3B,
4+
'orange':0xFFFF9800,
5+
'amber':0xFFFFC107,
6+
'lime':0xFFCDDC39,
7+
'lightGreen':0xFF8BC34A,
8+
'red':0xFFF44336,
9+
'deepOrange':0xFFFF5722,
10+
'blue':0xFF2196F3,
11+
'pink':0xFFE91E63
12+
};

lib/config/theme.dart

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,44 @@
11
import 'package:flutter/material.dart';
2-
2+
import 'color.dart' show materialColor;
33
/**
44
* yello #FFEB3B
55
* red #F44336
66
* blue #2196F3
77
*/
88

99
class AppTheme {
10-
static int yellow = 0xFFFFEB3B;
11-
static int orange = 0xFFFF9800;
12-
static int amber = 0xFFFFC107;
13-
static int lime = 0xFFCDDC39;
14-
static int lightGreen = 0xFF8BC34A;
15-
static int red = 0xFFF44336;
16-
static int deepOrange = 0xFFFF5722;
17-
static int blue = 0xFF2196F3;
18-
static int pink = 0xFFE91E63;
10+
1911
//static int mainColor = 0xFFD32F2F;
20-
static int mainColor = red;
12+
static int mainColor = materialColor['red'];
2113
static int secondColor = 0xFFFFFFFF;
2214
static int thirdColor = 0xFFFAFAFA;
2315
static int greyColor = 0x8A000000;
2416
static int blackColor = 0xFF000000;
2517
static int lineColor = 0xFFEEEEEE;
26-
static ThemeData themData = ThemeData(
27-
textTheme: TextTheme(
28-
body1: TextStyle(
29-
// color: Colors.black,
30-
// fontWeight: FontWeight.bold,
31-
),
32-
),
33-
//platform: TargetPlatform.iOS,
34-
iconTheme: IconThemeData(
35-
size: 32,
36-
color: Color(thirdColor),
37-
opacity: 0.85,
38-
),
39-
// primaryIconTheme 导航栏按钮颜色
40-
primaryIconTheme: IconThemeData(
41-
color: Color(secondColor),
42-
),
43-
accentColor: Colors.grey, // 选中颜色
44-
primaryColor: Color(mainColor), // appbar背景
45-
scaffoldBackgroundColor: Color(secondColor), // 整体的scaffold背景颜色
46-
);
18+
static getThemeData(String theme) {
19+
print('==================================getThemeData=$theme');
20+
mainColor =materialColor[theme];
21+
ThemeData themData = ThemeData(
22+
textTheme: TextTheme(
23+
body1: TextStyle(
24+
// color: Colors.black,
25+
// fontWeight: FontWeight.bold,
26+
),
27+
),
28+
//platform: TargetPlatform.iOS,
29+
iconTheme: IconThemeData(
30+
size: 32,
31+
color: Color(thirdColor),
32+
opacity: 0.85,
33+
),
34+
// primaryIconTheme 导航栏按钮颜色
35+
primaryIconTheme: IconThemeData(
36+
color: Color(secondColor),
37+
),
38+
accentColor: Colors.grey, // 选中颜色
39+
primaryColor: Color(mainColor), // appbar背景
40+
scaffoldBackgroundColor: Color(secondColor), // 整体的scaffold背景颜色
41+
);
42+
return themData;
43+
}
4744
}

lib/lang/index.dart

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:async';
33
import 'dart:convert';
44
import 'package:efox_flutter/lang/config.dart' as I18NConfig;
55
import 'package:flutter/services.dart' show rootBundle;
6+
import 'package:efox_flutter/utils/localstage.dart' show LocalStorage;
67

78
class AppLocalizations {
89
Locale _locale; // language
@@ -21,25 +22,26 @@ class AppLocalizations {
2122
}
2223

2324
// 设置语言切换代理
24-
static void setProxy(Function setState, AppLocalizationsDelegate delegate) async {
25+
static void setProxy(
26+
Function setState, AppLocalizationsDelegate delegate) async {
2527
_setState = setState;
2628
_delegate = delegate;
2729
print("_delegate = $_delegate");
2830
}
2931

3032
static get languageCode => _inst._locale.languageCode;
3133

32-
static void getLanguageJson([Locale locale]) async {
34+
static Future getLanguageJson([Locale locale]) async {
3335
Locale _tmpLocale = _inst._locale;
3436
print(_tmpLocale.languageCode);
3537
String jsonLang;
3638
try {
37-
jsonLang = await rootBundle
38-
.loadString('locale/${_tmpLocale.languageCode}.json');
39+
jsonLang =
40+
await rootBundle.loadString('locale/${_tmpLocale.languageCode}.json');
3941
} catch (e) {
4042
_inst._locale = Locale(I18NConfig.ConfigLanguage.defualtLanguage.code);
41-
jsonLang = await rootBundle
42-
.loadString('locale/${I18NConfig.ConfigLanguage.defualtLanguage.code}.json');
43+
jsonLang = await rootBundle.loadString(
44+
'locale/${I18NConfig.ConfigLanguage.defualtLanguage.code}.json');
4345
}
4446
json.decode(jsonLang);
4547
jsonLanguage = json.decode(jsonLang);
@@ -54,10 +56,12 @@ class AppLocalizations {
5456
: Locale("zh", "CH");
5557
}
5658
_inst._locale = locale;
57-
getLanguageJson(); // 根据语言获取对应的国际化文件
58-
_setState(() {
59-
_delegate = AppLocalizationsDelegate(locale);
60-
});
59+
LocalStorage.set('lang', locale.languageCode);
60+
getLanguageJson().then((v) {
61+
_setState(() {
62+
_delegate = AppLocalizationsDelegate(locale);
63+
});
64+
}); // 根据语言获取对应的国际化文件
6165
}
6266

6367
// get local language
@@ -95,22 +99,28 @@ class AppLocalizations {
9599
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
96100
final Locale locale;
97101

98-
AppLocalizationsDelegate ([this.locale]);
102+
AppLocalizationsDelegate([this.locale]);
99103

100104
@override
101105
bool isSupported(Locale locale) {
102-
return I18NConfig.ConfigLanguage.sopportLanguage.keys.toList().contains(locale.languageCode);
106+
return I18NConfig.ConfigLanguage.sopportLanguage.keys
107+
.toList()
108+
.contains(locale.languageCode);
103109
}
104110

105111
@override
106112
Future<AppLocalizations> load(Locale _locale) async {
107-
Locale _tmpLocale = locale ?? _locale;
108-
return await AppLocalizations.init(_tmpLocale);
113+
String lang = await LocalStorage.get('lang');
114+
Locale __locale = locale ?? _locale;
115+
if (lang != null) {
116+
__locale = Locale(lang);
117+
}
118+
return await AppLocalizations.init(__locale);
109119
}
110120

111121
@override
112122
bool shouldReload(LocalizationsDelegate<AppLocalizations> old) {
113123
// false时 不执行上述重写函数
114124
return false;
115125
}
116-
}
126+
}

lib/main.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class MainAppState extends State<MainApp> {
4444
Widget build(BuildContext context) {
4545
return Store.init(
4646
model: model,
47-
child: MaterialApp(
47+
child: Store.connect(builder: (context, child, model) {
48+
return MaterialApp(
4849
localeResolutionCallback: (deviceLocale, supportedLocales) {
4950
print(
5051
'deviceLocale=$deviceLocale supportedLocales=$supportedLocales');
@@ -65,10 +66,11 @@ class MainAppState extends State<MainApp> {
6566
],
6667
supportedLocales: ConfigLanguage.supportedLocales,
6768
// title: 'Flutter Demo',
68-
theme: AppTheme.themData,
69+
theme: AppTheme.getThemeData(model.config.state.theme),
6970
onGenerateRoute: FluroRouter.router.generator,
7071
navigatorObservers: <NavigatorObserver>[Analytics.observer],
71-
),
72+
);
73+
}),
7274
);
7375
}
7476
}

lib/page/mine/index_2.dart

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
import 'package:flutter/material.dart';
22
import 'package:efox_flutter/lang/index.dart' show AppLocalizations;
3-
import 'package:efox_flutter/router/index.dart' show FluroRouter;
3+
//import 'package:efox_flutter/router/index.dart' show FluroRouter;
44
import 'package:efox_flutter/config/theme.dart' show AppTheme;
5+
import 'package:efox_flutter/store/index.dart' show model;
6+
import 'package:efox_flutter/config/color.dart' show materialColor;
57

68
class _IndexState extends State<Index> {
79
@override
810
void initState() {
911
super.initState();
1012
}
1113

12-
void pop([message]) {
13-
Navigator.pop(context);
14-
if (message != null) {
15-
Scaffold.of(context).showSnackBar(new SnackBar(
16-
content: new Text(message),
17-
));
18-
}
19-
}
20-
2114
/**
2215
* 国际化
2316
*/
@@ -29,21 +22,19 @@ class _IndexState extends State<Index> {
2922
child: Wrap(
3023
children: <Widget>[
3124
ListTile(
32-
//leading: Icon(Icons.label_outline),
3325
title: Text(
34-
AppLocalizations.$t('common_mine_1.cn'),
26+
'中文',
3527
),
3628
onTap: () {
3729
AppLocalizations.changeLanguage(Locale('zh'));
38-
this.pop(AppLocalizations.$t('common_mine_1.success'));
30+
Navigator.pop(context);
3931
},
4032
),
4133
ListTile(
42-
//leading: Icon(Icons.label_outline),
43-
title: Text(AppLocalizations.$t('common_mine_1.en')),
34+
title: Text('English'),
4435
onTap: () {
4536
AppLocalizations.changeLanguage(Locale('en'));
46-
this.pop(AppLocalizations.$t('common_mine_1.success'));
37+
Navigator.pop(context);
4738
},
4839
),
4940
],
@@ -55,6 +46,10 @@ class _IndexState extends State<Index> {
5546

5647
@override
5748
Widget build(BuildContext context) {
49+
List<Widget> _EdageList = [];
50+
materialColor.forEach((k,v){
51+
_EdageList.add(this.Edage(k,v));
52+
});
5853
return Scaffold(
5954
appBar: AppBar(
6055
elevation: 0,
@@ -79,29 +74,28 @@ class _IndexState extends State<Index> {
7974
color: Color(AppTheme.lineColor),
8075
),
8176
ExpansionTile(
77+
leading: Icon(Icons.color_lens),
8278
title: Text(AppLocalizations.$t('common_mine_1.theme')),
8379
children: <Widget>[
8480
Wrap(
85-
//crossAxisAlignment: WrapCrossAlignment.start,
86-
alignment: WrapAlignment.spaceEvenly,
87-
runAlignment: WrapAlignment.spaceEvenly,
88-
children: <Widget>[
89-
this.Edage(AppTheme.yellow),
90-
this.Edage(AppTheme.blue),
91-
this.Edage(AppTheme.orange),
92-
this.Edage(AppTheme.lightGreen),
93-
this.Edage(AppTheme.red),
94-
],
81+
spacing: 10,
82+
runSpacing: 5,
83+
children: _EdageList,
9584
)
9685
],
97-
)
86+
),
87+
Divider(
88+
color: Color(AppTheme.lineColor),
89+
),
9890
],
9991
));
10092
}
10193

102-
Widget Edage(color) {
94+
Widget Edage(name,color) {
10395
return GestureDetector(
104-
onTap: () {},
96+
onTap: () {
97+
model.dispatch('config', 'setTheme', name);
98+
},
10599
child: Container(
106100
color: Color(color),
107101
height: 30,

lib/store/http.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Dio getDio([Options options]) {
66
connectTimeout: 30 * 1000,
77
receiveTimeout: 30 * 1000,
88
)); // with default Options
9-
dio.interceptors.add(LogInterceptor(responseBody: true));
9+
// dio.interceptors.add(LogInterceptor(responseBody: true));
1010
return dio;
1111
}
1212

lib/store/models/config_state_model.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,31 @@ import 'dart:convert';
22
import 'package:efox_flutter/config/index.dart' as Config;
33
import 'package:efox_flutter/store/index.dart' show model;
44
import 'package:efox_flutter/utils/loadAsset.dart' show loadAssets;
5+
import 'package:efox_flutter/utils/localstage.dart' show LocalStorage;
56

67
class ConfigInfo {
78
bool isPro = Config.isPro;
89
String version = '1.0';
910
dynamic env = Config.env;
11+
String theme = 'red';
1012
}
1113

1214
ConfigInfo _appConfigInfo = new ConfigInfo();
1315

1416
class ConfigModel {
1517
get state => _appConfigInfo;
1618

17-
dynamic getVersion () async {
19+
Future getTheme() async {
20+
String theme = await LocalStorage.get('theme');
21+
if (theme != null) {
22+
_appConfigInfo.theme = theme;
23+
}
24+
}
25+
26+
dynamic getVersion() async {
1827
print('version ${model.config.state.env.versionUrl}');
19-
String _version = await loadAssets(model.config.state.env.versionUrl).then((resp) {
28+
String _version =
29+
await loadAssets(model.config.state.env.versionUrl).then((resp) {
2030
Map<String, dynamic> res = json.decode(resp);
2131
return res['version'].toString() ?? '0.1';
2232
}).catchError((err) {
@@ -37,6 +47,10 @@ class ConfigModel {
3747
case 'setVersion':
3848
_appConfigInfo.version = await this.getVersion();
3949
break;
50+
case 'setTheme':
51+
_appConfigInfo.theme = payload;
52+
LocalStorage.set('theme',payload);
53+
break;
4054
}
4155
}
4256
}

lib/store/models/main_state_model.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class MainStateModel extends Model with UserModel {
2121
this.state = {
2222
'config': config,
2323
};
24+
//
25+
config.getTheme();
2426
}
2527

2628
/**

0 commit comments

Comments
 (0)