Skip to content

Commit 51b4c7d

Browse files
committed
add scope_model
1 parent e22fc8a commit 51b4c7d

File tree

6 files changed

+142
-109
lines changed

6 files changed

+142
-109
lines changed

lib/page/RepositoryDetailPage.dart

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:gsy_github_app_flutter/widget/GSYIConText.dart';
1212
import 'package:gsy_github_app_flutter/widget/GSYTabBarWidget.dart';
1313
import 'package:gsy_github_app_flutter/widget/GSYTitleBar.dart';
1414
import 'package:gsy_github_app_flutter/widget/ReposHeaderItem.dart';
15+
import 'package:scoped_model/scoped_model.dart';
1516

1617
/**
1718
* 仓库详情
@@ -40,7 +41,7 @@ class _RepositoryDetailPageState extends State<RepositoryDetailPage> {
4041

4142
final TarWidgetControl tarBarControl = new TarWidgetControl();
4243

43-
final ReposDetailParentControl reposDetailParentControl = new ReposDetailParentControl("master");
44+
final ReposDetailModel reposDetailModel = new ReposDetailModel();
4445

4546
final PageController topPageControl = new PageController();
4647

@@ -82,7 +83,6 @@ class _RepositoryDetailPageState extends State<RepositoryDetailPage> {
8283
this._getReposStatus();
8384
}
8485

85-
8686
_renderBottomItem(var text, var icon, var onPressed) {
8787
return new FlatButton(
8888
onPressed: onPressed,
@@ -139,7 +139,7 @@ class _RepositoryDetailPageState extends State<RepositoryDetailPage> {
139139
return new FlatButton(
140140
padding: EdgeInsets.all(0.0),
141141
onPressed: () {
142-
reposDetailParentControl.currentIndex = i;
142+
reposDetailModel.setCurrentIndex(i);
143143
topPageControl.jumpTo(MediaQuery.of(context).size.width * i);
144144
},
145145
child: new Text(
@@ -166,32 +166,32 @@ class _RepositoryDetailPageState extends State<RepositoryDetailPage> {
166166
releaseUrl = GSYConstant.app_default_share_url;
167167
tagUrl = GSYConstant.app_default_share_url;
168168
} else {
169-
releaseUrl = infoListKey.currentState.repository == null
170-
? GSYConstant.app_default_share_url
171-
: infoListKey.currentState.repository.htmlUrl + "/releases";
172-
tagUrl =
173-
infoListKey.currentState.repository == null ? GSYConstant.app_default_share_url : infoListKey.currentState.repository.htmlUrl + "/tags";
169+
releaseUrl =
170+
infoListKey.currentState.repository == null ? GSYConstant.app_default_share_url : infoListKey.currentState.repository.htmlUrl + "/releases";
171+
tagUrl = infoListKey.currentState.repository == null ? GSYConstant.app_default_share_url : infoListKey.currentState.repository.htmlUrl + "/tags";
174172
}
175173
NavigatorUtils.goReleasePage(context, userName, reposName, releaseUrl, tagUrl);
176-
}), ///Branch Page
174+
}),
175+
176+
///Branch Page
177177
new GSYOptionModel(CommonUtils.getLocale(context).repos_option_branch, CommonUtils.getLocale(context).repos_option_branch, (model) {
178-
if(branchList.length == 0) {
179-
return;
180-
}
181-
CommonUtils.showCommitOptionDialog(context, branchList, (value){
182-
setState(() {
183-
reposDetailParentControl.currentBranch = branchList[value];
184-
});
185-
if (infoListKey.currentState != null && infoListKey.currentState.mounted) {
186-
infoListKey.currentState.showRefreshLoading();
187-
}
188-
if (fileListKey.currentState != null && fileListKey.currentState.mounted) {
189-
fileListKey.currentState.showRefreshLoading();
190-
}
191-
if (readmeKey.currentState != null && readmeKey.currentState.mounted) {
192-
readmeKey.currentState.refreshReadme();
193-
}
178+
if (branchList.length == 0) {
179+
return;
180+
}
181+
CommonUtils.showCommitOptionDialog(context, branchList, (value) {
182+
setState(() {
183+
reposDetailModel.setCurrentBranch(branchList[value]);
194184
});
185+
if (infoListKey.currentState != null && infoListKey.currentState.mounted) {
186+
infoListKey.currentState.showRefreshLoading();
187+
}
188+
if (fileListKey.currentState != null && fileListKey.currentState.mounted) {
189+
fileListKey.currentState.showRefreshLoading();
190+
}
191+
if (readmeKey.currentState != null && readmeKey.currentState.mounted) {
192+
readmeKey.currentState.refreshReadme();
193+
}
194+
});
195195
}),
196196
];
197197
}
@@ -206,26 +206,33 @@ class _RepositoryDetailPageState extends State<RepositoryDetailPage> {
206206
@override
207207
Widget build(BuildContext context) {
208208
Widget widget = new GSYCommonOptionWidget(titleOptionControl, otherList: _getMoreOtherItem());
209-
return new GSYTabBarWidget(
210-
type: GSYTabBarWidget.TOP_TAB,
211-
tarWidgetControl: tarBarControl,
212-
tabItems: _renderTabItem(),
213-
tabViews: [
214-
new ReposDetailInfoPage(userName, reposName, reposDetailParentControl, titleOptionControl, key: infoListKey),
215-
new RepositoryDetailReadmePage(userName, reposName, reposDetailParentControl, key: readmeKey),
216-
new RepositoryDetailIssuePage(userName, reposName),
217-
new RepositoryDetailFileListPage(userName, reposName, reposDetailParentControl, key: fileListKey),
218-
],
219-
topPageControl: topPageControl,
220-
backgroundColor: GSYColors.primarySwatch,
221-
indicatorColor: Color(GSYColors.white),
222-
title: new GSYTitleBar(
223-
reposName,
224-
rightWidget: widget,
209+
return new ScopedModel<ReposDetailModel>(
210+
model: reposDetailModel,
211+
child: new ScopedModelDescendant<ReposDetailModel>(
212+
builder: (context, child, model) {
213+
return new GSYTabBarWidget(
214+
type: GSYTabBarWidget.TOP_TAB,
215+
tarWidgetControl: tarBarControl,
216+
tabItems: _renderTabItem(),
217+
tabViews: [
218+
new ReposDetailInfoPage(userName, reposName, titleOptionControl, key: infoListKey),
219+
new RepositoryDetailReadmePage(userName, reposName, key: readmeKey),
220+
new RepositoryDetailIssuePage(userName, reposName),
221+
new RepositoryDetailFileListPage(userName, reposName, key: fileListKey),
222+
],
223+
topPageControl: topPageControl,
224+
backgroundColor: GSYColors.primarySwatch,
225+
indicatorColor: Color(GSYColors.white),
226+
title: new GSYTitleBar(
227+
reposName,
228+
rightWidget: widget,
229+
),
230+
onPageChanged: (index) {
231+
reposDetailModel.setCurrentIndex(index);
232+
},
233+
);
234+
},
225235
),
226-
onPageChanged: (index) {
227-
reposDetailParentControl.currentIndex = index;
228-
},
229236
);
230237
}
231238
}
@@ -241,10 +248,26 @@ class BottomStatusModel {
241248
BottomStatusModel(this.watchText, this.starText, this.watchIcon, this.starIcon, this.watch, this.star);
242249
}
243250

244-
class ReposDetailParentControl {
245-
int currentIndex = 0;
251+
class ReposDetailModel extends Model {
252+
int _currentIndex = 0;
253+
254+
String _currentBranch = "master";
255+
256+
String get currentBranch => _currentBranch;
257+
258+
int get currentIndex => _currentIndex;
246259

247-
String currentBranch;
260+
static ReposDetailModel of(BuildContext context) => ScopedModel.of<ReposDetailModel>(context);
248261

249-
ReposDetailParentControl(this.currentBranch);
262+
void setCurrentBranch(String branch) {
263+
_currentBranch = branch;
264+
notifyListeners();
265+
}
266+
267+
void setCurrentIndex(int index) {
268+
_currentIndex = index;
269+
print("----");
270+
print(_currentIndex);
271+
notifyListeners();
272+
}
250273
}

lib/page/RepositoryDetailReadmePage.dart

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
77
import 'package:gsy_github_app_flutter/common/utils/CommonUtils.dart';
88
import 'package:gsy_github_app_flutter/page/RepositoryDetailPage.dart';
99
import 'package:gsy_github_app_flutter/widget/GSYMarkdownWidget.dart';
10+
import 'package:scoped_model/scoped_model.dart';
1011

1112
/**
1213
* Readme
@@ -19,12 +20,10 @@ class RepositoryDetailReadmePage extends StatefulWidget {
1920

2021
final String reposName;
2122

22-
final ReposDetailParentControl reposDetailParentControl;
23-
24-
RepositoryDetailReadmePage(this.userName, this.reposName, this.reposDetailParentControl, {Key key}) : super(key: key);
23+
RepositoryDetailReadmePage(this.userName, this.reposName, {Key key}) : super(key: key);
2524

2625
@override
27-
RepositoryDetailReadmePageState createState() => RepositoryDetailReadmePageState(userName, reposName, reposDetailParentControl);
26+
RepositoryDetailReadmePageState createState() => RepositoryDetailReadmePageState(userName, reposName);
2827
}
2928

3029
// ignore: mixin_inherits_from_not_object
@@ -33,16 +32,14 @@ class RepositoryDetailReadmePageState extends State<RepositoryDetailReadmePage>
3332

3433
final String reposName;
3534

36-
final ReposDetailParentControl reposDetailParentControl;
37-
3835
bool isShow = false;
3936

4037
String markdownData;
4138

42-
RepositoryDetailReadmePageState(this.userName, this.reposName, this.reposDetailParentControl);
39+
RepositoryDetailReadmePageState(this.userName, this.reposName);
4340

4441
refreshReadme() {
45-
ReposDao.getRepositoryDetailReadmeDao(userName, reposName, reposDetailParentControl.currentBranch).then((res) {
42+
ReposDao.getRepositoryDetailReadmeDao(userName, reposName, ReposDetailModel.of(context).currentBranch).then((res) {
4643
if (res != null && res.result) {
4744
if (isShow) {
4845
setState(() {
@@ -77,29 +74,31 @@ class RepositoryDetailReadmePageState extends State<RepositoryDetailReadmePage>
7774
void dispose() {
7875
isShow = false;
7976
super.dispose();
80-
8177
}
8278

8379
@override
8480
Widget build(BuildContext context) {
8581
super.build(context);
86-
if (markdownData == null) {
87-
return Center(
88-
child: new Container(
89-
width: 200.0,
90-
height: 200.0,
91-
padding: new EdgeInsets.all(4.0),
92-
child: new Row(
93-
mainAxisAlignment: MainAxisAlignment.center,
94-
children: <Widget>[
95-
new SpinKitDoubleBounce(color: Theme.of(context).primaryColor),
96-
new Container(width: 10.0),
97-
new Container(child: new Text(CommonUtils.getLocale(context).loading_text, style: GSYConstant.middleText)),
98-
],
99-
),
100-
),
101-
);
102-
}
103-
return GSYMarkdownWidget(markdownData: markdownData);
82+
var widget = (markdownData == null)
83+
? Center(
84+
child: new Container(
85+
width: 200.0,
86+
height: 200.0,
87+
padding: new EdgeInsets.all(4.0),
88+
child: new Row(
89+
mainAxisAlignment: MainAxisAlignment.center,
90+
children: <Widget>[
91+
new SpinKitDoubleBounce(color: Theme.of(context).primaryColor),
92+
new Container(width: 10.0),
93+
new Container(child: new Text(CommonUtils.getLocale(context).loading_text, style: GSYConstant.middleText)),
94+
],
95+
),
96+
),
97+
)
98+
: GSYMarkdownWidget(markdownData: markdownData);
99+
100+
return new ScopedModelDescendant<ReposDetailModel>(
101+
builder: (context, child, model) => widget,
102+
);
104103
}
105104
}

lib/page/RepositoryFileListPage.dart

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:gsy_github_app_flutter/page/RepositoryDetailPage.dart';
1111
import 'package:gsy_github_app_flutter/widget/GSYCardItem.dart';
1212
import 'package:gsy_github_app_flutter/widget/GSYListState.dart';
1313
import 'package:gsy_github_app_flutter/widget/GSYPullLoadWidget.dart';
14+
import 'package:scoped_model/scoped_model.dart';
1415

1516
/**
1617
* 仓库文件列表
@@ -23,12 +24,10 @@ class RepositoryDetailFileListPage extends StatefulWidget {
2324

2425
final String reposName;
2526

26-
final ReposDetailParentControl reposDetailParentControl;
27-
28-
RepositoryDetailFileListPage(this.userName, this.reposName, this.reposDetailParentControl, {Key key}) : super(key: key);
27+
RepositoryDetailFileListPage(this.userName, this.reposName, {Key key}) : super(key: key);
2928

3029
@override
31-
RepositoryDetailFileListPageState createState() => RepositoryDetailFileListPageState(userName, reposName, reposDetailParentControl);
30+
RepositoryDetailFileListPageState createState() => RepositoryDetailFileListPageState(userName, reposName);
3231
}
3332

3433
// ignore: mixin_inherits_from_not_object
@@ -38,16 +37,14 @@ class RepositoryDetailFileListPageState extends State<RepositoryDetailFileListPa
3837

3938
final String reposName;
4039

41-
final ReposDetailParentControl reposDetailParentControl;
42-
4340
String path = '';
4441

4542
String searchText;
4643
String issueState;
4744

4845
List<String> headerList = ["."];
4946

50-
RepositoryDetailFileListPageState(this.userName, this.reposName, this.reposDetailParentControl);
47+
RepositoryDetailFileListPageState(this.userName, this.reposName);
5148

5249
///渲染文件item
5350
_renderEventItem(index) {
@@ -140,19 +137,19 @@ class RepositoryDetailFileListPageState extends State<RepositoryDetailFileListPa
140137
reposName: reposName,
141138
userName: userName,
142139
path: path,
143-
branch: reposDetailParentControl.currentBranch,
140+
branch: ReposDetailModel.of(context).currentBranch,
144141
);
145142
}
146143
}
147144
}
148145

149146
_getDataLogic(String searchString) async {
150-
return await ReposDao.getReposFileDirDao(userName, reposName, path: path, branch: reposDetailParentControl.currentBranch);
147+
return await ReposDao.getReposFileDirDao(userName, reposName, path: path, branch: ReposDetailModel.of(context).currentBranch);
151148
}
152149

153150
/// 返回按键逻辑
154151
Future<bool> _dialogExitApp(BuildContext context) {
155-
if (reposDetailParentControl.currentIndex != 3) {
152+
if (ReposDetailModel.of(context).currentIndex != 3) {
156153
return Future.value(true);
157154
}
158155
if (headerList.length == 1) {
@@ -197,12 +194,16 @@ class RepositoryDetailFileListPageState extends State<RepositoryDetailFileListPa
197194
onWillPop: () {
198195
return _dialogExitApp(context);
199196
},
200-
child: GSYPullLoadWidget(
201-
pullLoadWidgetControl,
202-
(BuildContext context, int index) => _renderEventItem(index),
203-
handleRefresh,
204-
onLoadMore,
205-
refreshKey: refreshIndicatorKey,
197+
child: ScopedModelDescendant<ReposDetailModel>(
198+
builder: (context, child, model) {
199+
return GSYPullLoadWidget(
200+
pullLoadWidgetControl,
201+
(BuildContext context, int index) => _renderEventItem(index),
202+
handleRefresh,
203+
onLoadMore,
204+
refreshKey: refreshIndicatorKey,
205+
);
206+
},
206207
),
207208
),
208209
);

0 commit comments

Comments
 (0)