Flutter评分滑动插件rating_bar_swipe的使用

Flutter评分滑动插件rating_bar_swipe的使用

特性

First Way

Second Way

开始使用

要开始使用 rating_bar_swipe 插件,请确保在你的 pubspec.yaml 文件中添加依赖项:

dependencies:
  flutter:
    sdk: flutter
  rating_bar_swipe: ^版本号

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

使用示例

基本用法

你可以通过以下方式来使用 Rating 小部件:

Rating(
  functionUpdateRating: (value) {},
  iterativeValue: 0.1,
  initialRating: 0,
  isVertical: Axis.horizontal,
  itemSize: 40,
);

完整示例代码

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

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '评分滑动插件演示',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              '你已经点击了按钮次数:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.pushReplacement(
            context,
            MaterialPageRoute(
              builder: (BuildContext context) {
                return Rating(
                  functionUpdateRating: (value) {
                    print('当前评分: $value');
                  },
                  iterativeValue: 0.1,
                  initialRating: 0,
                  isVertical: Axis.horizontal,
                  itemSize: 40,
                );
              },
            ),
          );
        },
        tooltip: '增加计数',
        child: const Icon(Icons.add),
      ),
    );
  }
}

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

1 回复

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


rating_bar_swipe 是一个用于在 Flutter 应用中实现评分滑动功能的插件。它允许用户通过滑动来选择一个评分值。以下是使用该插件的基本步骤和示例代码。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  rating_bar_swipe: ^1.1.2  # 请使用最新版本

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

2. 使用 RatingBarSwipe 组件

在你的 Flutter 应用中,你可以使用 RatingBarSwipe 组件来实现滑动评分功能。

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

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

class _RatingPageState extends State<RatingPage> {
  double _rating = 0.0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Rating Bar Swipe Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '请滑动评分',
              style: TextStyle(fontSize: 20),
            ),
            SizedBox(height: 20),
            RatingBarSwipe(
              initialRating: _rating,
              onRatingStart: (rating) {
                setState(() {
                  _rating = rating;
                });
              },
              onRatingUpdate: (rating) {
                setState(() {
                  _rating = rating;
                });
              },
              onRatingEnd: (rating) {
                setState(() {
                  _rating = rating;
                });
                print('最终评分: $rating');
              },
              activeColor: Colors.amber,
              inactiveColor: Colors.grey,
              size: 40,
              itemPadding: EdgeInsets.symmetric(horizontal: 4.0),
              allowHalfRating: true,
            ),
            SizedBox(height: 20),
            Text(
              '当前评分: $_rating',
              style: TextStyle(fontSize: 24),
            ),
          ],
        ),
      ),
    );
  }
}

void main() => runApp(MaterialApp(
  home: RatingPage(),
));
回到顶部