1+ import 'package:flutter/cupertino.dart' as IOS;
12import 'package:flutter/material.dart' ;
23import 'package:flutter_spinkit/flutter_spinkit.dart' ;
34import 'package:gsy_github_app_flutter/common/style/gsy_style.dart' ;
@@ -17,33 +18,43 @@ class GSYPullLoadWidget extends StatefulWidget {
1718 ///控制器,比如数据和一些配置
1819 final GSYPullLoadWidgetControl control;
1920
21+ final ScrollController scrollController;
22+
23+ final bool userIos;
24+
2025 ///刷新key
2126 final Key refreshKey;
2227
23- GSYPullLoadWidget (this .control, this .itemBuilder, this .onRefresh, this .onLoadMore, {this .refreshKey});
28+ GSYPullLoadWidget (
29+ this .control, this .itemBuilder, this .onRefresh, this .onLoadMore,
30+ {this .refreshKey, this .scrollController, this .userIos = false });
2431
2532 @override
2633 _GSYPullLoadWidgetState createState () => _GSYPullLoadWidgetState ();
2734}
2835
2936class _GSYPullLoadWidgetState extends State <GSYPullLoadWidget > {
30- final ScrollController _scrollController = new ScrollController () ;
37+ ScrollController _scrollController;
3138
3239 bool isRefreshing = false ;
3340
3441 bool isLoadMoring = false ;
3542
3643 @override
3744 void initState () {
45+ _scrollController = widget.scrollController ?? new ScrollController ();
46+
3847 ///增加滑动监听
3948 _scrollController.addListener (() {
4049 ///判断当前滑动位置是不是到达底部,触发加载更多回调
41- if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
50+ if (_scrollController.position.pixels ==
51+ _scrollController.position.maxScrollExtent) {
4252 if (widget.control.needLoadMore) {
4353 handleLoadMore ();
4454 }
4555 }
4656 });
57+
4758 widget.control.addListener (() {
4859 setState (() {});
4960 try {
@@ -66,27 +77,36 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
6677 if (widget.control.needHeader) {
6778 ///如果需要头部,用Item 0 的 Widget 作为ListView的头部
6879 ///列表数量大于0时,因为头部和底部加载更多选项,需要对列表数据总数+2
69- return (widget.control.dataList.length > 0 ) ? widget.control.dataList.length + 2 : widget.control.dataList.length + 1 ;
80+ return (widget.control.dataList.length > 0 )
81+ ? widget.control.dataList.length + 2
82+ : widget.control.dataList.length + 1 ;
7083 } else {
7184 ///如果不需要头部,在没有数据时,固定返回数量1用于空页面呈现
7285 if (widget.control.dataList.length == 0 ) {
7386 return 1 ;
7487 }
7588
7689 ///如果有数据,因为部加载更多选项,需要对列表数据总数+1
77- return (widget.control.dataList.length > 0 ) ? widget.control.dataList.length + 1 : widget.control.dataList.length;
90+ return (widget.control.dataList.length > 0 )
91+ ? widget.control.dataList.length + 1
92+ : widget.control.dataList.length;
7893 }
7994 }
8095
8196 ///根据配置状态返回实际列表渲染Item
8297 _getItem (int index) {
83- if (! widget.control.needHeader && index == widget.control.dataList.length && widget.control.dataList.length != 0 ) {
98+ if (! widget.control.needHeader &&
99+ index == widget.control.dataList.length &&
100+ widget.control.dataList.length != 0 ) {
84101 ///如果不需要头部,并且数据不为0,当index等于数据长度时,渲染加载更多Item(因为index是从0开始)
85102 return _buildProgressIndicator ();
86- } else if (widget.control.needHeader && index == _getListCount () - 1 && widget.control.dataList.length != 0 ) {
103+ } else if (widget.control.needHeader &&
104+ index == _getListCount () - 1 &&
105+ widget.control.dataList.length != 0 ) {
87106 ///如果需要头部,并且数据不为0,当index等于实际渲染长度 - 1时,渲染加载更多Item(因为index是从0开始)
88107 return _buildProgressIndicator ();
89- } else if (! widget.control.needHeader && widget.control.dataList.length == 0 ) {
108+ } else if (! widget.control.needHeader &&
109+ widget.control.dataList.length == 0 ) {
90110 ///如果不需要头部,并且数据为0,渲染空页面
91111 return _buildEmpty ();
92112 } else {
@@ -148,6 +168,33 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
148168
149169 @override
150170 Widget build (BuildContext context) {
171+ if (widget.userIos) {
172+ ///用ios模式的下拉刷新
173+ return CustomScrollView (
174+ controller: _scrollController,
175+ ///回弹效果
176+ physics: const BouncingScrollPhysics (
177+ parent: AlwaysScrollableScrollPhysics (),
178+ ),
179+ slivers: < Widget > [
180+ ///控制显示刷新的 CupertinoSliverRefreshControl
181+ IOS .CupertinoSliverRefreshControl (
182+ onRefresh: handleRefresh,
183+ ),
184+ SliverSafeArea (
185+ sliver: SliverList (
186+ delegate: SliverChildBuilderDelegate (
187+ (BuildContext context, int index) {
188+ return _getItem (index);
189+ },
190+ childCount: _getListCount (),
191+ ),
192+ ),
193+ ),
194+ ],
195+ );
196+ }
197+
151198 return new RefreshIndicator (
152199 ///GlobalKey,用户外部获取RefreshIndicator的State,做显示刷新
153200 key: widget.refreshKey,
@@ -181,10 +228,14 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
181228 children: < Widget > [
182229 FlatButton (
183230 onPressed: () {},
184- child: new Image (image: new AssetImage (GSYICons .DEFAULT_USER_ICON ), width: 70.0 , height: 70.0 ),
231+ child: new Image (
232+ image: new AssetImage (GSYICons .DEFAULT_USER_ICON ),
233+ width: 70.0 ,
234+ height: 70.0 ),
185235 ),
186236 Container (
187- child: Text (CommonUtils .getLocale (context).app_empty, style: GSYConstant .normalText),
237+ child: Text (CommonUtils .getLocale (context).app_empty,
238+ style: GSYConstant .normalText),
188239 ),
189240 ],
190241 ),
@@ -195,23 +246,26 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
195246 Widget _buildProgressIndicator () {
196247 ///是否需要显示上拉加载更多的loading
197248 Widget bottomWidget = (widget.control.needLoadMore)
198- ? new Row (mainAxisAlignment: MainAxisAlignment .center, children: < Widget > [
199- ///loading框
200- new SpinKitRotatingCircle (color: Theme .of (context).primaryColor),
201- new Container (
202- width: 5.0 ,
203- ),
204-
205- ///加载中文本
206- new Text (
207- CommonUtils .getLocale (context).load_more_text,
208- style: TextStyle (
209- color: Color (0xFF121917 ),
210- fontSize: 14.0 ,
211- fontWeight: FontWeight .bold,
212- ),
213- )
214- ])
249+ ? new Row (
250+ mainAxisAlignment: MainAxisAlignment .center,
251+ children: < Widget > [
252+ ///loading框
253+ new SpinKitRotatingCircle (
254+ color: Theme .of (context).primaryColor),
255+ new Container (
256+ width: 5.0 ,
257+ ),
258+
259+ ///加载中文本
260+ new Text (
261+ CommonUtils .getLocale (context).load_more_text,
262+ style: TextStyle (
263+ color: Color (0xFF121917 ),
264+ fontSize: 14.0 ,
265+ fontWeight: FontWeight .bold,
266+ ),
267+ )
268+ ])
215269
216270 /// 不需要加载
217271 : new Container ();
0 commit comments