Flutter动画按钮插件animated_random_button的使用

Flutter动画按钮插件animated_random_button的使用

Flutter Version License

Animated Random Button Flutter 插件提供了一组四个带有动画效果的按钮,用于生成不同场景下的随机值。这些按钮包括:

  1. 硬币按钮:按下时会随机输出“奇数”或“偶数”。
  2. 骰子按钮:默认情况下按下时会输出1到6之间的随机值,但范围可以更改。
  3. 转盘按钮:按下时会输出一个随机颜色。
  4. 魔法球按钮:按下时会像经典的魔法球玩具一样输出随机答案。

安装

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

dependencies:
  animated_random_button: ^1.0.2

然后在终端运行 flutter pub get 来安装包。

使用

在 Dart 文件中导入该包:

import 'package:animated_random_button/animated_random_button.dart';

硬币按钮

硬币按钮可以按如下方式使用:

CoinButton(
  onPressed: () {},
  radius: 100,
  coin: Coins.Cent,
  duration: const Duration(seconds: 2),
),

你也可以添加自定义硬币:

class newCoins extends Coins {

  static const myFancyCoin = Coins(Coin('assets/FancyCoinHead.png','assets/FancyCoinTail.png'));

  newCoins(super.value);
}

骰子按钮

骰子按钮可以按如下方式使用:

BouncingDiceButton(
  start: 1, // 默认值
  end: 6, // 默认值
  duration: Duration(milliseconds: 500),
  onPressed: () {
    print("hello");
  },
),

转盘按钮

转盘按钮可以按如下方式使用:

RandomColorWheel(
  colors: const [
    Colors.black,
    Colors.amber,
    Colors.black12,
    Color(0xFF9D53CC),
  ],
  size: 100,
  onPressed: () {
    print("Wheel is spinning");
  },
  duration: const Duration(seconds: 1),
  waitForAnimation: false,
),

魔法球按钮

魔法球按钮可以按如下方式使用:

Magic8Ball(
  radius: 100,
  shakeDistance: 15,
  numberOfShakes: 10,
  durationOfShake: Duration(milliseconds: 100),
  answers: ["yes", "no"], // 可以使用默认答案
),

自定义

你可以更改动画的持续时间、调整按钮的大小和样本。

示例

要查看如何更全面地使用 Animated Random Button 包的示例,可以检查包中提供的 example 文件夹。

import 'package:animated_random_button/animated_random_button.dart';
import 'package:animated_random_button/coin_button.dart';
import 'package:animated_random_button/magic_sphere.dart';
import 'package:flutter/material.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(const MaterialApp(
    home: MyApp(),
  ));
}

class newCoins extends Coins {

  static const myFancyCoin = Coins(Coin('assets/FancyCoinHead.png','assets/FancyCoinTail.png'));

  newCoins(super.value);
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    final eurController = CoinController();
    final rubController = CoinController();
    final centController = CoinController();
    final fancyCoinController = CoinController();
    final colorController = RandomColorWheelController();
    final diceButtonController = DiceButtonController();
    final magicBallController = MagicBallController();
    return MaterialApp(
      title: '示例:随机小部件工具包',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('示例:随机小部件工具包'),
        ),
        body: Center(
            child: SingleChildScrollView(
          child: Column(
            children: [
              const SizedBox(
                height: 100,
              ),
              RandomColorWheel(
                controller: colorController,
                colors: const [
                  Colors.black,
                  Colors.amber,
                  Colors.black12,
                  Color(0xFF9D53CC),
                  Color(0xFF67508D),
                  Color(0xff141460),
                  Color.fromRGBO(10, 186, 181, 1),
                ],
                size: 100,
                onPressed: () {
                  print(colorController.color);
                  print("Wheel is spinning");
                },
                duration: const Duration(seconds: 1),
              ),
              const SizedBox(
                width: 30,
                height: 30,
              ),
              const SizedBox(
                height: 50,
              ),
              BouncingDiceButton(
                controller: diceButtonController,
                start: 1,
                end: 6,
                duration: const Duration(milliseconds: 2000),
                onPressed: () {
                  print(diceButtonController.value);
                  print("hello");
                },
              ),
              const SizedBox(
                height: 50,
              ),
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  CoinButton(radius: 100, coin: newCoins.myFancyCoin, coinController: fancyCoinController, onPressed: (){},),
                  const SizedBox(
                    width: 25,
                  ),
                  CoinButton(
                    onPressed: () {},
                    radius: 100,
                    coin: Coins.Euro,
                    duration: const Duration(seconds: 2),
                    coinController: eurController,
                  ),
                  const SizedBox(
                    width: 25,
                  ),
                  CoinButton(
                    onPressed: () {},
                    radius: 100,
                    coin: Coins.Ruble,
                    duration: const Duration(seconds: 2),
                    coinController: rubController,
                  ),
                  const SizedBox(
                    width: 25,
                  ),
                  CoinButton(
                    onPressed: () {},
                    radius: 100,
                    coin: Coins.Cent,
                    duration: const Duration(seconds: 2),
                    coinController: centController,
                  ),
                ],
              ),
              const SizedBox(
                height: 50,
              ),
              Magic8Ball(
                radius: 100,
                shakeDistance: 15,
                numberOfShakes: 10,
                durationOfShake: Duration(milliseconds: 100),
                answers: ["yes", "definitely yes"],
                controller: magicBallController,
                onPressed: (){print(magicBallController.answer);},
              ),
              const SizedBox(
                height: 50,
              ),
            ],
          ),
        )),
      ),
    );
  }
}

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

1 回复

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


当然,以下是如何在Flutter中使用animated_random_button插件的一个简单示例。animated_random_button插件允许你创建一个带有随机动画效果的按钮。以下是一个基本的实现案例:

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

dependencies:
  flutter:
    sdk: flutter
  animated_random_button: ^最新版本号  # 请替换为实际的最新版本号

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

接下来,你可以在你的Dart文件中使用AnimatedRandomButton。以下是一个完整的示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Animated Random Button Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Animated Random Button Demo'),
        ),
        body: Center(
          child: AnimatedRandomButtonExample(),
        ),
      ),
    );
  }
}

class AnimatedRandomButtonExample extends StatefulWidget {
  @override
  _AnimatedRandomButtonExampleState createState() => _AnimatedRandomButtonExampleState();
}

class _AnimatedRandomButtonExampleState extends State<AnimatedRandomButtonExample> {
  void _onButtonPressed() {
    // 在这里处理按钮点击事件
    print('Button pressed!');
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedRandomButton(
      // 设置按钮的宽度和高度
      width: 200,
      height: 60,
      // 按钮点击事件处理函数
      onPressed: _onButtonPressed,
      // 按钮文本
      child: Text(
        'Click Me',
        style: TextStyle(fontSize: 20, color: Colors.white),
      ),
      // 按钮的初始颜色
      color: Colors.blue,
      // 按钮颜色的动画类型,可以是 linear、easeInOutQuad 等
      animationType: AnimationType.easeInOutQuad,
      // 按钮颜色的动画持续时间
      animationDuration: Duration(milliseconds: 500),
      // 按钮颜色的随机变化范围
      colorRange: [
        Colors.red,
        Colors.green,
        Colors.blue,
      ],
      // 按钮边框半径
      borderRadius: BorderRadius.circular(15),
      // 按钮阴影
      elevation: 10,
      // 按钮文本的对齐方式
      textAlign: TextAlign.center,
      // 按钮动画完成后的颜色
      finalColor: Colors.blueAccent,
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个AnimatedRandomButton。按钮在被点击时会随机改变颜色,并且颜色的变化遵循我们设置的动画类型和持续时间。

注意:

  • AnimationType 是一个枚举,提供了不同的动画效果,如 linear, easeInOutQuad 等。
  • colorRange 是一个颜色列表,按钮的颜色将在这个列表中随机选择。
  • finalColor 是动画完成后的按钮颜色。

确保你使用的是最新版本的animated_random_button插件,因为API可能会随着版本更新而变化。

回到顶部