Flutter评分插件rating_bar的使用

Flutter评分插件rating_bar的使用

rating_bar 是一个支持半星评分的自定义 Flutter 评分栏。

安装

在项目的 pubspec.yaml 文件中添加以下依赖:

dependencies:
  ...
  rating_bar: ^最新版本号

使用

首先导入 rating_bar 包:

import 'package:rating_bar/rating_bar.dart';

基本用法

onRatingChanged 回调函数会在评分改变时返回当前评分(类型为 double)。

RatingBar(
  onRatingChanged: (rating) => setState(() => _rating = rating),
  filledIcon: Icons.star,
  emptyIcon: Icons.star_border,
  halfFilledIcon: Icons.star_half,
  isHalfAllowed: true,
  filledColor: Colors.green,
  emptyColor: Colors.redAccent,
  halfFilledColor: Colors.amberAccent, 
  size: 48,
),

只读评分栏

你可以使用只读的评分栏小部件。

RatingBar.readOnly(
  initialRating: 3.5,
  isHalfAllowed: true,
  halfFilledIcon: Icons.star_half,
  filledIcon: Icons.star,
  emptyIcon: Icons.star_border,
),

示例代码

以下是一个完整的示例代码,展示了如何在 Flutter 应用程序中使用 rating_bar 插件。

import 'package:flutter/material.dart';
import 'package:rating_bar/rating_bar.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHomePage(title: 'Rating Bar Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  double _ratingStar = 0;
  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: <Widget>[
          Text(
            'Rating : $_ratingStar',
            style: Theme.of(context).textTheme.subhead,
          ),
          SizedBox(height: 8),
          RatingBar(
            onRatingChanged: (rating) => setState(() => _ratingStar = rating),
            filledIcon: Icons.star,
            emptyIcon: Icons.star_border,
          ),
          SizedBox(height: 32),
          Text(
            'Read Only Rating : 3.5',
            style: Theme.of(context).textTheme.subhead,
          ),
          SizedBox(height: 8),
          RatingBar.readOnly(
            initialRating: 3.5,
            isHalfAllowed: true,
            halfFilledIcon: Icons.star_half,
            filledIcon: Icons.star,
            emptyIcon: Icons.star_border,
          ),
          SizedBox(height: 32),
          Text(
            'Rating : $_ratingStarLong',
            style: Theme.of(context).textTheme.subhead,
          ),
          SizedBox(height: 8),
          RatingBar(
            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),
          RatingBar(
            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的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


rating_bar 是一个用于 Flutter 的评分组件,它允许用户通过点击星星或其他图标来选择一个评分值。以下是如何在 Flutter 项目中使用 rating_bar 插件的步骤。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  rating_bar: ^1.0.0  # 请使用最新版本

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

2. 导入包

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

import 'package:rating_bar/rating_bar.dart';

3. 使用 RatingBar

RatingBar 组件可以通过多种方式进行自定义。以下是一个简单的示例:

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Rating Bar Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RatingBar(
              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: 24),
            ),
          ],
        ),
      ),
    );
  }
}

4. 参数说明

  • initialRating: 初始评分值。
  • minRating: 最小评分值,默认为 0。
  • direction: 评分条的方向,可以是 Axis.horizontalAxis.vertical
  • allowHalfRating: 是否允许选择半颗星,默认为 false
  • itemCount: 评分条中图标的总数,默认为 5。
  • itemPadding: 图标之间的间距。
  • itemBuilder: 用于构建每个图标的回调函数。通常返回一个 IconImage 组件。
  • onRatingUpdate: 当评分值更新时调用的回调函数。

5. 自定义图标

你可以通过 itemBuilder 自定义评分图标。例如,使用不同的图标或颜色:

itemBuilder: (context, _) => Icon(
  Icons.favorite,
  color: Colors.red,
),

6. 处理评分值

onRatingUpdate 回调函数会在用户选择评分值时触发。你可以在这个回调中更新状态或执行其他操作。

onRatingUpdate: (rating) {
  setState(() {
    _rating = rating;
  });
  print('Selected rating: $rating');
},

7. 完整示例

import 'package:flutter/material.dart';
import 'package:rating_bar/rating_bar.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Rating Bar Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Rating Bar Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RatingBar(
              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: 24),
            ),
          ],
        ),
      ),
    );
  }
}
回到顶部