Skip to content

Commit

Permalink
feat:重构数据层 增加 provider demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ckken committed Aug 29, 2019
1 parent f3243d0 commit 3bf97d3
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 149 deletions.
8 changes: 4 additions & 4 deletions lib/components/widget_comp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class IndexState extends State<Index> {
this._bodyList.length = 0;
String mdText = await this.getMdFile(widget.mdUrl);
String nameKey = AuthorList.list[widget.title];
if (nameKey != null) {
/*if (nameKey != null) {
this._bodyList.add(authorTile(nameKey));
this._bodyList.add(Divider());
}
}*/
print('文档完成长度:${mdText.length}');
if (mdText.length > 30) {
this._bodyList.add(await markdown_comp.Index(mdText));
Expand Down Expand Up @@ -152,7 +152,7 @@ class IndexState extends State<Index> {

getActions(context) {
return [
IconButton(
/*IconButton(
color: Color(AppTheme.blackColor),
icon: Icon(
Icons.code
Expand All @@ -172,7 +172,7 @@ class IndexState extends State<Index> {
'/webview?title=${widget.title}&url=${Uri.encodeComponent(widget.originCodeUrl)}',
);
},
),
),*/
IconButton(
icon: Icon(Icons.share),
color: Color(AppTheme.blackColor),
Expand Down
12 changes: 6 additions & 6 deletions lib/controller/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import 'package:efox_flutter/utils/appVersion.dart' show AppVersion;

void initState() async {
// 获取版本号
Store.valueNotCtx<ConfigModel>().getAppVersion();
Store.value<ConfigModel>().getAppVersion();
// 登录
Store.valueNotCtx<UserModel>().getLocalUserInfo().then((res) {
/*Store.value<UserModel>().getLocalUserInfo().then((res) {
if (res) {
Store.valueNotCtx<UserModel>().getUserStar();
Store.value<UserModel>().getUserStar();
}
});
Store.valueNotCtx<UserModel>().getFlutterUIStar();
});*/
// Store.value<UserModel>().getFlutterUIStar();
Future.delayed(Duration(seconds: 3), () {
AppVersion().check(Store.widgetCtx);
AppVersion().check(Store.context);
});
}
4 changes: 2 additions & 2 deletions lib/http/loading.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void beforeRequest(uri, Map<dynamic, dynamic> options) {
void afterResponse(uri, Map<dynamic, dynamic> options) {
dict.remove(uri);
if (dict.length == 0 && loading == true) {
Navigator.of(Store.widgetCtx, rootNavigator: true).pop('close dialog');
Navigator.of(Store.context, rootNavigator: true).pop('close dialog');
loading = false;
}
}
Expand All @@ -31,7 +31,7 @@ void showAppLoading(Map<dynamic, dynamic> options) {
'text': options['text'] ?? 'loading...'
};
showDialog(
context: Store.widgetCtx,
context: Store.context,
builder: (context) {
return LoadingDialog(text: options['text']);
},
Expand Down
60 changes: 29 additions & 31 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import 'package:flutter_localizations/flutter_localizations.dart'; //语言包
import 'package:efox_flutter/lang/index.dart'
show AppLocalizationsDelegate, AppLocalizations;
import 'package:efox_flutter/lang/config.dart' show ConfigLanguage;
import 'package:efox_flutter/store/index.dart'
show Store, ConfigModel; //引用Store 层
import 'package:efox_flutter/store/index.dart'; //引用Store 层
import 'package:efox_flutter/router/index.dart' show FluroRouter; //路由
import 'package:efox_flutter/config/theme.dart' show AppTheme; //主题
import 'package:efox_flutter/utils/analytics.dart' as Analytics; //统计
Expand All @@ -26,40 +25,39 @@ class MainAppState extends State<MainApp> {
//实例化多语言
super.initState();
_delegate = AppLocalizationsDelegate();
Store.setStoreCtx(context); // 初始化数据层

Future.delayed(Duration.zero, () async {
Store.value<ConfigModel>(context).getTheme();
});
}

@override
Widget build(BuildContext context) {
Store.value<ConfigModel>(context).getTheme();

return Store.connect<ConfigModel>(
builder: (context, child, model) {
return MaterialApp(
localeResolutionCallback: (deviceLocale, supportedLocales) {
print(
'deviceLocale=$deviceLocale supportedLocales=$supportedLocales');
Locale _locale = supportedLocales.contains(deviceLocale)
? deviceLocale
: Locale('zh');
return _locale;
},
onGenerateTitle: (ctx) {
// 设置多语言代理
AppLocalizations.setProxy(setState, _delegate);
return 'flutter';
},
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
_delegate,
],
supportedLocales: ConfigLanguage.supportedLocales,
theme: AppTheme.getThemeData(model.theme),
onGenerateRoute: FluroRouter.router.generator,
navigatorObservers: <NavigatorObserver>[Analytics.observer],
);
Store.of(context);
print('Store.value<ConfigModel>(context)=${Store.value<ConfigModel>(context).theme}');
return MaterialApp(
localeResolutionCallback: (deviceLocale, supportedLocales) {
print(
'deviceLocale=$deviceLocale supportedLocales=$supportedLocales');
Locale _locale = supportedLocales.contains(deviceLocale)
? deviceLocale
: Locale('zh');
return _locale;
},
onGenerateTitle: (ctx) {
// 设置多语言代理
AppLocalizations.setProxy(setState, _delegate);
return 'flutter';
},
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
_delegate,
],
supportedLocales: ConfigLanguage.supportedLocales,
theme: AppTheme.getThemeData(Store.value<ConfigModel>(context).theme),
onGenerateRoute: FluroRouter.router.generator,
navigatorObservers: <NavigatorObserver>[Analytics.observer],
);
}
}
Expand Down
21 changes: 11 additions & 10 deletions lib/page/comment/details.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'package:flutter/material.dart';
import 'package:efox_flutter/lang/index.dart' show AppLocalizations;
import 'package:efox_flutter/store/index.dart' show Store, UserModel;
import 'package:efox_flutter/store/index.dart';
import 'package:efox_flutter/store/objects/flutter_ui_issues.dart' show IssuesContent;
import 'package:efox_flutter/store/objects/issues_comment.dart' show IssuesDetails;
import 'package:efox_flutter/page/app_login/index.dart' as LoginIndex;

class Index extends StatefulWidget {
int indexes;
final int indexes;
Index({ Key key, @required this.indexes }):super(key: key);
@override
_IndexState createState() => _IndexState();
Expand Down Expand Up @@ -56,8 +56,8 @@ class _IndexState extends State<Index> {
}

Widget _IssueContent (BuildContext context) {
return Store.connect<UserModel>(
builder: (context, child, model) {
return Consumer<UserModel>(
builder: (context, model,child) {
IssuesContent issuesContent = model.flutter_ui_issues.issuesContent[widget.indexes];
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
Expand Down Expand Up @@ -98,6 +98,7 @@ class _IndexState extends State<Index> {
Widget _CommentContent (BuildContext context) {
return FutureBuilder(
future: _getComment,
// ignore: missing_return
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Container(
Expand All @@ -108,8 +109,8 @@ class _IndexState extends State<Index> {
);
} else if(snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
return Store.connect<UserModel>(
builder: (context, child, model) {
return Consumer<UserModel>(
builder: (context, model,child) {
List<Widget> items = [];
for(var issuesDetails in model.issues_comment.issuesDetails) {
items.add(_CommentContentItem(context, issuesDetails));
Expand All @@ -130,8 +131,8 @@ class _IndexState extends State<Index> {
}

Future<String> _getIssueComment(BuildContext context) async {
IssuesContent issuesContent = Store.valueNotCtx<UserModel>().flutter_ui_issues.issuesContent[widget.indexes];
await Store.valueNotCtx<UserModel>().getIssueComment(issuesContent.number);
IssuesContent issuesContent = Store.value<UserModel>(context).flutter_ui_issues.issuesContent[widget.indexes];
await Store.value<UserModel>(context).getIssueComment(issuesContent.number);
return 'end';
}

Expand Down Expand Up @@ -174,8 +175,8 @@ class _IndexState extends State<Index> {
flex: 1,
child: _InputBox(),
),
Store.connect<UserModel>(
builder: (context, child, model) {
Consumer<UserModel>(
builder: (context, model,child) {
IssuesContent issuesContent = model.flutter_ui_issues.issuesContent[widget.indexes];
return GestureDetector(
onTap: () async {
Expand Down
4 changes: 2 additions & 2 deletions lib/page/comment/index.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:efox_flutter/lang/index.dart' show AppLocalizations;
import 'package:efox_flutter/store/index.dart' show Store, UserModel;
import 'package:efox_flutter/store/index.dart';
import 'package:efox_flutter/store/objects/flutter_ui_issues.dart'
show IssuesContent;
import 'package:efox_flutter/router/index.dart' show FluroRouter;
Expand All @@ -26,7 +26,7 @@ class _IndexState extends State<Index> {
}

Widget _CommentList(BuildContext context) {
return Store.connect<UserModel>(builder: (context, child, model) {
return Consumer<UserModel>(builder: (context, model,child) {
if (model.flutter_ui_issues != null &&
model.flutter_ui_issues.issuesContent != null &&
model.flutter_ui_issues.issuesContent.length != 0) {
Expand Down
1 change: 1 addition & 0 deletions lib/page/component/tabs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class _IndexState extends State<Index>

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

// ignore: must_call_super
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
Expand Down
24 changes: 12 additions & 12 deletions lib/page/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'app_login/index.dart' as LoginIndex;
import 'comment/index.dart' as CommentIndex;
import 'library/index.dart' as LibraryIndex;

import 'package:efox_flutter/store/index.dart' show Store, UserModel;
import 'package:efox_flutter/store/index.dart';

class Index extends StatefulWidget {
@override
Expand Down Expand Up @@ -46,22 +46,22 @@ class _IndexState extends State<Index> {
BottomNavigationBarItem(
title: Text(AppLocalizations.$t('title_component')),
icon: Icon(Icons.dashboard)),
BottomNavigationBarItem(
/* BottomNavigationBarItem(
title: Text(AppLocalizations.$t('title_comment')),
icon: Icon(Icons.comment)),
BottomNavigationBarItem(
title: Text(AppLocalizations.$t('title_library')),
icon: Icon(Icons.library_add)),
icon: Icon(Icons.library_add)),*/
BottomNavigationBarItem(
title: Text(AppLocalizations.$t('title_my')),
icon: Icon(Icons.person_outline)),
],
// type: BottomNavigationBarType.fixed,
currentIndex: _currentIndex,
onTap: (int index) {
if (index == 1 && _currentIndex != index) {
/* if (index == 1 && _currentIndex != index) {
Store.value<UserModel>(context).getIssueFlutterUI();
}
}*/
_pageController.jumpToPage(index);
},
),
Expand Down Expand Up @@ -104,7 +104,7 @@ class _IndexState extends State<Index> {
renderDrawer() {
print('renderDrawer $context');
return Drawer(
child: Store.connect<UserModel>(builder: (context, child, model) {
child: Consumer<UserModel>(builder: (context, model,child) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expand Down Expand Up @@ -145,7 +145,7 @@ class _IndexState extends State<Index> {
}

Widget _floatingActionButton(context) {
return Store.connect<UserModel>(builder: (context, child, model) {
return Consumer<UserModel>(builder: (context, model,child) {
return FloatingActionButton(
backgroundColor: Theme.of(context).primaryColor,
onPressed: () {
Expand Down Expand Up @@ -184,12 +184,12 @@ class _IndexState extends State<Index> {

@override
Widget build(BuildContext context) {
Store.setWidgetCtx(context); // 初始化scaffold的上下文作为全局上下文,提供弹窗等使用。
// Store.setWidgetCtx(context); // 初始化scaffold的上下文作为全局上下文,提供弹窗等使用。
return Scaffold(
drawer: renderDrawer(),
bottomNavigationBar: _bottomNavigationBar(),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: _floatingActionButton(context),
// floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
// floatingActionButton: _floatingActionButton(context),
body: PageView(
controller: _pageController,
physics: NeverScrollableScrollPhysics(),
Expand All @@ -200,8 +200,8 @@ class _IndexState extends State<Index> {
},
children: <Widget>[
TabIndex.Index(),
CommentIndex.Index(),
LibraryIndex.Index(),
// CommentIndex.Index(),
//LibraryIndex.Index(),
MyIndex.Index(),
],
),
Expand Down
Loading

0 comments on commit 3bf97d3

Please sign in to comment.