14.7 过滤面板 - 打分评级
实现步骤:
第 1 步:星级列表组件
lib/common/components/stars_list.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 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
| 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, ); } }
|
第 2 步:控制器
lib/pages/search/search_filter/controller.dart
1 2 3 4 5
| void onStarTap(int value) { starValue = value; update(["filter_stars"]); }
|
第 3 步:FilterView 视图
lib/pages/search/search_filter/widgets/filter_view.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Widget _buildStars() { return GetBuilder<SearchFilterController>( id: "filter_stars", builder: ( _) { return StarsListWidget( value: controller.starValue, onTap: controller.onStarTap, selectedColor: AppColors.highlight, size: 18, ).paddingBottom(AppSpace.listRow * 2); }, ); }
|
1 2 3 4 5 6
| Widget _buildView() { return <Widget>[ ... _buildTitle(LocaleKeys.searchFilterReview.tr), _buildStars(),
|
提交代码到 git