|
1 | 1 | import 'package:cached_network_image/cached_network_image.dart';
|
| 2 | +import 'package:flutter/cupertino.dart'; |
2 | 3 | import 'package:flutter/material.dart';
|
3 | 4 | import 'package:flutter_bloc/flutter_bloc.dart';
|
4 | 5 | import 'package:flutter_swiper/flutter_swiper.dart';
|
@@ -28,10 +29,16 @@ class _ProjectSubPageState extends State<ProjectSubPage>
|
28 | 29 | List<BannerEntity> banners;
|
29 | 30 | List<ProjectTypeEntity> projectTypes;
|
30 | 31 | List<ProjectEntity> projectDatas;
|
| 32 | + ScrollController _scrollController; |
| 33 | + int currentProjectPage; |
| 34 | + int totalProjectPage; |
| 35 | + bool isLoading = false; |
| 36 | + GlobalKey<RefreshIndicatorState> refreshKey; |
31 | 37 |
|
32 | 38 | @override
|
33 | 39 | void initState() {
|
34 | 40 | super.initState();
|
| 41 | + refreshKey = GlobalKey(); |
35 | 42 | projectBloc = ProjectBloc(BlocProvider.of<HomeBloc>(context));
|
36 | 43 | banners ??= [];
|
37 | 44 | projectTypes ??= [
|
@@ -81,20 +88,41 @@ class _ProjectSubPageState extends State<ProjectSubPage>
|
81 | 88 | ProjectTypeEntity.t(1, '1开发模式'),
|
82 | 89 | ];
|
83 | 90 | projectDatas ??= [];
|
84 |
| - t(); |
| 91 | + currentProjectPage ??= 1; |
| 92 | + totalProjectPage ??= 1; |
| 93 | + _scrollController ??= ScrollController(); |
| 94 | + _scrollController.addListener(() { |
| 95 | + // 如果下拉的当前位置到scroll的最下面 |
| 96 | + if (_scrollController.position.pixels == |
| 97 | + _scrollController.position.maxScrollExtent) { |
| 98 | + if (currentProjectPage < totalProjectPage && !isLoading) { |
| 99 | + t(currentProjectPage + 1); |
| 100 | + } |
| 101 | + } |
| 102 | + }); |
| 103 | + t(currentProjectPage); |
85 | 104 | }
|
86 | 105 |
|
87 |
| - Future t() async { |
| 106 | + Future t(int page) async { |
| 107 | + isLoading = true; |
88 | 108 | await Future.delayed(Duration(seconds: 2));
|
89 |
| - Response res = |
90 |
| - await dio.get('https://www.wanandroid.com/article/listproject/0/json'); |
| 109 | + Response res = await ProjectApi.getNewProjects(page); |
91 | 110 | await dio.get('https://www.wanandroid.com/lg/collect/list/0/json');
|
92 | 111 | BaseEntity baseEntity = BaseEntity.fromJson(res.data);
|
93 | 112 | BaseListEntity<List> baseListEntity =
|
94 | 113 | BaseListEntity.fromJson(baseEntity.data);
|
95 |
| - projectDatas = baseListEntity.datas.map((json) { |
96 |
| - return ProjectEntity.fromJson(json); |
97 |
| - }).toList(); |
| 114 | + currentProjectPage = baseListEntity.curPage; |
| 115 | + totalProjectPage = baseListEntity.pageCount; |
| 116 | + if (projectDatas == null || projectDatas.length == 0) { |
| 117 | + projectDatas = baseListEntity.datas.map((json) { |
| 118 | + return ProjectEntity.fromJson(json); |
| 119 | + }).toList(); |
| 120 | + } else { |
| 121 | + projectDatas.addAll(baseListEntity.datas.map((json) { |
| 122 | + return ProjectEntity.fromJson(json); |
| 123 | + }).toList()); |
| 124 | + } |
| 125 | + isLoading = false; |
98 | 126 | setState(() {});
|
99 | 127 | }
|
100 | 128 |
|
@@ -127,69 +155,92 @@ class _ProjectSubPageState extends State<ProjectSubPage>
|
127 | 155 | child: BlocBuilder<ProjectEvent, ProjectState>(
|
128 | 156 | bloc: projectBloc,
|
129 | 157 | builder: (context, state) {
|
130 |
| - return CustomScrollView( |
131 |
| - physics: AlwaysScrollableScrollPhysics( |
132 |
| - parent: BouncingScrollPhysics()), |
133 |
| - slivers: <Widget>[ |
134 |
| - SliverToBoxAdapter( |
135 |
| - child: bannerView( |
136 |
| - datas: banners.map((entity) { |
137 |
| - return BannerModel( |
138 |
| - entity.title ?? '', |
139 |
| - entity.imagePath ?? '', |
140 |
| - entity.url ?? '', |
141 |
| - ); |
142 |
| - }).toList()), |
143 |
| - ), |
144 |
| - SliverToBoxAdapter( |
145 |
| - child: typesGridView( |
146 |
| - datas: projectTypes |
147 |
| - .asMap() |
148 |
| - .map<int, ProjectTypesModel>((index, entity) { |
149 |
| - return MapEntry( |
150 |
| - index, |
151 |
| - ProjectTypesModel( |
152 |
| - entity.id, |
153 |
| - entity.name, |
154 |
| - Image.asset( |
155 |
| - MyImage.animals[index % MyImage.animals.length], |
156 |
| - width: pt(40), |
157 |
| - height: pt(40), |
| 158 | + return RefreshIndicator( |
| 159 | + key: refreshKey, |
| 160 | + color: WColors.theme_color, |
| 161 | + onRefresh: ()async{ |
| 162 | + //todo 在bloc模式里这里该怎么做?如何弄一个可以自己控制阻塞、完成的future对象? |
| 163 | + await Future.delayed(Duration(seconds: 2)); |
| 164 | + }, |
| 165 | + child: CustomScrollView( |
| 166 | + controller: _scrollController, |
| 167 | + physics: AlwaysScrollableScrollPhysics( |
| 168 | + parent: BouncingScrollPhysics()), |
| 169 | + slivers: <Widget>[ |
| 170 | + SliverToBoxAdapter( |
| 171 | + child: bannerView( |
| 172 | + datas: banners.map((entity) { |
| 173 | + return BannerModel( |
| 174 | + entity.title ?? '', |
| 175 | + entity.imagePath ?? '', |
| 176 | + entity.url ?? '', |
| 177 | + ); |
| 178 | + }).toList()), |
| 179 | + ), |
| 180 | + SliverToBoxAdapter( |
| 181 | + child: typesGridView( |
| 182 | + datas: projectTypes |
| 183 | + .asMap() |
| 184 | + .map<int, ProjectTypesModel>((index, entity) { |
| 185 | + return MapEntry( |
| 186 | + index, |
| 187 | + ProjectTypesModel( |
| 188 | + entity.id, |
| 189 | + entity.name, |
| 190 | + Image.asset( |
| 191 | + MyImage.animals[index % MyImage.animals.length], |
| 192 | + width: pt(40), |
| 193 | + height: pt(40), |
| 194 | + ), |
158 | 195 | ),
|
159 |
| - ), |
160 |
| - ); |
161 |
| - }) |
162 |
| - .values |
163 |
| - .toList(), |
| 196 | + ); |
| 197 | + }) |
| 198 | + .values |
| 199 | + .toList(), |
| 200 | + ), |
164 | 201 | ),
|
165 |
| - ), |
166 |
| - SliverToBoxAdapter( |
167 |
| - child: Container( |
168 |
| - color: WColors.gray_background, |
169 |
| - padding: EdgeInsets.only(left: pt(16), top: pt(8)), |
170 |
| - child: Row( |
171 |
| - children: <Widget>[ |
172 |
| - Image.asset( |
173 |
| - 'images/new.png', |
174 |
| - width: pt(30), |
175 |
| - height: pt(30), |
176 |
| - ), |
177 |
| - SizedBox( |
178 |
| - width: pt(10), |
179 |
| - ), |
180 |
| - Text( |
181 |
| - res.newestProject, |
182 |
| - style: TextStyle( |
183 |
| - color: Colors.black, |
184 |
| - fontSize: 17, |
185 |
| - fontWeight: FontWeight.bold), |
186 |
| - ) |
187 |
| - ], |
| 202 | + SliverToBoxAdapter( |
| 203 | + child: Container( |
| 204 | + color: WColors.gray_background, |
| 205 | + padding: EdgeInsets.only(left: pt(16), top: pt(8)), |
| 206 | + child: Row( |
| 207 | + children: <Widget>[ |
| 208 | + Image.asset( |
| 209 | + 'images/new.png', |
| 210 | + width: pt(30), |
| 211 | + height: pt(30), |
| 212 | + ), |
| 213 | + SizedBox( |
| 214 | + width: pt(10), |
| 215 | + ), |
| 216 | + Text( |
| 217 | + res.newestProject, |
| 218 | + style: TextStyle( |
| 219 | + color: Colors.black, |
| 220 | + fontSize: 17, |
| 221 | + fontWeight: FontWeight.bold), |
| 222 | + ) |
| 223 | + ], |
| 224 | + ), |
188 | 225 | ),
|
189 | 226 | ),
|
190 |
| - ), |
191 |
| - projectGrid(datas: projectDatas), |
192 |
| - ], |
| 227 | + projectGrid(datas: projectDatas), |
| 228 | + SliverToBoxAdapter( |
| 229 | + child: Container( |
| 230 | + width: double.infinity, |
| 231 | + height: pt(45), |
| 232 | + color: WColors.gray_background, |
| 233 | + alignment: Alignment.center, |
| 234 | + child: (currentProjectPage < totalProjectPage) |
| 235 | + ? CupertinoActivityIndicator() |
| 236 | + : Text( |
| 237 | + res.isBottomst, |
| 238 | + style: TextStyle(color: WColors.hint_color), |
| 239 | + ), |
| 240 | + ), |
| 241 | + ), |
| 242 | + ], |
| 243 | + ), |
193 | 244 | );
|
194 | 245 | },
|
195 | 246 | ),
|
@@ -444,7 +495,7 @@ class _ProjectSubPageState extends State<ProjectSubPage>
|
444 | 495 | child: GestureDetector(
|
445 | 496 | onTap: () {
|
446 | 497 | print('click item');
|
447 |
| - t(); |
| 498 | + refreshKey.currentState.show(); |
448 | 499 | },
|
449 | 500 | child: Column(
|
450 | 501 | crossAxisAlignment: CrossAxisAlignment.start,
|
@@ -530,7 +581,7 @@ class _ProjectSubPageState extends State<ProjectSubPage>
|
530 | 581 | Icon(
|
531 | 582 | Icons.chevron_right,
|
532 | 583 | color: WColors.hint_color_dark,
|
533 |
| - size: pt(10), |
| 584 | + size: pt(12), |
534 | 585 | ),
|
535 | 586 | Expanded(
|
536 | 587 | child: Align(
|
|
0 commit comments