Flutter应用评分提示插件givestarreviews的使用

Flutter应用评分提示插件givestarreviews的使用

简介

give-star-reviews 是一个用于给事物评分的 Flutter 包。

开始使用

首先,在 pubspec.yaml 文件中添加依赖项:

dependencies:
  givestarreviews: ^1.0.2

然后运行以下命令来获取依赖项:

$ flutter pub get

接下来,在 Dart 文件中导入包:

import 'package:givestarreviews/givestarreviews.dart';

示例

多个评分

你可以通过 GiveStarReviews 组件创建多个评分。每个评分可以有不同的文本描述和不同的回调函数。

GiveStarReviews(
  starData: [
    GiveStarData(text: 'Review 1', onChanged: (rate) {}),
    GiveStarData(text: 'Review 2', onChanged: null),
    GiveStarData(text: 'Review 3', starCount: 3, onChanged: (rate) {}),
  ],
)

单个评分

你可以通过 StarRating 组件创建单个评分。如果希望只读(不可更改),则将 onChanged 设置为 null 或留空。

StarRating(onChanged: (rate) {}),

只读评分

要使 StarRating 成为只读,只需将 onChanged 设置为 null 或留空。

Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    Text('Read only'),
    StarRating(value: 2),
  ],
)

自定义星数

你也可以自定义星星的数量,例如设置为7颗星。

Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    Text('7 Stars'),
    StarRating(
      value: 2,
      starCount: 7,
      onChanged: (rate) {
        _showToast(context, rate.toString());
      },
    ),
  ],
)

完整示例代码

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Give Star Reviews Example',
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Builder(
          builder: (context) => Container(
            padding: EdgeInsets.all(16),
            child: Column(
              children: <Widget>[
                StarRating(onChanged: (rate) {
                  _showToast(context, rate.toString());
                }),
                Divider(height: 20),
                GiveStarReviews(
                  starData: [
                    GiveStarData(
                      text: 'Review 1',
                      onChanged: (rate) {
                        _showToast(context, rate.toString());
                      },
                    ),
                    GiveStarData(
                      text: 'Review 2',
                      onChanged: (rate) {
                        _showToast(context, rate.toString());
                      },
                    ),
                    GiveStarData(
                      text: 'Review 3',
                      onChanged: (rate) {
                        _showToast(context, rate.toString());
                      },
                    ),
                  ],
                ),
                SizedBox(height: 24),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('Read only'),
                    StarRating(value: 2),
                  ],
                ),
                SizedBox(height: 24),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('7 Stars'),
                    StarRating(
                      value: 2,
                      starCount: 7,
                      onChanged: (rate) {
                        _showToast(context, rate.toString());
                      },
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _showToast(BuildContext context, String txt) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        duration: Duration(milliseconds: 500),
        content: Text(txt),
      ),
    );
  }
}

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

1 回复

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


givestarreviews 是一个用于在 Flutter 应用中提示用户进行评分的插件。它可以帮助你轻松地集成一个评分提示对话框,鼓励用户为你的应用评分。以下是如何使用这个插件的基本步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  givestarreviews: ^1.0.0  # 请确保使用最新版本

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

2. 导入插件

在你的 Dart 文件中导入 givestarreviews 插件:

import 'package:givestarreviews/givestarreviews.dart';

3. 使用插件

你可以在应用的任何地方调用 GiveStarReviewsshowStarReview 方法来显示评分提示。通常,你会在用户完成某个重要操作或在应用中使用了一段时间后调用它。

GiveStarReviews.showStarReview(
  context: context,
  appPackageName: 'com.example.yourapp', // 替换为你的应用包名
  onButtonTap: (bool isSubmitted) {
    // 在这里处理用户点击按钮后的逻辑
    if (isSubmitted) {
      print('用户提交了评分');
    } else {
      print('用户取消评分');
    }
  },
);

4. 配置参数

showStarReview 方法接受多个参数,你可以根据需要自定义对话框的行为和外观:

  • context: 当前的 BuildContext,用于显示对话框。
  • appPackageName: 你应用的包名,用于打开应用商店。
  • onButtonTap: 用户点击按钮后的回调函数,返回一个布尔值表示是否提交了评分。
  • dialogTitle: 对话框的标题(可选)。
  • dialogMessage: 对话框的提示信息(可选)。
  • positiveButtonText: 正面按钮的文本(可选)。
  • negativeButtonText: 负面按钮的文本(可选)。
  • neutralButtonText: 中性按钮的文本(可选)。

5. 示例代码

以下是一个完整的示例代码,展示如何在应用中使用 givestarreviews 插件:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Give Star Reviews Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              GiveStarReviews.showStarReview(
                context: context,
                appPackageName: 'com.example.yourapp',
                onButtonTap: (bool isSubmitted) {
                  if (isSubmitted) {
                    print('用户提交了评分');
                  } else {
                    print('用户取消评分');
                  }
                },
              );
            },
            child: Text('Show Rating Dialog'),
          ),
        ),
      ),
    );
  }
}
回到顶部