Flutter自定义星级评分插件custom_star_rating的使用
Flutter自定义星级评分插件custom_star_rating的使用
StarRating
一个用于 Flutter 的可定制星级评分组件。
特性
- 可定制的星星数量
- 可调整的星星大小和颜色
- 分数星级(半星支持)
- 自定义星星图标
- 只读模式
- 平滑过渡和动画
- 辅助功能支持
安装
在 pubspec.yaml
文件中添加 star_rating
作为依赖项以使用此包。
开始使用
首先,确保你已经在项目中引入了 star_rating
包。
使用
导入包
import 'package:star_rating/custom_star_rating.dart';
示例
以下是一个简单的示例,帮助你快速上手:
import 'package:flutter/material.dart';
import 'package:star_rating/custom_star_rating.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('星级评分示例'),
),
body: Center(
child: StarRating(
rating: 3,
starColor: Colors.amber,
size: 40.0,
mainAxisAlignment: MainAxisAlignment.center,
onTap: (rating) {
print("选择的评分: $rating");
},
),
),
),
);
}
}
更多关于Flutter自定义星级评分插件custom_star_rating的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter自定义星级评分插件custom_star_rating的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中,如果你想要实现一个自定义的星级评分功能,你可以使用现有的插件,或者自己创建一个自定义的 CustomStarRating
小部件。下面是一个简单的示例,展示如何创建一个自定义的星级评分小部件。
自定义星级评分小部件示例
import 'package:flutter/material.dart';
class CustomStarRating extends StatefulWidget {
final int maxStars;
final int initialRating;
final double starSize;
final Color filledColor;
final Color unfilledColor;
final ValueChanged<int> onRatingChanged;
CustomStarRating({
Key? key,
this.maxStars = 5,
this.initialRating = 0,
this.starSize = 24.0,
this.filledColor = Colors.amber,
this.unfilledColor = Colors.grey,
required this.onRatingChanged,
}) : super(key: key);
@override
_CustomStarRatingState createState() => _CustomStarRatingState();
}
class _CustomStarRatingState extends State<CustomStarRating> {
int _currentRating = 0;
@override
void initState() {
super.initState();
_currentRating = widget.initialRating;
}
void _updateRating(int rating) {
setState(() {
_currentRating = rating;
});
widget.onRatingChanged(rating);
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: List.generate(widget.maxStars, (index) {
return GestureDetector(
onTap: () => _updateRating(index + 1),
child: Icon(
index < _currentRating ? Icons.star : Icons.star_border,
size: widget.starSize,
color: index < _currentRating ? widget.filledColor : widget.unfilledColor,
),
);
}),
);
}
}
使用 CustomStarRating
小部件
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Custom Star Rating Example')),
body: Center(
child: CustomStarRating(
initialRating: 3,
onRatingChanged: (rating) {
print('Selected rating: $rating');
},
),
),
),
);
}
}
参数说明
maxStars
:最大星数,默认为5。initialRating
:初始评分,默认为0。starSize
:星星的大小,默认为24.0。filledColor
:填充星星的颜色,默认为Colors.amber
。unfilledColor
:未填充星星的颜色,默认为Colors.grey
。onRatingChanged
:当用户选择评分时的回调函数。
运行效果
当用户点击星星时,CustomStarRating
会根据点击的星星数量更新评分,并通过 onRatingChanged
回调函数返回当前的评分值。
自定义风格
你可以通过修改 filledColor
、unfilledColor
和 starSize
来调整星星的外观,以满足你的设计需求。
其他插件
如果你不想自己实现,也可以使用现有的插件,例如 flutter_rating_bar
,它提供了丰富的功能和自定义选项。你可以通过 pubspec.yaml
文件添加依赖:
dependencies:
flutter_rating_bar: ^4.0.0
然后在代码中使用:
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
RatingBar.builder(
initialRating: 3,
minRating: 1,
direction: Axis.horizontal,
allowHalfRating: true,
itemCount: 5,
itemPadding: EdgeInsets.symmetric(horizontal: 4.0),
itemBuilder: (context, _) => Icon(
Icons.star,
color: Colors.amber,
),
onRatingUpdate: (rating) {
print(rating);
},
);