Flutter动画效果插件widget_animation的使用

Flutter动画效果插件widget_animation的使用

本README描述了该插件。如果您将此插件发布到pub.dev,则此README的内容将出现在您的插件页面上。

功能

  • 单个Widget展示动画
  • 多个Widget列表展示动画

开始使用

dependencies:
  widget_animation: ^0.0.2

使用方法

SingleWidgetAnimation(
  animationDuration: const Duration(seconds: 2),
  delayStart: const Duration(seconds: 6),
  child: child
);

完整示例

以下是使用widget_animation插件的完整示例:

import 'package:flutter/material.dart';
import 'package:widget_animation/widget_animation.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: 'Widget Animation 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> {

  final TextStyle style = const TextStyle(
    fontSize: 24,
    fontWeight: FontWeight.bold,
  );

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 显示动画文本
            SingleWidgetAnimation(
              child: Text(
                '显示动画',
                key: const Key("显示动画"),
                style: style,
              ),
            ),
            const SizedBox(height: 10),
            // 延迟显示动画文本
            SingleWidgetAnimation(
              delayStart: const Duration(seconds: 1),
              child: Text(
                '延迟显示动画',
                key: const Key("延迟显示动画"),
                style: style,
              ),
            ),
            const SizedBox(height: 10),
            // 水平方向动画文本
            SingleWidgetAnimation(
              direction: Direction.horizontal,
              child: Text(
                '水平方向动画',
                key: const Key("水平方向动画"),
                style: style,
              ),
            ),
            const SizedBox(height: 10),
            // 较大偏移量动画文本
            SingleWidgetAnimation(
              offset: 5,
              delayStart: const Duration(seconds: 3),
              child: Text(
                '较大偏移量',
                key: const Key('较大偏移量'),
                style: style,
              ),
            ),
            const SizedBox(height: 10),
            // 水平较大偏移量动画文本
            SingleWidgetAnimation(
              offset: 5,
              delayStart: const Duration(seconds: 4),
              direction: Direction.horizontal,
              child: Text(
                '水平较大偏移量',
                key: const Key('水平较大偏移量'),
                style: style,
              ),
            ),
            const SizedBox(height: 10),
            // 弹跳曲线动画文本
            SingleWidgetAnimation(
              delayStart: const Duration(seconds: 5),
              curve: Curves.bounceIn,
              direction: Direction.horizontal,
              offset: -0.2,
              child: Text(
                '弹跳曲线动画',
                key: const Key('弹跳曲线动画'),
                style: style,
              ),
            ),
            const SizedBox(height: 10),
            // 慢动作动画文本
            SingleWidgetAnimation(
              animationDuration: const Duration(seconds: 2),
              delayStart: const Duration(seconds: 6),
              child: Text(
                '慢动作',
                key: const Key('慢动作'),
                style: style,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


widget_animation 是一个用于在 Flutter 中实现各种动画效果的插件。它简化了创建复杂动画的过程,使开发者能够轻松地为 Widget 添加动画效果。以下是如何使用 widget_animation 插件的基本步骤和示例。

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 widget_animation 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  widget_animation: ^0.1.0  # 请检查最新版本

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

2. 导入包

在 Dart 文件中导入 widget_animation 包:

import 'package:widget_animation/widget_animation.dart';

3. 使用 WidgetAnimation

WidgetAnimation 提供了多种预定义的动画效果,你可以通过简单的配置来应用这些动画。

基本使用示例

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

class AnimationExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Widget Animation Example'),
      ),
      body: Center(
        child: WidgetAnimation(
          duration: Duration(seconds: 2),
          curve: Curves.easeInOut,
          animationType: AnimationType.fadeIn,
          child: Container(
            width: 200,
            height: 200,
            color: Colors.blue,
            child: Center(
              child: Text(
                'Hello, World!',
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

void main() => runApp(MaterialApp(
  home: AnimationExample(),
));

4. 支持的动画类型

WidgetAnimation 支持多种动画类型,你可以通过 animationType 参数来指定:

  • AnimationType.fadeIn: 淡入
  • AnimationType.fadeOut: 淡出
  • AnimationType.slideInLeft: 从左侧滑入
  • AnimationType.slideInRight: 从右侧滑入
  • AnimationType.slideInTop: 从顶部滑入
  • AnimationType.slideInBottom: 从底部滑入
  • AnimationType.scale: 缩放
  • AnimationType.rotate: 旋转

5. 自定义动画

你可以通过 durationcurve 参数来进一步定制动画的持续时间和动画曲线。

示例:自定义动画

WidgetAnimation(
  duration: Duration(seconds: 3),
  curve: Curves.bounceOut,
  animationType: AnimationType.rotate,
  child: Container(
    width: 200,
    height: 200,
    color: Colors.red,
    child: Center(
      child: Text(
        'Rotate!',
        style: TextStyle(color: Colors.white, fontSize: 20),
      ),
    ),
  ),
)

6. 组合动画

你还可以通过嵌套多个 WidgetAnimation 来组合不同的动画效果。

示例:组合动画

WidgetAnimation(
  duration: Duration(seconds: 2),
  curve: Curves.easeInOut,
  animationType: AnimationType.fadeIn,
  child: WidgetAnimation(
    duration: Duration(seconds: 1),
    curve: Curves.bounceOut,
    animationType: AnimationType.scale,
    child: Container(
      width: 200,
      height: 200,
      color: Colors.green,
      child: Center(
        child: Text(
          'Combo!',
          style: TextStyle(color: Colors.white, fontSize: 20),
        ),
      ),
    ),
  ),
)
回到顶部