11.2 右侧商品内容列表编写
效果
实现步骤:
第 1 步:控制器
lib/pages/goods/category/controller.dart
1 2 3 4 5 6 7 8 9 10
| final RefreshController refreshController = RefreshController( initialRefresh: true, );
List<ProductModel> items = [];
int _page = 1;
final int _limit = 20;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
Future<bool> _loadSearch(bool isRefresh) async { var result = await ProductApi.products(ProductsReq( page: isRefresh ? 1 : _page, prePage: _limit, category: categoryId.toString(), ));
if (isRefresh) { _page = 1; items.clear(); }
if (result.isNotEmpty) { _page++;
items.addAll(result); }
return result.isEmpty; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| void onLoading() async { if (items.isNotEmpty) { try { var isEmpty = await _loadSearch(false);
if (isEmpty) { refreshController.loadNoData(); } else { refreshController.loadComplete(); } } catch (e) { refreshController.loadFailed(); } } else { refreshController.loadNoData(); } update(["product_list"]); }
|
1 2 3 4 5 6 7 8 9 10 11
| void onRefresh() async { try { await _loadSearch(true); refreshController.refreshCompleted(); } catch (error) { refreshController.refreshFailed(); } update(["product_list"]); }
|
1 2 3 4 5 6
| @override void onClose() { super.onClose(); refreshController.dispose(); }
|
1 2 3 4 5 6
| void onCategoryTap(int id) async { categoryId = id; refreshController.requestRefresh(); update(["left_nav"]); }
|
第 2 步:视图
lib/pages/goods/category/view.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| Widget _buildProductList() { return GetBuilder<CategoryController>( id: "product_list", builder: (controller) { return SmartRefresher( controller: controller.refreshController, enablePullUp: true, onRefresh: controller.onRefresh, onLoading: controller.onLoading, footer: const SmartRefresherFooterWidget(), child: controller.items.isEmpty ? const PlaceholdWidget() : GridView.builder( itemCount: controller.items.length, itemBuilder: (context, index) { var product = controller.items[index]; return ProductItemWidget( product, imgHeight: 117.w, ); }, gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, mainAxisSpacing: AppSpace.listRow, crossAxisSpacing: AppSpace.listItem, childAspectRatio: 0.8, ), ), ) .paddingHorizontal(AppSpace.listView); }, ); }
|
1 2 3 4 5 6 7 8 9
| Widget _buildView() { return <Widget>[ _buildLeftNav(), _buildProductList().expanded(), ].toRow(); }
|
注意需要用 expanded()
撑开右侧内容区
提交代码到 git