Skip to content

Commit 0cc67b1

Browse files
committed
add refresh manager
1 parent c270b2c commit 0cc67b1

10 files changed

Lines changed: 130 additions & 52 deletions

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ flutter packages pub run build_runner build
2929

3030
### 配合AutomaticKeepAliveClientMixin可以keep住tab
3131

32+
### 如何主动刷新refresh,如何设置appbar上的leading和bottom,如何设置tabbar的live
3233

3334
https://github.com/flutter/flutter/issues/19030 没有webview,残念

‎lib/page/DynamicPage.dart‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class _DynamicPageState extends State<DynamicPage> with AutomaticKeepAliveClient
3131

3232
final GSYPullLoadWidgetControl pullLoadWidgetControl = new GSYPullLoadWidgetControl();
3333

34+
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
35+
3436
Future<Null> _handleRefresh() async {
3537
if (isLoading) {
3638
return null;
@@ -84,7 +86,9 @@ class _DynamicPageState extends State<DynamicPage> with AutomaticKeepAliveClient
8486
void didChangeDependencies() {
8587
pullLoadWidgetControl.dataList = _getStore().state.eventList;
8688
if (pullLoadWidgetControl.dataList.length == 0) {
87-
_handleRefresh();
89+
new Future.delayed(const Duration(seconds: 0), () {
90+
_refreshIndicatorKey.currentState.show().then((e) {});
91+
});
8892
}
8993
super.didChangeDependencies();
9094
}
@@ -94,8 +98,13 @@ class _DynamicPageState extends State<DynamicPage> with AutomaticKeepAliveClient
9498
super.build(context); // See AutomaticKeepAliveClientMixin.
9599
return new StoreBuilder<GSYState>(
96100
builder: (context, store) {
97-
return GSYPullLoadWidget(pullLoadWidgetControl, (BuildContext context, int index) => _renderEventItem(pullLoadWidgetControl.dataList[index]),
98-
_handleRefresh, _onLoadMore);
101+
return GSYPullLoadWidget(
102+
pullLoadWidgetControl,
103+
(BuildContext context, int index) => _renderEventItem(pullLoadWidgetControl.dataList[index]),
104+
_handleRefresh,
105+
_onLoadMore,
106+
refreshKey: _refreshIndicatorKey,
107+
);
99108
},
100109
);
101110
}

‎lib/page/MyPage.dart‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:async';
2-
import 'dart:math';
32

43
import 'package:flutter/material.dart';
54
import 'package:flutter_redux/flutter_redux.dart';
@@ -32,6 +31,8 @@ class _MyPageState extends State<MyPage> with AutomaticKeepAliveClientMixin {
3231

3332
final GSYPullLoadWidgetControl pullLoadWidgetControl = new GSYPullLoadWidgetControl();
3433

34+
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
35+
3536
Future<Null> _handleRefresh() async {
3637
if (isLoading) {
3738
return null;
@@ -105,7 +106,9 @@ class _MyPageState extends State<MyPage> with AutomaticKeepAliveClientMixin {
105106
void didChangeDependencies() {
106107
pullLoadWidgetControl.dataList = dataList;
107108
if (pullLoadWidgetControl.dataList.length == 0) {
108-
_handleRefresh();
109+
new Future.delayed(const Duration(seconds: 0), () {
110+
_refreshIndicatorKey.currentState.show().then((e) {});
111+
});
109112
}
110113
super.didChangeDependencies();
111114
}
@@ -116,7 +119,12 @@ class _MyPageState extends State<MyPage> with AutomaticKeepAliveClientMixin {
116119
return new StoreBuilder<GSYState>(
117120
builder: (context, store) {
118121
return GSYPullLoadWidget(
119-
pullLoadWidgetControl, (BuildContext context, int index) => _renderEventItem(store.state.userInfo, index), _handleRefresh, _onLoadMore);
122+
pullLoadWidgetControl,
123+
(BuildContext context, int index) => _renderEventItem(store.state.userInfo, index),
124+
_handleRefresh,
125+
_onLoadMore,
126+
refreshKey: _refreshIndicatorKey,
127+
);
120128
},
121129
);
122130
}

‎lib/page/PersonPage.dart‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class _PersonState extends State<PersonPage> with AutomaticKeepAliveClientMixin
4141

4242
final GSYPullLoadWidgetControl pullLoadWidgetControl = new GSYPullLoadWidgetControl();
4343

44+
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
45+
4446
Future<Null> _handleRefresh() async {
4547
if (isLoading) {
4648
return null;
@@ -117,7 +119,9 @@ class _PersonState extends State<PersonPage> with AutomaticKeepAliveClientMixin
117119
void didChangeDependencies() {
118120
pullLoadWidgetControl.dataList = dataList;
119121
if (pullLoadWidgetControl.dataList.length == 0) {
120-
_handleRefresh();
122+
new Future.delayed(const Duration(seconds: 0), () {
123+
_refreshIndicatorKey.currentState.show().then((e) {});
124+
});
121125
}
122126
super.didChangeDependencies();
123127
}
@@ -128,6 +132,12 @@ class _PersonState extends State<PersonPage> with AutomaticKeepAliveClientMixin
128132
appBar: new AppBar(
129133
title: new Text((userInfo != null && userInfo.login != null) ? userInfo.login : ""),
130134
),
131-
body: GSYPullLoadWidget(pullLoadWidgetControl, (BuildContext context, int index) => _renderEventItem(index), _handleRefresh, _onLoadMore));
135+
body: GSYPullLoadWidget(
136+
pullLoadWidgetControl,
137+
(BuildContext context, int index) => _renderEventItem(index),
138+
_handleRefresh,
139+
_onLoadMore,
140+
refreshKey: _refreshIndicatorKey,
141+
));
132142
}
133143
}

‎lib/page/RepositoryDetailIssueList.dart‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
44
import 'package:gsy_github_app_flutter/common/config/Config.dart';
55
import 'package:gsy_github_app_flutter/common/dao/IssueDao.dart';
66
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
7-
import 'package:gsy_github_app_flutter/widget/GSYCardItem.dart';
87
import 'package:gsy_github_app_flutter/widget/GSYPullLoadWidget.dart';
98
import 'package:gsy_github_app_flutter/widget/IssueItem.dart';
109
import 'package:gsy_github_app_flutter/widget/RepositoryIssueListHeader.dart';
@@ -27,6 +26,8 @@ class RepositoryDetailIssuePage extends StatefulWidget {
2726

2827
// ignore: mixin_inherits_from_not_object
2928
class _RepositoryDetailIssuePageState extends State<RepositoryDetailIssuePage> with AutomaticKeepAliveClientMixin {
29+
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
30+
3031
final String userName;
3132

3233
final String reposName;
@@ -99,7 +100,9 @@ class _RepositoryDetailIssuePageState extends State<RepositoryDetailIssuePage> w
99100
pullLoadWidgetControl.needHeader = false;
100101
pullLoadWidgetControl.dataList = dataList;
101102
if (pullLoadWidgetControl.dataList.length == 0) {
102-
_handleRefresh();
103+
new Future.delayed(const Duration(seconds: 0), () {
104+
_refreshIndicatorKey.currentState.show().then((e) {});
105+
});
103106
}
104107
}
105108

@@ -116,10 +119,10 @@ class _RepositoryDetailIssuePageState extends State<RepositoryDetailIssuePage> w
116119
appBar: new AppBar(
117120
leading: new Container(),
118121
flexibleSpace: new Container(
119-
padding: new EdgeInsets.only(left: 20.0, top: 12.0, right: 20.0, bottom:12.0),
122+
padding: new EdgeInsets.only(left: 20.0, top: 12.0, right: 20.0, bottom: 12.0),
120123
color: Colors.white,
121124
child: new TextField(
122-
autofocus: true,
125+
autofocus: false,
123126
decoration: new InputDecoration.collapsed(
124127
hintText: GSYStrings.repos_issue_search,
125128
hintStyle: GSYConstant.subSmallText,
@@ -129,9 +132,15 @@ class _RepositoryDetailIssuePageState extends State<RepositoryDetailIssuePage> w
129132
),
130133
elevation: 0.0,
131134
backgroundColor: Color(GSYColors.mainBackgroundColor),
132-
bottom: new RepositoryIssueListHeader(),
135+
bottom: new RepositoryIssueListHeader((selectIndex) {}),
136+
),
137+
body: GSYPullLoadWidget(
138+
pullLoadWidgetControl,
139+
(BuildContext context, int index) => _renderEventItem(index),
140+
_handleRefresh,
141+
_onLoadMore,
142+
refreshKey: _refreshIndicatorKey,
133143
),
134-
body: GSYPullLoadWidget(pullLoadWidgetControl, (BuildContext context, int index) => _renderEventItem(index), _handleRefresh, _onLoadMore),
135144
);
136145
}
137146
}

‎lib/page/RepostoryDetailInfoPage.dart‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class _ReposDetailInfoPageState extends State<ReposDetailInfoPage> with Automati
4141

4242
final GSYPullLoadWidgetControl pullLoadWidgetControl = new GSYPullLoadWidgetControl();
4343

44+
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
45+
4446
_ReposDetailInfoPageState(this.reposDetailInfoPageControl, this.userName, this.reposName);
4547

4648
Future<Null> _handleRefresh() async {
@@ -87,7 +89,7 @@ class _ReposDetailInfoPageState extends State<ReposDetailInfoPage> with Automati
8789
return new ReposHeaderItem(reposDetailInfoPageControl.reposHeaderViewModel);
8890
}
8991

90-
EventViewModel eventViewModel = pullLoadWidgetControl.dataList[index - 1];
92+
EventViewModel eventViewModel = pullLoadWidgetControl.dataList[index - 1];
9193
return new EventItem(
9294
pullLoadWidgetControl.dataList[index - 1],
9395
onPressed: () {
@@ -105,7 +107,9 @@ class _ReposDetailInfoPageState extends State<ReposDetailInfoPage> with Automati
105107
pullLoadWidgetControl.needHeader = true;
106108
pullLoadWidgetControl.dataList = dataList;
107109
if (pullLoadWidgetControl.dataList.length == 0) {
108-
_handleRefresh();
110+
new Future.delayed(const Duration(seconds: 0), () {
111+
_refreshIndicatorKey.currentState.show().then((e) {});
112+
});
109113
}
110114
}
111115

@@ -117,7 +121,13 @@ class _ReposDetailInfoPageState extends State<ReposDetailInfoPage> with Automati
117121
@override
118122
Widget build(BuildContext context) {
119123
super.build(context); // See AutomaticKeepAliveClientMixin.
120-
return GSYPullLoadWidget(pullLoadWidgetControl, (BuildContext context, int index) => _renderEventItem(index), _handleRefresh, _onLoadMore);
124+
return GSYPullLoadWidget(
125+
pullLoadWidgetControl,
126+
(BuildContext context, int index) => _renderEventItem(index),
127+
_handleRefresh,
128+
_onLoadMore,
129+
refreshKey: _refreshIndicatorKey,
130+
);
121131
}
122132
}
123133

‎lib/page/TrendPage.dart‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixi
2626

2727
final GSYPullLoadWidgetControl pullLoadWidgetControl = new GSYPullLoadWidgetControl();
2828

29+
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
30+
2931
Future<Null> _handleRefresh() async {
3032
if (isLoading) {
3133
return null;
@@ -66,7 +68,9 @@ class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixi
6668
@override
6769
void didChangeDependencies() {
6870
if (pullLoadWidgetControl.dataList.length == 0) {
69-
_handleRefresh();
71+
new Future.delayed(const Duration(seconds: 0), () {
72+
_refreshIndicatorKey.currentState.show().then((e) {});
73+
});
7074
}
7175
super.didChangeDependencies();
7276
}
@@ -75,6 +79,11 @@ class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixi
7579
Widget build(BuildContext context) {
7680
super.build(context); // See AutomaticKeepAliveClientMixin.
7781
return GSYPullLoadWidget(
78-
pullLoadWidgetControl, (BuildContext context, int index) => _renderItem(pullLoadWidgetControl.dataList[index]), _handleRefresh, _onLoadMore);
82+
pullLoadWidgetControl,
83+
(BuildContext context, int index) => _renderItem(pullLoadWidgetControl.dataList[index]),
84+
_handleRefresh,
85+
_onLoadMore,
86+
refreshKey: _refreshIndicatorKey,
87+
);
7988
}
8089
}

‎lib/widget/GSYPullLoadWidget.dart‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
99

1010
///通用下上刷新控件
1111
class GSYPullLoadWidget extends StatefulWidget {
12-
1312
///item渲染
1413
final IndexedWidgetBuilder itemBuilder;
1514

@@ -22,11 +21,12 @@ class GSYPullLoadWidget extends StatefulWidget {
2221
///控制器,比如数据和一些配置
2322
final GSYPullLoadWidgetControl control;
2423

24+
final Key refreshKey;
2525

26-
GSYPullLoadWidget(this.control, this.itemBuilder, this.onRefresh, this.onLoadMore);
26+
GSYPullLoadWidget(this.control, this.itemBuilder, this.onRefresh, this.onLoadMore, {this.refreshKey});
2727

2828
@override
29-
_GSYPullLoadWidgetState createState() => _GSYPullLoadWidgetState(this.control, this.itemBuilder, this.onRefresh, this.onLoadMore);
29+
_GSYPullLoadWidgetState createState() => _GSYPullLoadWidgetState(this.control, this.itemBuilder, this.onRefresh, this.onLoadMore, this.refreshKey);
3030
}
3131

3232
class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
@@ -36,9 +36,11 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
3636

3737
final RefreshCallback onRefresh;
3838

39+
final Key refreshKey;
40+
3941
GSYPullLoadWidgetControl control;
4042

41-
_GSYPullLoadWidgetState(this.control, this.itemBuilder, this.onRefresh, this.onLoadMore);
43+
_GSYPullLoadWidgetState(this.control, this.itemBuilder, this.onRefresh, this.onLoadMore, this.refreshKey);
4244

4345
final ScrollController _scrollController = new ScrollController();
4446

@@ -65,6 +67,7 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
6567
@override
6668
Widget build(BuildContext context) {
6769
return new RefreshIndicator(
70+
key: refreshKey,
6871
onRefresh: onRefresh,
6972
child: new ListView.builder(
7073
physics: const AlwaysScrollableScrollPhysics(),
@@ -98,8 +101,10 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
98101
class GSYPullLoadWidgetControl {
99102
///数据,对齐增减,不能替换
100103
List dataList = new List();
104+
101105
///是否需要加载更多
102106
bool needLoadMore = true;
107+
103108
///是否需要头部
104109
bool needHeader = false;
105110
}

‎lib/widget/IssueItem.dart‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ class IssueItem extends StatelessWidget {
2121

2222
@override
2323
Widget build(BuildContext context) {
24+
Color issueStateColor = issueItemViewModel.state == "open" ? Colors.green : Colors.red;
2425
Widget bottomContainer = (needBottom)
2526
? new Container()
2627
: new Row(
2728
children: <Widget>[
2829
new GSYIConText(
2930
GSYICons.ISSUE_ITEM_ISSUE,
3031
issueItemViewModel.state,
31-
GSYConstant.subSmallText,
32-
issueItemViewModel.state == "open" ? Colors.green : Colors.red,
32+
TextStyle(
33+
color: issueStateColor,
34+
fontSize: GSYConstant.smallTextSize,
35+
),
36+
issueStateColor,
3337
15.0,
3438
padding: 2.0,
3539
),
@@ -81,13 +85,10 @@ class IssueItem extends StatelessWidget {
8185
),
8286
],
8387
),
84-
new Padding(
85-
padding: new EdgeInsets.only(left: 0.0, top: 2.0, right: 0.0, bottom: 0.0),
86-
),
8788
new Container(
8889
child: new Text(
8990
issueItemViewModel.issueComment,
90-
style: GSYConstant.smallTextBold,
91+
style: GSYConstant.subSmallText,
9192
maxLines: 2,
9293
),
9394
margin: new EdgeInsets.only(top: 6.0, bottom: 2.0),

0 commit comments

Comments
 (0)