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 回复

更多关于Flutter评分条插件rating_bar_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


rating_bar_flutter 是一个用于在 Flutter 应用中创建评分条的插件。它允许你自定义评分条的外观和行为,例如星星的数量、颜色、大小等。以下是如何在 Flutter 项目中使用 rating_bar_flutter 插件的步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 rating_bar_flutter 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  rating_bar_flutter: ^1.0.0  # 请根据最新版本号进行更新

然后运行 flutter pub get 来获取依赖。

2. 导入包

在你的 Dart 文件中导入 rating_bar_flutter 包:

import 'package:rating_bar_flutter/rating_bar_flutter.dart';

3. 使用 RatingBar.builder

rating_bar_flutter 提供了 RatingBar.builder 来创建评分条。你可以通过以下方式使用它:

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  double _rating = 0.0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Rating Bar Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RatingBar.builder(
              initialRating: _rating,
              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) {
                setState(() {
                  _rating = rating;
                });
              },
            ),
            SizedBox(height: 20),
            Text(
              'Rating: $_rating',
              style: TextStyle(fontSize: 20),
            ),
          ],
        ),
      ),
    );
  }
}

4. 参数说明

  • initialRating: 初始评分值。
  • minRating: 最小评分值,默认为 0。
  • direction: 评分条的方向,可以是 Axis.horizontalAxis.vertical
  • allowHalfRating: 是否允许半星评分。
  • itemCount: 评分条中星星的数量。
  • itemPadding: 星星之间的间距。
  • itemBuilder: 用于构建每个星星的 Widget。
  • onRatingUpdate: 当评分值更新时的回调函数。

5. 运行项目

现在你可以运行你的 Flutter 项目,并查看评分条的效果。用户可以通过点击星星来设置评分,评分值会实时更新并显示在屏幕上。

6. 自定义

你可以根据需要自定义评分条的外观,例如更改星星的颜色、大小、形状等。只需在 itemBuilder 中返回你想要的 Widget 即可。

itemBuilder: (context, _) => Icon(
  Icons.favorite,
  color: Colors.red,
  size: 40.0,
),
回到顶部