12.12 下拉刷新数据
实现步骤:
第 1 步:控制器
lib/pages/goods/product_details/controller.dart
| 12
 3
 4
 
 | final RefreshController mainRefreshController = RefreshController(
 initialRefresh: true,
 );
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | void onMainRefresh() async {
 try {
 
 await _loadProduct();
 
 mainRefreshController.refreshCompleted();
 } catch (error) {
 
 mainRefreshController.refreshFailed();
 }
 update(["product_details"]);
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | @overridevoid onClose() {
 super.onClose();
 
 tabController.dispose();
 
 mainRefreshController.dispose();
 
 reviewsRefreshController.dispose();
 }
 
 | 
第 2 步:视图
lib/pages/goods/product_details/view.dart
| 12
 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
 
 | @overrideWidget build(BuildContext context) {
 return GetBuilder<ProductDetailsController>(
 init: ProductDetailsController(),
 id: "product_details",
 
 tag: tag,
 builder: (_) {
 return Scaffold(
 
 appBar: mainAppBarWidget(titleString: LocaleKeys.gDetailTitle.tr),
 
 body: SafeArea(
 child: <Widget>[
 
 SmartRefresher(
 controller: controller.mainRefreshController,
 onRefresh: controller.onMainRefresh,
 child: _buildView(),
 ),
 
 _buildButtons().positioned(
 bottom: 10,
 left: 0,
 right: 0,
 ),
 ].toStack(),
 ),
 );
 },
 );
 }
 
 | 
第 3 步:评论星标组件
 
lib/common/components/stars_list.dart
| 12
 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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 
 | import 'package:flutter/material.dart';
 import '../index.dart';
 
 
 class StarsListWidget extends StatelessWidget {
 
 final Function(int value)? onTap;
 
 
 final IconData? iconData;
 
 
 final int starNum;
 
 
 final int value;
 
 
 final double? size;
 
 
 final Color? color;
 
 
 final Color? selectedColor;
 
 
 final double? spacing;
 
 
 final double? runSpacing;
 
 
 StarsListWidget({
 Key? key,
 this.onTap,
 this.size = 24,
 this.iconData,
 Color? color,
 Color? selectedColor,
 this.spacing = 5,
 this.runSpacing = 5,
 this.starNum = 5,
 this.value = 0,
 })  : color = color ?? AppColors.surfaceVariant,
 selectedColor = selectedColor ?? AppColors.primary,
 super(key: key);
 
 @override
 Widget build(BuildContext context) {
 return <Widget>[
 for (var i = 1; i <= starNum; i++)
 IconWidget.icon(
 iconData ?? Icons.star,
 size: size ?? 12,
 color: i <= value ? selectedColor : color,
 ).onTap(() {
 if (value == 1 && i == value) {
 onTap?.call(0);
 } else {
 onTap?.call(i);
 }
 }),
 ].toWrap(
 spacing: spacing ?? AppSpace.listItem,
 runSpacing: runSpacing ?? AppSpace.listRow,
 );
 }
 }
 
 
 | 
lib/pages/goods/product_details/widgets/tab_reviews.dart
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | _buildListItem(ReviewModel item) {
 return <Widget>[
 ...
 
 
 <Widget>[
 
 StarsListWidget(
 value: item.rating ?? 0,
 size: 12,
 ),
 
 | 
提交代码到 git