Skip to content

Commit cd8bcd7

Browse files
committed
改造 trend
1 parent 9b728b1 commit cd8bcd7

4 files changed

Lines changed: 112 additions & 40 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
- 一些布局可以简化重构(但是改起来好懒怎么办?)
3737

3838

39+
```!
40+
因为是偏学习项目,所以项目里会才有各式各样的模式和库,请不要介意
41+
42+
1、 TrendPage : 目前采用纯 bloc 的 rxdart(stream) + streamBuilder 模式效果
43+
```
44+
3945
## 相关文章
4046

4147
* ### [Flutter 完整开发实战详解(一、Dart 语言和 Flutter 基础)](https://juejin.im/entry/5b631e3e51882519861c2ef1 )

‎lib/bloc/trend_bloc.dart‎

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
1-
import 'package:gsy_github_app_flutter/bloc/base/base_bloc.dart';
21
import 'package:gsy_github_app_flutter/common/dao/repos_dao.dart';
2+
import 'package:gsy_github_app_flutter/common/model/TrendingRepoModel.dart';
3+
import 'package:rxdart/rxdart.dart';
34

45
/**
56
* Created by guoshuyu
67
* on 2019/3/23.
78
*/
8-
class TrendBloc extends BlocListBase {
9-
requestRefresh(selectTime, selectType) async {
10-
pageReset();
9+
class TrendBloc {
10+
bool _requested = false;
11+
12+
bool _isLoading = false;
13+
14+
///是否正在loading
15+
bool get isLoading => _isLoading;
16+
17+
///是否已经请求过
18+
bool get requested => _requested;
19+
20+
///rxdart 实现的 stream
21+
var _subject = PublishSubject<List<TrendingRepoModel>>();
22+
23+
Observable<List<TrendingRepoModel>> get stream => _subject.stream;
24+
25+
///根据数据库和网络返回数据
26+
Future<void> requestRefresh(selectTime, selectType) async {
27+
_isLoading = true;
28+
//_subject.add([]);
1129
var res = await ReposDao.getTrendDao(since: selectTime.value, languageType: selectType.value);
12-
changeLoadMoreStatus(getLoadMoreStatus(res));
13-
refreshData(res);
30+
if (res != null && res.result) {
31+
_subject.add(res.data);
32+
}
1433
await doNext(res);
15-
return res;
34+
_isLoading = false;
35+
_requested = true;
36+
return;
37+
}
38+
39+
///请求next,是否有网络
40+
doNext(res) async {
41+
if (res.next != null) {
42+
var resNext = await res.next;
43+
if (resNext != null && resNext.result) {
44+
if (res != null && res.result) {
45+
_subject.add(res.data);
46+
}
47+
}
48+
}
1649
}
1750
}

‎lib/common/dao/repos_dao.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ReposDao {
7373

7474
if (needDb) {
7575
List<TrendingRepoModel> list = await provider.getData(languageTypeDb, since);
76-
if (list != null && list.length > 0) {
76+
if (list == null || list.length == 0) {
7777
return await next();
7878
}
7979
DataResult dataResult = new DataResult(list, true, next: next());

‎lib/page/trend_page.dart‎

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ import 'dart:async';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_redux/flutter_redux.dart';
55
import 'package:fluttertoast/fluttertoast.dart';
6-
import 'package:gsy_github_app_flutter/bloc/base/base_bloc.dart';
76
import 'package:gsy_github_app_flutter/bloc/trend_bloc.dart';
7+
import 'package:gsy_github_app_flutter/common/model/TrendingRepoModel.dart';
88
import 'package:gsy_github_app_flutter/common/redux/gsy_state.dart';
99
import 'package:gsy_github_app_flutter/common/style/gsy_style.dart';
1010
import 'package:gsy_github_app_flutter/common/utils/common_utils.dart';
1111
import 'package:gsy_github_app_flutter/common/utils/navigator_utils.dart';
1212
import 'package:gsy_github_app_flutter/widget/gsy_card_item.dart';
13-
import 'package:gsy_github_app_flutter/widget/gsy_bloc_list_state.dart';
14-
import 'package:gsy_github_app_flutter/widget/gsy_pull_new_load_widget.dart';
1513
import 'package:gsy_github_app_flutter/widget/repos_item.dart';
1614
import 'package:redux/redux.dart';
1715

1816
/**
1917
* 主页趋势tab页
18+
* 目前采用纯 bloc 的 rxdart(stream) + streamBuilder
2019
* Created by guoshuyu
2120
* Date: 2018-07-16
2221
*/
@@ -25,13 +24,23 @@ class TrendPage extends StatefulWidget {
2524
_TrendPageState createState() => _TrendPageState();
2625
}
2726

28-
class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixin<TrendPage>, GSYListState<TrendPage> {
27+
class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixin<TrendPage> {
2928
static TrendTypeModel selectTime = null;
3029

3130
static TrendTypeModel selectType = null;
3231

32+
final GlobalKey<RefreshIndicatorState> refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
33+
3334
final TrendBloc trendBloc = new TrendBloc();
3435

36+
///显示刷新
37+
_showRefreshLoading() {
38+
new Future.delayed(const Duration(seconds: 0), () {
39+
refreshIndicatorKey.currentState.show().then((e) {});
40+
return true;
41+
});
42+
}
43+
3544
_renderItem(e) {
3645
ReposViewModel reposViewModel = ReposViewModel.fromTrendMap(e);
3746
return new ReposItem(reposViewModel, onPressed: () {
@@ -54,25 +63,25 @@ class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixi
5463
child: new Row(
5564
children: <Widget>[
5665
_renderHeaderPopItem(selectTime.name, trendTime(context), (TrendTypeModel result) {
57-
if (bloc.pullLoadWidgetControl.isLoading) {
66+
if (trendBloc.isLoading) {
5867
Fluttertoast.showToast(msg: CommonUtils.getLocale(context).loading_text);
5968
return;
6069
}
6170
setState(() {
6271
selectTime = result;
6372
});
64-
showRefreshLoading();
73+
_showRefreshLoading();
6574
}),
6675
new Container(height: 10.0, width: 0.5, color: Color(GSYColors.white)),
6776
_renderHeaderPopItem(selectType.name, trendType(context), (TrendTypeModel result) {
68-
if (bloc.pullLoadWidgetControl.isLoading) {
77+
if (trendBloc.isLoading) {
6978
Fluttertoast.showToast(msg: CommonUtils.getLocale(context).loading_text);
7079
return;
7180
}
7281
setState(() {
7382
selectType = result;
7483
});
75-
showRefreshLoading();
84+
_showRefreshLoading();
7685
}),
7786
],
7887
),
@@ -103,34 +112,50 @@ class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixi
103112
return list;
104113
}
105114

106-
@override
107-
requestRefresh() async {
108-
return await trendBloc.requestRefresh(selectTime, selectType);
115+
Future<void> requestRefresh() async {
116+
return trendBloc.requestRefresh(selectTime, selectType);
109117
}
110118

111119
@override
112-
requestLoadMore() async {
113-
return null;
114-
}
115-
116-
@override
117-
BlocListBase get bloc => trendBloc;
118-
119-
@override
120-
bool get isRefreshFirst => false;
120+
bool get wantKeepAlive => true;
121121

122122
@override
123123
void didChangeDependencies() {
124-
if (bloc.getDataLength() == 0) {
124+
if (!trendBloc.requested) {
125125
setState(() {
126126
selectTime = trendTime(context)[0];
127127
selectType = trendType(context)[0];
128128
});
129-
showRefreshLoading();
129+
_showRefreshLoading();
130130
}
131131
super.didChangeDependencies();
132132
}
133133

134+
///空页面
135+
Widget _buildEmpty() {
136+
var statusBar = MediaQueryData.fromWindow(WidgetsBinding.instance.window).padding.top;
137+
var bottomArea = MediaQueryData.fromWindow(WidgetsBinding.instance.window).padding.bottom;
138+
var height = MediaQuery.of(context).size.height - statusBar - bottomArea - kBottomNavigationBarHeight - kToolbarHeight;
139+
return SingleChildScrollView(
140+
child: new Container(
141+
height: height,
142+
width: MediaQuery.of(context).size.width,
143+
child: new Column(
144+
mainAxisAlignment: MainAxisAlignment.center,
145+
children: <Widget>[
146+
FlatButton(
147+
onPressed: () {},
148+
child: new Image(image: new AssetImage(GSYICons.DEFAULT_USER_ICON), width: 70.0, height: 70.0),
149+
),
150+
Container(
151+
child: Text(CommonUtils.getLocale(context).app_empty, style: GSYConstant.normalText),
152+
),
153+
],
154+
),
155+
),
156+
);
157+
}
158+
134159
@override
135160
Widget build(BuildContext context) {
136161
super.build(context); // See AutomaticKeepAliveClientMixin.
@@ -144,16 +169,24 @@ class _TrendPageState extends State<TrendPage> with AutomaticKeepAliveClientMixi
144169
leading: new Container(),
145170
elevation: 0.0,
146171
),
147-
body: BlocProvider<TrendBloc>(
148-
bloc: trendBloc,
149-
child: GSYPullLoadWidget(
150-
bloc.pullLoadWidgetControl,
151-
(BuildContext context, int index) => _renderItem(bloc.dataList[index]),
152-
requestRefresh,
153-
requestLoadMore,
154-
refreshKey: refreshIndicatorKey,
155-
),
156-
),
172+
///采用目前采用纯 bloc 的 rxdart(stream) + streamBuilder
173+
body: StreamBuilder<List<TrendingRepoModel>>(
174+
stream: trendBloc.stream,
175+
builder: (context, snapShot) {
176+
return new RefreshIndicator(
177+
key: refreshIndicatorKey,
178+
onRefresh: requestRefresh,
179+
child: (snapShot.data == null || snapShot.data.length == 0)
180+
? _buildEmpty()
181+
: new ListView.builder(
182+
physics: const AlwaysScrollableScrollPhysics(),
183+
itemBuilder: (context, index) {
184+
return _renderItem(snapShot.data[index]);
185+
},
186+
itemCount: snapShot.data.length,
187+
),
188+
);
189+
}),
157190
);
158191
},
159192
);

0 commit comments

Comments
 (0)