Flutter评分条插件rating_bar_flutter的使用
Flutter评分条插件rating_bar_flutter的使用
安装
首先,在项目的pubspec.yaml
文件中添加以下依赖:
dependencies:
...
rating_bar_flutter: ^1.0.0
然后运行flutter pub get
来获取该插件。
使用
首先导入rating_bar_flutter
包:
import 'package:rating_bar_flutter/rating_bar_flutter.dart';
接下来,可以创建一个可交互的评分条:
class _MyHomePageState extends State<MyHomePage> {
double _ratingStar = 0;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 显示当前评分
Text(
'Rating : $_ratingStar',
style: Theme.of(context).textTheme.subhead,
),
SizedBox(height: 8),
// 可交互的评分条
RatingBarFlutter(
onRatingChanged: (rating) => setState(() => _ratingStar = rating!),
filledIcon: Icons.star,
emptyIcon: Icons.star_border,
),
],
),
);
}
}
你还可以使用只读模式的评分条:
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 显示只读评分
Text(
'Read Only Rating : 3.5',
style: Theme.of(context).textTheme.subhead,
),
SizedBox(height: 8),
// 只读评分条
RatingBarFlutter.readOnly(
initialRating: 3.5,
isHalfAllowed: true,
halfFilledIcon: Icons.star_half,
filledIcon: Icons.star,
emptyIcon: Icons.star_border,
),
],
),
);
}
}
此外,你可以自定义评分条的最大评分、颜色等属性:
class _MyHomePageState extends State<MyHomePage> {
double _ratingStarLong = 0;
double _ratingSmile = 0;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 显示长评分
Text(
'Rating : $_ratingStarLong',
style: Theme.of(context).textTheme.subhead,
),
SizedBox(height: 8),
// 自定义最大评分为10的评分条
RatingBarFlutter(
maxRating: 10,
onRatingChanged: (rating) => setState(() => _ratingStarLong = rating!),
filledIcon: Icons.star,
emptyIcon: Icons.star_border,
halfFilledIcon: Icons.star_half,
isHalfAllowed: true,
filledColor: Colors.amber,
size: 36,
),
SizedBox(height: 32),
// 使用表情作为评分图标
Text(
'Rating : $_ratingSmile',
style: Theme.of(context).textTheme.subhead,
),
SizedBox(height: 8),
RatingBarFlutter(
onRatingChanged: (rating) => setState(() => _ratingSmile = rating!),
filledIcon: Icons.sentiment_satisfied,
emptyIcon: Icons.sentiment_dissatisfied,
halfFilledIcon: Icons.sentiment_neutral,
isHalfAllowed: true,
filledColor: Colors.green,
emptyColor: Colors.redAccent,
halfFilledColor: Colors.amberAccent,
size: 48,
),
],
),
);
}
}
更多关于Flutter评分条插件rating_bar_flutter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复