@@ -3,20 +3,19 @@ import 'dart:async';
33import 'package:flutter/material.dart' ;
44import 'package:flutter_redux/flutter_redux.dart' ;
55import 'package:fluttertoast/fluttertoast.dart' ;
6- import 'package:gsy_github_app_flutter/bloc/base/base_bloc.dart' ;
76import 'package:gsy_github_app_flutter/bloc/trend_bloc.dart' ;
7+ import 'package:gsy_github_app_flutter/common/model/TrendingRepoModel.dart' ;
88import 'package:gsy_github_app_flutter/common/redux/gsy_state.dart' ;
99import 'package:gsy_github_app_flutter/common/style/gsy_style.dart' ;
1010import 'package:gsy_github_app_flutter/common/utils/common_utils.dart' ;
1111import 'package:gsy_github_app_flutter/common/utils/navigator_utils.dart' ;
1212import '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' ;
1513import 'package:gsy_github_app_flutter/widget/repos_item.dart' ;
1614import '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