Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3bf97d3

Browse files
committed
feat:重构数据层 增加 provider demo
1 parent f3243d0 commit 3bf97d3

File tree

13 files changed

+196
-149
lines changed

13 files changed

+196
-149
lines changed

‎lib/components/widget_comp.dart‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ class IndexState extends State<Index> {
7575
this._bodyList.length = 0;
7676
String mdText = await this.getMdFile(widget.mdUrl);
7777
String nameKey = AuthorList.list[widget.title];
78-
if (nameKey != null) {
78+
/*if (nameKey != null) {
7979
this._bodyList.add(authorTile(nameKey));
8080
this._bodyList.add(Divider());
81-
}
81+
}*/
8282
print('文档完成长度:${mdText.length}');
8383
if (mdText.length > 30) {
8484
this._bodyList.add(await markdown_comp.Index(mdText));
@@ -152,7 +152,7 @@ class IndexState extends State<Index> {
152152

153153
getActions(context) {
154154
return [
155-
IconButton(
155+
/*IconButton(
156156
color: Color(AppTheme.blackColor),
157157
icon: Icon(
158158
Icons.code
@@ -172,7 +172,7 @@ class IndexState extends State<Index> {
172172
'/webview?title=${widget.title}&url=${Uri.encodeComponent(widget.originCodeUrl)}',
173173
);
174174
},
175-
),
175+
),*/
176176
IconButton(
177177
icon: Icon(Icons.share),
178178
color: Color(AppTheme.blackColor),

‎lib/controller/index.dart‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import 'package:efox_flutter/utils/appVersion.dart' show AppVersion;
55

66
void initState() async {
77
// 获取版本号
8-
Store.valueNotCtx<ConfigModel>().getAppVersion();
8+
Store.value<ConfigModel>().getAppVersion();
99
// 登录
10-
Store.valueNotCtx<UserModel>().getLocalUserInfo().then((res) {
10+
/*Store.value<UserModel>().getLocalUserInfo().then((res) {
1111
if (res) {
12-
Store.valueNotCtx<UserModel>().getUserStar();
12+
Store.value<UserModel>().getUserStar();
1313
}
14-
});
15-
Store.valueNotCtx<UserModel>().getFlutterUIStar();
14+
});*/
15+
// Store.value<UserModel>().getFlutterUIStar();
1616
Future.delayed(Duration(seconds: 3), () {
17-
AppVersion().check(Store.widgetCtx);
17+
AppVersion().check(Store.context);
1818
});
1919
}

‎lib/http/loading.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void beforeRequest(uri, Map<dynamic, dynamic> options) {
1616
void afterResponse(uri, Map<dynamic, dynamic> options) {
1717
dict.remove(uri);
1818
if (dict.length == 0 && loading == true) {
19-
Navigator.of(Store.widgetCtx, rootNavigator: true).pop('close dialog');
19+
Navigator.of(Store.context, rootNavigator: true).pop('close dialog');
2020
loading = false;
2121
}
2222
}
@@ -31,7 +31,7 @@ void showAppLoading(Map<dynamic, dynamic> options) {
3131
'text': options['text'] ?? 'loading...'
3232
};
3333
showDialog(
34-
context: Store.widgetCtx,
34+
context: Store.context,
3535
builder: (context) {
3636
return LoadingDialog(text: options['text']);
3737
},

‎lib/main.dart‎

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import 'package:flutter_localizations/flutter_localizations.dart'; //语言包
33
import 'package:efox_flutter/lang/index.dart'
44
show AppLocalizationsDelegate, AppLocalizations;
55
import 'package:efox_flutter/lang/config.dart' show ConfigLanguage;
6-
import 'package:efox_flutter/store/index.dart'
7-
show Store, ConfigModel; //引用Store 层
6+
import 'package:efox_flutter/store/index.dart'; //引用Store 层
87
import 'package:efox_flutter/router/index.dart' show FluroRouter; //路由
98
import 'package:efox_flutter/config/theme.dart' show AppTheme; //主题
109
import 'package:efox_flutter/utils/analytics.dart' as Analytics; //统计
@@ -26,40 +25,39 @@ class MainAppState extends State<MainApp> {
2625
//实例化多语言
2726
super.initState();
2827
_delegate = AppLocalizationsDelegate();
29-
Store.setStoreCtx(context); // 初始化数据层
28+
29+
Future.delayed(Duration.zero, () async {
30+
Store.value<ConfigModel>(context).getTheme();
31+
});
3032
}
3133

3234
@override
3335
Widget build(BuildContext context) {
34-
Store.value<ConfigModel>(context).getTheme();
35-
36-
return Store.connect<ConfigModel>(
37-
builder: (context, child, model) {
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;
46-
},
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(model.theme),
59-
onGenerateRoute: FluroRouter.router.generator,
60-
navigatorObservers: <NavigatorObserver>[Analytics.observer],
61-
);
36+
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;
46+
},
47+
onGenerateTitle: (ctx) {
48+
// 设置多语言代理
49+
AppLocalizations.setProxy(setState, _delegate);
50+
return 'flutter';
6251
},
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],
6361
);
6462
}
6563
}

‎lib/page/comment/details.dart‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import 'package:flutter/material.dart';
22
import 'package:efox_flutter/lang/index.dart' show AppLocalizations;
3-
import 'package:efox_flutter/store/index.dart'show Store, UserModel;
3+
import 'package:efox_flutter/store/index.dart';
44
import 'package:efox_flutter/store/objects/flutter_ui_issues.dart' show IssuesContent;
55
import 'package:efox_flutter/store/objects/issues_comment.dart' show IssuesDetails;
66
import 'package:efox_flutter/page/app_login/index.dart' as LoginIndex;
77

88
class Index extends StatefulWidget {
9-
int indexes;
9+
finalint indexes;
1010
Index({ Key key, @required this.indexes }):super(key: key);
1111
@override
1212
_IndexState createState() => _IndexState();
@@ -56,8 +56,8 @@ class _IndexState extends State<Index> {
5656
}
5757

5858
Widget _IssueContent (BuildContext context) {
59-
return Store.connect<UserModel>(
60-
builder: (context, child, model) {
59+
return Consumer<UserModel>(
60+
builder: (context, model,child) {
6161
IssuesContent issuesContent = model.flutter_ui_issues.issuesContent[widget.indexes];
6262
return Column(
6363
crossAxisAlignment: CrossAxisAlignment.start,
@@ -98,6 +98,7 @@ class _IndexState extends State<Index> {
9898
Widget _CommentContent (BuildContext context) {
9999
return FutureBuilder(
100100
future: _getComment,
101+
// ignore: missing_return
101102
builder: (BuildContext context, AsyncSnapshot snapshot) {
102103
if (snapshot.connectionState == ConnectionState.waiting) {
103104
return Container(
@@ -108,8 +109,8 @@ class _IndexState extends State<Index> {
108109
);
109110
} else if(snapshot.connectionState == ConnectionState.done) {
110111
if (snapshot.hasData) {
111-
return Store.connect<UserModel>(
112-
builder: (context, child, model) {
112+
return Consumer<UserModel>(
113+
builder: (context, model,child) {
113114
List<Widget> items = [];
114115
for(var issuesDetails in model.issues_comment.issuesDetails) {
115116
items.add(_CommentContentItem(context, issuesDetails));
@@ -130,8 +131,8 @@ class _IndexState extends State<Index> {
130131
}
131132

132133
Future<String> _getIssueComment(BuildContext context) async {
133-
IssuesContent issuesContent = Store.valueNotCtx<UserModel>().flutter_ui_issues.issuesContent[widget.indexes];
134-
await Store.valueNotCtx<UserModel>().getIssueComment(issuesContent.number);
134+
IssuesContent issuesContent = Store.value<UserModel>(context).flutter_ui_issues.issuesContent[widget.indexes];
135+
await Store.value<UserModel>(context).getIssueComment(issuesContent.number);
135136
return 'end';
136137
}
137138

@@ -174,8 +175,8 @@ class _IndexState extends State<Index> {
174175
flex: 1,
175176
child: _InputBox(),
176177
),
177-
Store.connect<UserModel>(
178-
builder: (context, child, model) {
178+
Consumer<UserModel>(
179+
builder: (context, model,child) {
179180
IssuesContent issuesContent = model.flutter_ui_issues.issuesContent[widget.indexes];
180181
return GestureDetector(
181182
onTap: () async {

‎lib/page/comment/index.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:efox_flutter/lang/index.dart' show AppLocalizations;
3-
import 'package:efox_flutter/store/index.dart'show Store, UserModel;
3+
import 'package:efox_flutter/store/index.dart';
44
import 'package:efox_flutter/store/objects/flutter_ui_issues.dart'
55
show IssuesContent;
66
import 'package:efox_flutter/router/index.dart' show FluroRouter;
@@ -26,7 +26,7 @@ class _IndexState extends State<Index> {
2626
}
2727

2828
Widget _CommentList(BuildContext context) {
29-
return Store.connect<UserModel>(builder: (context, child, model) {
29+
return Consumer<UserModel>(builder: (context, model,child) {
3030
if (model.flutter_ui_issues != null &&
3131
model.flutter_ui_issues.issuesContent != null &&
3232
model.flutter_ui_issues.issuesContent.length != 0) {

‎lib/page/component/tabs.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class _IndexState extends State<Index>
3838

3939
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
4040

41+
// ignore: must_call_super
4142
Widget build(BuildContext context) {
4243
return Scaffold(
4344
key: _scaffoldKey,

‎lib/page/home.dart‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'app_login/index.dart' as LoginIndex;
88
import 'comment/index.dart' as CommentIndex;
99
import 'library/index.dart' as LibraryIndex;
1010

11-
import 'package:efox_flutter/store/index.dart'show Store, UserModel;
11+
import 'package:efox_flutter/store/index.dart';
1212

1313
class Index extends StatefulWidget {
1414
@override
@@ -46,22 +46,22 @@ class _IndexState extends State<Index> {
4646
BottomNavigationBarItem(
4747
title: Text(AppLocalizations.$t('title_component')),
4848
icon: Icon(Icons.dashboard)),
49-
BottomNavigationBarItem(
49+
/* BottomNavigationBarItem(
5050
title: Text(AppLocalizations.$t('title_comment')),
5151
icon: Icon(Icons.comment)),
5252
BottomNavigationBarItem(
5353
title: Text(AppLocalizations.$t('title_library')),
54-
icon: Icon(Icons.library_add)),
54+
icon: Icon(Icons.library_add)),*/
5555
BottomNavigationBarItem(
5656
title: Text(AppLocalizations.$t('title_my')),
5757
icon: Icon(Icons.person_outline)),
5858
],
5959
// type: BottomNavigationBarType.fixed,
6060
currentIndex: _currentIndex,
6161
onTap: (int index) {
62-
if (index == 1 && _currentIndex != index) {
62+
/* if (index == 1 && _currentIndex != index) {
6363
Store.value<UserModel>(context).getIssueFlutterUI();
64-
}
64+
}*/
6565
_pageController.jumpToPage(index);
6666
},
6767
),
@@ -104,7 +104,7 @@ class _IndexState extends State<Index> {
104104
renderDrawer() {
105105
print('renderDrawer $context');
106106
return Drawer(
107-
child: Store.connect<UserModel>(builder: (context, child, model) {
107+
child: Consumer<UserModel>(builder: (context, model,child) {
108108
return Column(
109109
crossAxisAlignment: CrossAxisAlignment.start,
110110
children: <Widget>[
@@ -145,7 +145,7 @@ class _IndexState extends State<Index> {
145145
}
146146

147147
Widget _floatingActionButton(context) {
148-
return Store.connect<UserModel>(builder: (context, child, model) {
148+
return Consumer<UserModel>(builder: (context, model,child) {
149149
return FloatingActionButton(
150150
backgroundColor: Theme.of(context).primaryColor,
151151
onPressed: () {
@@ -184,12 +184,12 @@ class _IndexState extends State<Index> {
184184

185185
@override
186186
Widget build(BuildContext context) {
187-
Store.setWidgetCtx(context); // 初始化scaffold的上下文作为全局上下文,提供弹窗等使用。
187+
// Store.setWidgetCtx(context); // 初始化scaffold的上下文作为全局上下文,提供弹窗等使用。
188188
return Scaffold(
189189
drawer: renderDrawer(),
190190
bottomNavigationBar: _bottomNavigationBar(),
191-
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
192-
floatingActionButton: _floatingActionButton(context),
191+
// floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
192+
// floatingActionButton: _floatingActionButton(context),
193193
body: PageView(
194194
controller: _pageController,
195195
physics: NeverScrollableScrollPhysics(),
@@ -200,8 +200,8 @@ class _IndexState extends State<Index> {
200200
},
201201
children: <Widget>[
202202
TabIndex.Index(),
203-
CommentIndex.Index(),
204-
LibraryIndex.Index(),
203+
// CommentIndex.Index(),
204+
//LibraryIndex.Index(),
205205
MyIndex.Index(),
206206
],
207207
),

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /