ratingWidget static method
通用的评分组件
Implementation
static Widget ratingWidget({
required double rating, // 传入评分
double iconSize = 16,
Color color = Colors.amber,
TextStyle? textStyle,
}) {
int fullStars = rating.floor(); // 满星
bool hasHalfStar = (rating - fullStars) >= 0.5; // 半星
int emptyStars = 5 - fullStars - (hasHalfStar ? 1 : 0); // 空星
return Row(
mainAxisSize: MainAxisSize.min,
children: [
// 满星
...List.generate(
fullStars,
(index) => Icon(Icons.star, size: iconSize, color: color),
),
// 半星
if (hasHalfStar) Icon(Icons.star_half, size: iconSize, color: color),
// 空星
...List.generate(
emptyStars,
(index) => Icon(Icons.star_border, size: iconSize, color: color),
),
const SizedBox(width: 4),
Text(
rating.toStringAsFixed(1),
style:
textStyle ?? const TextStyle(fontSize: 13, color: Colors.black87),
),
],
);
}