Skip to content

Commit ce0c854

Browse files
committed
feat:重构数据层 增加 provider demo
1 parent 1589620 commit ce0c854

File tree

8 files changed

+50
-38
lines changed

8 files changed

+50
-38
lines changed

lib/config/theme.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AppTheme {
1515
static int blackColor = 0xFF000000;
1616
static int lineColor = 0xFFEEEEEE;
1717
static getThemeData(String theme) {
18-
//print('==================================getThemeData=$theme');
18+
print('==================================getThemeData=$theme');
1919
mainColor = materialColor[theme];
2020
ThemeData themData = ThemeData(
2121
textTheme: TextTheme(

lib/main.dart

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import 'package:efox_flutter/store/index.dart'; //引用Store 层
77
import 'package:efox_flutter/router/index.dart' show FluroRouter; //路由
88
import 'package:efox_flutter/config/theme.dart' show AppTheme; //主题
99
import 'package:efox_flutter/utils/analytics.dart' as Analytics; //统计
10+
import 'package:oktoast/oktoast.dart' show OKToast;
11+
import 'package:efox_flutter/page/home.dart' as HomePage;
12+
1013
// import './mock/index.dart' as TestCase;
1114
class MainApp extends StatefulWidget {
1215
MainApp() {
@@ -17,6 +20,7 @@ class MainApp extends StatefulWidget {
1720
@override
1821
MainAppState createState() => MainAppState();
1922
}
23+
2024
class MainAppState extends State<MainApp> {
2125
// 定义全局 语言代理
2226
AppLocalizationsDelegate _delegate;
@@ -27,39 +31,47 @@ class MainAppState extends State<MainApp> {
2731
_delegate = AppLocalizationsDelegate();
2832

2933
Future.delayed(Duration.zero, () async {
30-
Store.value<ConfigModel>(context).getTheme();
34+
Store.value<ConfigModel>().getTheme();
3135
});
3236
}
3337

3438
@override
3539
Widget build(BuildContext context) {
3640
Store.of(context);
37-
print('Store.value<ConfigModel>(context)=${Store.value<ConfigModel>(context).theme}');
38-
return MaterialApp(
39-
localeResolutionCallback: (deviceLocale, supportedLocales) {
40-
print(
41-
'deviceLocale=$deviceLocale supportedLocales=$supportedLocales');
42-
Locale _locale = supportedLocales.contains(deviceLocale)
43-
? deviceLocale
44-
: Locale('zh');
45-
return _locale;
41+
return Consumer<ConfigModel>(
42+
builder: (context, configModel, child) {
43+
return MaterialApp(
44+
localeResolutionCallback: (deviceLocale, supportedLocales) {
45+
print(
46+
'deviceLocale=$deviceLocale supportedLocales=$supportedLocales context=$context');
47+
Locale _locale = supportedLocales.contains(deviceLocale)
48+
? deviceLocale
49+
: Locale('zh');
50+
return _locale;
51+
},
52+
onGenerateTitle: (ctx) {
53+
// 设置多语言代理
54+
AppLocalizations.setProxy(setState, _delegate);
55+
return 'flutter';
56+
},
57+
localizationsDelegates: [
58+
GlobalMaterialLocalizations.delegate,
59+
GlobalWidgetsLocalizations.delegate,
60+
_delegate,
61+
],
62+
supportedLocales: ConfigLanguage.supportedLocales,
63+
theme: AppTheme.getThemeData(configModel.theme),
64+
// onGenerateRoute: FluroRouter.router.generator,
65+
home: HomePage.Index(),
66+
navigatorObservers: <NavigatorObserver>[Analytics.observer],
67+
);
4668
},
47-
onGenerateTitle: (ctx) {
48-
// 设置多语言代理
49-
AppLocalizations.setProxy(setState, _delegate);
50-
return 'flutter';
51-
},
52-
localizationsDelegates: [
53-
GlobalMaterialLocalizations.delegate,
54-
GlobalWidgetsLocalizations.delegate,
55-
_delegate,
56-
],
57-
supportedLocales: ConfigLanguage.supportedLocales,
58-
theme: AppTheme.getThemeData(Store.value<ConfigModel>(context).theme),
59-
onGenerateRoute: FluroRouter.router.generator,
60-
navigatorObservers: <NavigatorObserver>[Analytics.observer],
6169
);
6270
}
6371
}
6472

65-
void main() => runApp(Store.init(child: MainApp()));
73+
void main() => runApp(
74+
Store.init(
75+
child: MainApp(),
76+
),
77+
);

lib/package/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Flutter Packages for FLUTTER UI APP
2+
> packages 为纯flutter 组件,沉淀通用组件,通用方法,通用模块

lib/package/router/router.dart

Whitespace-only changes.

lib/page/home.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class _IndexState extends State<Index> {
105105
print('renderDrawer $context');
106106
return Drawer(
107107
child: Consumer<UserModel>(builder: (context, model,child) {
108+
print('render model $model');
108109
return Column(
109110
crossAxisAlignment: CrossAxisAlignment.start,
110111
children: <Widget>[

lib/plugin/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Flutter Plugin for FLUTTER UI APP
2+
> plugin 为混编 flutter 组件,沉淀通用组件,通用方法,通用模块

lib/store/index.dart

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ export './models/author_state_model.dart' show AuthorModel;
1212

1313
class Store {
1414
static BuildContext context;
15-
static of(BuildContext context){
15+
static of(BuildContext context) {
1616
Store.context ??= context;
1717
return context;
1818
}
19-
static init({context, child}) {
20-
Store.context ??= context;
19+
20+
static init({child}) {
2121
return MultiProvider(
2222
child: child,
2323
providers: [
@@ -30,15 +30,9 @@ class Store {
3030

3131
static T value<T>([BuildContext context]) {
3232
context ??= Store.context;
33-
return Provider.of(context);
33+
return Provider.of<T>(context);
3434
}
35+
36+
3537
}
3638

37-
class StoreProvider extends StatelessWidget {
38-
final Widget child;
39-
StoreProvider({this.child});
40-
@override
41-
Widget build(BuildContext context) {
42-
return Store.init(child: child,context: context);
43-
}
44-
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ dependencies:
4141
flutter_downloader: ^1.1.6
4242
open_file: ^2.0.1+1
4343
permission_handler: ^3.0.0
44+
oktoast: ^2.2.0
4445

4546
dev_dependencies:
4647
flutter_test:

0 commit comments

Comments
 (0)