Flutter评分插件awesome_rating的使用
Flutter评分插件awesome_rating的使用
awesome_rating
是一个用于 Flutter 的简单评分组件,支持完全自定义,并且可以显示任何分数的评分。
功能
- 可以设置最小和最大评分
- 支持任何小部件作为评分条/指示器项
- 在同一个评分条中可以根据位置使用不同的小部件
AwesomeStarRating(
starCount: 5,
rating: 5.0,
size: 30.0,
color: Colors.orange,
borderColor: Colors.orange,
),
AwesomeStarRating(
starCount: 5,
rating: 5.0,
size: 30.0,
onRatingChanged: (double value) {
setState(() {
rating = value;
});
},
color: Colors.orange,
borderColor: Colors.orange,
),
完整示例
以下是一个完整的 Flutter 应用程序示例,展示了如何使用 awesome_rating
插件。
import 'package:flutter/material.dart';
import 'package:awesome_rating/awesome_rating.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Awesome rating',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Awesome rating Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
_DemoItem2(),
SizedBox(
height: 24.0,
),
_DemoItem1(),
SizedBox(
height: 24.0,
),
_DemoItem3(),
],
),
),
);
}
}
class _DemoItem1 extends StatelessWidget {
const _DemoItem1({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AwesomeStarRating(
starCount: 5,
rating: 1.0,
size: 30.0,
color: Colors.orange,
borderColor: Colors.orange,
),
const SizedBox(width: 8.0),
Text(
'1 reviews',
style: Theme.of(context).textTheme.bodyText1,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AwesomeStarRating(
starCount: 5,
rating: 2.0,
size: 30.0,
color: Colors.orange,
borderColor: Colors.orange,
),
const SizedBox(width: 8.0),
Text(
'2 reviews',
style: Theme.of(context).textTheme.bodyText1,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AwesomeStarRating(
starCount: 5,
rating: 3.0,
size: 30.0,
color: Colors.orange,
borderColor: Colors.orange,
),
const SizedBox(width: 8.0),
Text(
'3 reviews',
style: Theme.of(context).textTheme.bodyText1,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AwesomeStarRating(
starCount: 5,
rating: 4.0,
size: 30.0,
color: Colors.orange,
borderColor: Colors.orange,
),
const SizedBox(width: 8.0),
Text(
'4 reviews',
style: Theme.of(context).textTheme.bodyText1,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AwesomeStarRating(
starCount: 5,
rating: 5.0,
size: 30.0,
color: Colors.orange,
borderColor: Colors.orange,
),
const SizedBox(width: 8.0),
Text(
'5 reviews',
style: Theme.of(context).textTheme.bodyText1,
),
],
),
],
);
}
}
class _DemoItem2 extends StatefulWidget {
const _DemoItem2({Key? key}) : super(key: key);
[@override](/user/override)
State<_DemoItem2> createState() => _DemoItem2State();
}
class _DemoItem2State extends State<_DemoItem2> {
double rating = 4.0;
[@override](/user/override)
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AwesomeStarRating(
starCount: 5,
rating: rating,
size: 30.0,
color: Colors.orange,
onRatingChanged: (double value) {
setState(() {
rating = value;
});
},
borderColor: Colors.orange,
),
const SizedBox(height: 8.0),
Text(
'tap to update rating',
style: Theme.of(context).textTheme.bodyText1,
),
],
);
}
}
class _DemoItem3 extends StatelessWidget {
const _DemoItem3({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 8.0),
Text(
'Custom rating widget',
style: Theme.of(context).textTheme.subtitle1,
),
const SizedBox(height: 8.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AwesomeStarRating(
starCount: 5,
rating: 1.0,
size: 30.0,
customRatingWidget: RatingWidget(
full: Icon(Icons.star_outlined, size: 30.0,),
half: Icon(Icons.star_half_outlined, size: 30.0,),
empty: Icon(Icons.star_border_purple500_outlined, size: 30.0,),
),
color: Colors.orange,
borderColor: Colors.orange,
),
const SizedBox(width: 8.0),
Text(
'1 reviews',
style: Theme.of(context).textTheme.bodyText1,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AwesomeStarRating(
starCount: 5,
rating: 2.0,
size: 30.0,
customRatingWidget: RatingWidget(
full: Icon(Icons.star_outlined, size: 30.0,),
half: Icon(Icons.star_half_outlined, size: 30.0,),
empty: Icon(Icons.star_border_purple500_outlined, size: 30.0,),
),
color: Colors.orange,
borderColor: Colors.orange,
),
const SizedBox(width: 8.0),
Text(
'2 reviews',
style: Theme.of(context).textTheme.bodyText1,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AwesomeStarRating(
starCount: 5,
rating: 3.0,
size: 30.0,
customRatingWidget: RatingWidget(
full: Icon(Icons.star_outlined, size: 30.0,),
half: Icon(Icons.star_half_outlined, size: 30.0,),
empty: Icon(Icons.star_border_purple500_outlined, size: 30.0,),
),
color: Colors.orange,
borderColor: Colors.orange,
),
const SizedBox(width: 8.0),
Text(
'3 reviews',
style: Theme.of(context).textTheme.bodyText1,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AwesomeStarRating(
starCount: 5,
rating: 4.0,
size: 30.0,
customRatingWidget: RatingWidget(
full: Icon(Icons.star_outlined, size: 30.0,),
half: Icon(Icons.star_half_outlined, size: 30.0,),
empty: Icon(Icons.star_border_purple500_outlined, size: 30.0,),
),
color: Colors.orange,
borderColor: Colors.orange,
),
const SizedBox(width: 8.0),
Text(
'4 reviews',
style: Theme.of(context).textTheme.bodyText1,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const AwesomeStarRating(
starCount: 5,
rating: 5.0,
size: 30.0,
customRatingWidget: RatingWidget(
full: Icon(Icons.star_outlined, size: 30.0,),
half: Icon(Icons.star_half_outlined, size: 30.0,),
empty: Icon(Icons.star_border_purple500_outlined, size: 30.0,),
),
color: Colors.orange,
borderColor: Colors.orange,
),
const SizedBox(width: 8.0),
Text(
'5 reviews',
style: Theme.of(context).textTheme.bodyText1,
),
],
),
],
);
}
}
更多关于Flutter评分插件awesome_rating的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter评分插件awesome_rating的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用awesome_rating
插件的一个示例代码案例。这个插件允许用户通过简单的点击或滑动来给出评分。
步骤 1: 添加依赖
首先,在你的pubspec.yaml
文件中添加awesome_rating
依赖:
dependencies:
flutter:
sdk: flutter
awesome_rating: ^0.0.6 # 请注意版本号,使用最新版本
然后运行flutter pub get
来安装依赖。
步骤 2: 导入包
在你的Dart文件中导入awesome_rating
包:
import 'package:awesome_rating/awesome_rating.dart';
import 'package:flutter/material.dart';
步骤 3: 使用AwesomeRating
下面是一个完整的示例,展示了如何在Flutter应用中使用AwesomeRating
组件:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _rating = 0;
void _onRatingChanged(int rating) {
setState(() {
_rating = rating;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Awesome Rating Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AwesomeRating(
rating: _rating,
allowHalfRating: true, // 是否允许半星评分
minRating: 1, // 最小评分
maxRating: 5, // 最大评分
size: 30, // 星形图标的大小
color: Colors.amber, // 未选中星形的颜色
borderColor: Colors.grey, // 星形边框的颜色
borderWidth: 2.0, // 星形边框的宽度
spacing: 5.0, // 星形之间的间距
listener: (rating, gesture) {
// 当评分改变时调用的回调函数
_onRatingChanged(rating);
},
),
SizedBox(height: 20),
Text('Your Rating: $_rating'),
],
),
),
);
}
}
解释
- 依赖添加:在
pubspec.yaml
中添加awesome_rating
依赖。 - 导入包:在Dart文件中导入
awesome_rating
包。 - 使用组件:
AwesomeRating
组件接受多个参数,包括当前评分(rating
)、是否允许半星评分(allowHalfRating
)、最小和最大评分(minRating
和maxRating
)、星形图标的大小(size
)、颜色(color
)、边框颜色(borderColor
)和宽度(borderWidth
)、星形之间的间距(spacing
)以及一个监听器(listener
)。- 当评分改变时,
listener
回调被触发,你可以在这里更新UI或执行其他逻辑。
以上代码展示了如何在Flutter应用中使用awesome_rating
插件来收集用户的评分。你可以根据需要调整参数和样式。