Flutter动态效果按钮插件wormy_effect_button的使用

Flutter动态效果按钮插件wormy_effect_button的使用

drawing

pub package pub points popularity

平台支持

Android iOS MacOS Web Linux Windows
✔️ ✔️ ✔️ ✔️ ✔️ ✔️

特性

该插件帮助你创建具有蠕动效果的可拖动小部件。

示例

开始使用

你需要确保在你的Flutter项目中添加依赖项。

dependencies:
  wormy_effect_button: "^0.0.4"

使用方法

以下是一个简单的示例,展示了如何使用WormyEffectButton插件:

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

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

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  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: [
            const Text(
              '你已经点击了按钮多少次:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: WormyEffectButton(
        onPressed: _incrementCounter, // 点击事件函数
        initialOffset: Offset(MediaQuery.of(context).size.width - 60, // 按钮的初始位置
            MediaQuery.of(context).size.height - 60),
        holdPosition: false, // 按钮是否保持在当前位置
        hideUnderlying: true, // 是否隐藏底层控件
        curve: Curves.fastLinearToSlowEaseIn, // 动画曲线
        motionDelay: 250, // 动画延迟时间
        children: [
          Container(
            width: 50,
            height: 50,
            child: const Icon(Icons.camera),
            decoration:
                const BoxDecoration(color: Colors.blue, shape: BoxShape.circle),
          ),
          Container(
            width: 50,
            height: 50,
            child: const Icon(Icons.camera),
            decoration:
                const BoxDecoration(color: Colors.red, shape: BoxShape.circle),
          ),
          Container(
            width: 50,
            height: 50,
            child: const Icon(Icons.camera),
            decoration: const BoxDecoration(
                color: Colors.orange, shape: BoxShape.circle),
          ),
          Container(
            width: 50,
            height: 50,
            child: const Icon(Icons.camera),
            decoration: const BoxDecoration(
                color: Colors.green, shape: BoxShape.circle),
          ),
          Container(
            width: 50,
            height: 50,
            child: const Icon(Icons.camera),
            decoration: const BoxDecoration(
                color: Colors.purple, shape: BoxShape.circle),
          ),
        ],
      ),
    );
  }
}

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

1 回复

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


wormy_effect_button 是一个在 Flutter 中实现动态按钮效果的插件。它通常用于创建具有动画效果的按钮,比如当用户按下按钮时,按钮会有一个类似“蠕虫”收缩和扩展的动画效果。这个插件可以增强用户交互体验,让按钮看起来更加生动和有趣。

安装 wormy_effect_button

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

dependencies:
  flutter:
    sdk: flutter
  wormy_effect_button: ^1.0.0  # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

基本使用

以下是一个简单的示例,展示如何使用 wormy_effect_button

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Wormy Effect Button Example'),
        ),
        body: Center(
          child: WormyEffectButton(
            onPressed: () {
              print('Button Pressed!');
            },
            child: Text(
              'Press Me',
              style: TextStyle(color: Colors.white),
            ),
            buttonColor: Colors.blue,
          ),
        ),
      ),
    );
  }
}

参数说明

  • onPressed: 当按钮被按下时触发的回调函数。
  • child: 按钮内部的内容,通常是 TextIcon
  • buttonColor: 按钮的背景颜色。
  • duration: 动画的持续时间,默认为 500ms
  • scale: 按钮缩放的比例,默认为 0.95

自定义动画效果

你可以通过调整 durationscale 参数来自定义动画效果。例如,增加 duration 可以让动画更慢,减少 scale 可以让按钮收缩得更小。

WormyEffectButton(
  onPressed: () {
    print('Button Pressed!');
  },
  child: Text(
    'Press Me',
    style: TextStyle(color: Colors.white),
  ),
  buttonColor: Colors.blue,
  duration: Duration(milliseconds: 1000), // 动画持续1秒
  scale: 0.8, // 按钮缩小到80%
);
回到顶部