Skip to content

Commit cbbf964

Browse files
committed
初步修改为bloc
1 parent 4ab9374 commit cbbf964

File tree

8 files changed

+81
-112
lines changed

8 files changed

+81
-112
lines changed

lib/bloc/base/base_bloc.dart

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class BlocProvider<T extends BlocBase> extends StatefulWidget {
2929
}
3030

3131
class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> {
32+
33+
3234
@override
3335
void dispose() {
3436
widget.bloc.dispose();
@@ -39,17 +41,34 @@ class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> {
3941
Widget build(BuildContext context) {
4042
return widget.child;
4143
}
44+
45+
@override
46+
void initState() {
47+
super.initState();
48+
widget.bloc.initState();
49+
50+
}
4251
}
4352

4453
abstract class BlocListBase extends BlocBase {
4554

55+
bool _isShow = false;
56+
4657
int _page = 1;
4758

4859
final GSYPullLoadWidgetControl pullLoadWidgetControl = new GSYPullLoadWidgetControl();
4960

61+
@mustCallSuper
62+
@override
63+
void initState() {
64+
_isShow = true;
65+
pullLoadWidgetControl.dispose();
66+
}
67+
5068
@mustCallSuper
5169
@override
5270
void dispose() {
71+
_isShow = false;
5372
pullLoadWidgetControl.dispose();
5473
}
5574

@@ -68,8 +87,16 @@ abstract class BlocListBase extends BlocBase {
6887
return (res != null && res.data != null && res.data.length == Config.PAGE_SIZE);
6988
}
7089

71-
int get page => _page;
90+
@protected
91+
resolveRefreshResult(res) {
92+
if (res != null && res.result) {
93+
if (_isShow) {
94+
refreshData(res.data);
95+
}
96+
}
97+
}
7298

99+
int get page => _page;
73100

74101
///列表数据长度
75102
int getDataLength() {
@@ -110,5 +137,8 @@ abstract class BlocListBase extends BlocBase {
110137
}
111138

112139
abstract class BlocBase {
140+
141+
void initState();
142+
113143
void dispose();
114144
}

lib/bloc/dynamic_bloc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:gsy_github_app_flutter/bloc/base/base_bloc.dart';
2-
import 'package:gsy_github_app_flutter/common/config/config.dart';
32
import 'package:gsy_github_app_flutter/common/dao/event_dao.dart';
43

54
/**
@@ -20,4 +19,5 @@ class DynamicBloc extends BlocListBase {
2019
changeLoadMoreStatus(getLoadMoreStatus(res));
2120
return res;
2221
}
22+
2323
}

lib/common/dao/event_dao.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,4 @@ class EventDao {
8484
}
8585
return await next();
8686
}
87-
88-
static clearEvent(Store store) {
89-
store.state.eventList.clear();
90-
91-
/// todo 清理
92-
}
9387
}

lib/page/dynamic_page.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,13 @@ class _DynamicPageState extends State<DynamicPage> with AutomaticKeepAliveClient
9292
super.build(context); // See AutomaticKeepAliveClientMixin.
9393
return new StoreBuilder<GSYState>(
9494
builder: (context, store) {
95-
bloc.changeNeedHeaderStatus(needHeader);
9695
return BlocProvider<DynamicBloc>(
9796
bloc: dynamicBloc,
9897
child: GSYPullLoadWidget(
9998
bloc.pullLoadWidgetControl,
10099
(BuildContext context, int index) => _renderEventItem(bloc.dataList[index]),
101-
handleRefresh,
102-
onLoadMore,
100+
requestRefresh,
101+
requestLoadMore,
103102
refreshKey: refreshIndicatorKey,
104103
),
105104
);

lib/page/trend_page.dart

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'package:flutter_redux/flutter_redux.dart';
55
import 'package:fluttertoast/fluttertoast.dart';
66
import 'package:gsy_github_app_flutter/bloc/base/base_bloc.dart';
77
import 'package:gsy_github_app_flutter/bloc/trend_bloc.dart';
8-
import 'package:gsy_github_app_flutter/common/dao/repos_dao.dart';
98
import 'package:gsy_github_app_flutter/common/redux/gsy_state.dart';
109
import 'package:gsy_github_app_flutter/common/style/gsy_style.dart';
1110
import 'package:gsy_github_app_flutter/common/utils/common_utils.dart';
@@ -55,7 +54,7 @@ class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixi
5554
child: new Row(
5655
children: <Widget>[
5756
_renderHeaderPopItem(selectTime.name, trendTime(context), (TrendTypeModel result) {
58-
if (isLoading) {
57+
if (bloc.pullLoadWidgetControl.isLoading) {
5958
Fluttertoast.showToast(msg: CommonUtils.getLocale(context).loading_text);
6059
return;
6160
}
@@ -66,7 +65,7 @@ class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixi
6665
}),
6766
new Container(height: 10.0, width: 0.5, color: Color(GSYColors.white)),
6867
_renderHeaderPopItem(selectType.name, trendType(context), (TrendTypeModel result) {
69-
if (isLoading) {
68+
if (bloc.pullLoadWidgetControl.isLoading) {
7069
Fluttertoast.showToast(msg: CommonUtils.getLocale(context).loading_text);
7170
return;
7271
}
@@ -132,18 +131,11 @@ class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixi
132131
super.didChangeDependencies();
133132
}
134133

135-
@override
136-
void dispose() {
137-
super.dispose();
138-
clearData();
139-
}
140-
141134
@override
142135
Widget build(BuildContext context) {
143136
super.build(context); // See AutomaticKeepAliveClientMixin.
144137
return new StoreBuilder<GSYState>(
145138
builder: (context, store) {
146-
bloc.changeNeedHeaderStatus(needHeader);
147139
return new Scaffold(
148140
backgroundColor: Color(GSYColors.mainBackgroundColor),
149141
appBar: new AppBar(
@@ -157,8 +149,8 @@ class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixi
157149
child: GSYPullLoadWidget(
158150
bloc.pullLoadWidgetControl,
159151
(BuildContext context, int index) => _renderItem(bloc.dataList[index]),
160-
handleRefresh,
161-
onLoadMore,
152+
requestRefresh,
153+
requestLoadMore,
162154
refreshKey: refreshIndicatorKey,
163155
),
164156
),

lib/widget/gsy_bloc_list_state.dart

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,66 +13,14 @@ mixin GSYListState<T extends StatefulWidget> on State<T>, AutomaticKeepAliveClie
1313

1414
final GlobalKey<RefreshIndicatorState> refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
1515

16-
bool isShow = false;
17-
18-
bool isLoading = false;
19-
20-
16+
///显示刷新
2117
showRefreshLoading() {
2218
new Future.delayed(const Duration(seconds: 0), () {
2319
refreshIndicatorKey.currentState.show().then((e) {});
2420
return true;
2521
});
2622
}
2723

28-
@protected
29-
resolveRefreshResult(res) {
30-
if (res != null && res.result) {
31-
if (isShow) {
32-
bloc.refreshData(res.data);
33-
}
34-
}
35-
}
36-
37-
@protected
38-
Future<Null> handleRefresh() async {
39-
if (isLoading) {
40-
return null;
41-
}
42-
isLoading = true;
43-
var res = await requestRefresh();
44-
resolveRefreshResult(res);
45-
if (res.next != null) {
46-
var resNext = await res.next;
47-
resolveRefreshResult(resNext);
48-
}
49-
isLoading = false;
50-
return null;
51-
}
52-
53-
@protected
54-
Future<Null> onLoadMore() async {
55-
if (isLoading) {
56-
return null;
57-
}
58-
isLoading = true;
59-
var res = await requestLoadMore();
60-
if (res != null && res.result) {
61-
if (isShow) {
62-
bloc.loadMoreData(res.data);
63-
}
64-
}
65-
isLoading = false;
66-
return null;
67-
}
68-
69-
@protected
70-
clearData() {
71-
if (isShow) {
72-
bloc.clearData();
73-
}
74-
}
75-
7624
///下拉刷新数据
7725
@protected
7826
requestRefresh();
@@ -98,17 +46,10 @@ mixin GSYListState<T extends StatefulWidget> on State<T>, AutomaticKeepAliveClie
9846

9947
@override
10048
void initState() {
101-
isShow = true;
10249
super.initState();
50+
bloc.changeNeedHeaderStatus(needHeader);
10351
if (bloc.getDataLength() == 0 && isRefreshFirst) {
10452
showRefreshLoading();
10553
}
10654
}
107-
108-
@override
109-
void dispose() {
110-
isShow = false;
111-
isLoading = false;
112-
super.dispose();
113-
}
11455
}

lib/widget/gsy_pull_new_load_widget.dart

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,16 @@ class GSYPullLoadWidget extends StatefulWidget {
1717
///控制器,比如数据和一些配置
1818
final GSYPullLoadWidgetControl control;
1919

20+
///刷新key
2021
final Key refreshKey;
2122

2223
GSYPullLoadWidget(this.control, this.itemBuilder, this.onRefresh, this.onLoadMore, {this.refreshKey});
2324

2425
@override
25-
_GSYPullLoadWidgetState createState() => _GSYPullLoadWidgetState(this.control, this.itemBuilder, this.onRefresh, this.onLoadMore, this.refreshKey);
26+
_GSYPullLoadWidgetState createState() => _GSYPullLoadWidgetState();
2627
}
2728

2829
class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
29-
final IndexedWidgetBuilder itemBuilder;
30-
31-
final RefreshCallback onLoadMore;
32-
33-
final RefreshCallback onRefresh;
34-
35-
final Key refreshKey;
36-
37-
GSYPullLoadWidgetControl control;
38-
39-
_GSYPullLoadWidgetState(this.control, this.itemBuilder, this.onRefresh, this.onLoadMore, this.refreshKey);
4030

4131
final ScrollController _scrollController = new ScrollController();
4232

@@ -46,12 +36,12 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
4636
_scrollController.addListener(() {
4737
///判断当前滑动位置是不是到达底部,触发加载更多回调
4838
if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
49-
if (this.control.needLoadMore) {
50-
this.onLoadMore?.call();
39+
if (widget.control.needLoadMore) {
40+
handleLoadMore();
5141
}
5242
}
5343
});
54-
control.addListener(() {
44+
widget.control.addListener(() {
5545
setState(() {});
5646
});
5747
super.initState();
@@ -62,46 +52,68 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
6252
///比如多个头部,是否需要空页面,是否需要显示加载更多。
6353
_getListCount() {
6454
///是否需要头部
65-
if (control.needHeader) {
55+
if (widget.control.needHeader) {
6656
///如果需要头部,用Item 0 的 Widget 作为ListView的头部
6757
///列表数量大于0时,因为头部和底部加载更多选项,需要对列表数据总数+2
68-
return (control.dataList.length > 0) ? control.dataList.length + 2 : control.dataList.length + 1;
58+
return (widget.control.dataList.length > 0) ? widget.control.dataList.length + 2 : widget.control.dataList.length + 1;
6959
} else {
7060
///如果不需要头部,在没有数据时,固定返回数量1用于空页面呈现
71-
if (control.dataList.length == 0) {
61+
if (widget.control.dataList.length == 0) {
7262
return 1;
7363
}
7464

7565
///如果有数据,因为部加载更多选项,需要对列表数据总数+1
76-
return (control.dataList.length > 0) ? control.dataList.length + 1 : control.dataList.length;
66+
return (widget.control.dataList.length > 0) ? widget.control.dataList.length + 1 : widget.control.dataList.length;
7767
}
7868
}
7969

8070
///根据配置状态返回实际列表渲染Item
8171
_getItem(int index) {
82-
if (!control.needHeader && index == control.dataList.length && control.dataList.length != 0) {
72+
if (!widget.control.needHeader && index == widget.control.dataList.length && widget.control.dataList.length != 0) {
8373
///如果不需要头部,并且数据不为0,当index等于数据长度时,渲染加载更多Item(因为index是从0开始)
8474
return _buildProgressIndicator();
85-
} else if (control.needHeader && index == _getListCount() - 1 && control.dataList.length != 0) {
75+
} else if (widget.control.needHeader && index == _getListCount() - 1 && widget.control.dataList.length != 0) {
8676
///如果需要头部,并且数据不为0,当index等于实际渲染长度 - 1时,渲染加载更多Item(因为index是从0开始)
8777
return _buildProgressIndicator();
88-
} else if (!control.needHeader && control.dataList.length == 0) {
78+
} else if (!widget.control.needHeader && widget.control.dataList.length == 0) {
8979
///如果不需要头部,并且数据为0,渲染空页面
9080
return _buildEmpty();
9181
} else {
9282
///回调外部正常渲染Item,如果这里有需要,可以直接返回相对位置的index
93-
return itemBuilder(context, index);
83+
return widget.itemBuilder(context, index);
84+
}
85+
}
86+
87+
@protected
88+
Future<Null> handleRefresh() async {
89+
if (widget.control.isLoading) {
90+
return null;
9491
}
92+
widget.control.isLoading = true;
93+
await widget.onRefresh?.call();
94+
widget.control.isLoading = false;
95+
return null;
96+
}
97+
98+
@protected
99+
Future<Null> handleLoadMore() async {
100+
if (widget.control.isLoading) {
101+
return null;
102+
}
103+
widget.control.isLoading = true;
104+
await widget.onLoadMore?.call();
105+
widget.control.isLoading = false;
106+
return null;
95107
}
96108

97109
@override
98110
Widget build(BuildContext context) {
99111
return new RefreshIndicator(
100112
///GlobalKey,用户外部获取RefreshIndicator的State,做显示刷新
101-
key: refreshKey,
113+
key: widget.refreshKey,
102114

103115
///下拉刷新触发,返回的是一个Future
104-
onRefresh: onRefresh,
116+
onRefresh: handleRefresh,
105117
child: new ListView.builder(
106118
///保持ListView任何情况都能滚动,解决在RefreshIndicator的兼容问题。
107119
physics: const AlwaysScrollableScrollPhysics(),
@@ -142,7 +154,7 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
142154
///上拉加载更多
143155
Widget _buildProgressIndicator() {
144156
///是否需要显示上拉加载更多的loading
145-
Widget bottomWidget = (control.needLoadMore)
157+
Widget bottomWidget = (widget.control.needLoadMore)
146158
? new Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
147159
///loading框
148160
new SpinKitRotatingCircle(color: Theme.of(context).primaryColor),
@@ -208,4 +220,7 @@ class GSYPullLoadWidgetControl extends ChangeNotifier {
208220
}
209221

210222
get needHeader => _needHeader;
223+
224+
///是否加载中
225+
bool isLoading = false;
211226
}

0 commit comments

Comments
 (0)