Flutter动画效果插件phlox_animations的使用

发布于 1周前 作者 itying888 来自 Flutter

Flutter动画效果插件phlox_animations的使用

Phlox Animations 是一个用于创建漂亮自定义动画的简单包。它支持同时对多个属性进行动画处理。

开始使用

添加依赖

pubspec.yaml 文件中添加 phlox_animations 依赖:

dependencies:
  phlox_animations: any

然后运行以下命令来获取和升级包:

flutter pub get
flutter pub upgrade

导入库

在 Dart 文件中导入库:

import 'package:phlox_animations/phlox_animations.dart';

示例代码

缩放动画 (Scale)

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: PhloxAnimations(
          duration: const Duration(seconds: 2),
          child: Container(
            height: 300,
            width: 300,
            decoration: BoxDecoration(
              color: Colors.blue.shade900,
              borderRadius: BorderRadius.circular(32)
            ),
          ),
          fromScale: 0,
          toScale: 2,
        ),
      ),
    );
  }
}

移动动画 (Move X)

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: PhloxAnimations(
          duration: const Duration(seconds: 2),
          child: Container(
            height: 310,
            width: 310,
            decoration: BoxDecoration(
              color: Colors.blue.shade900,
              borderRadius: BorderRadius.circular(32)
            ),
          ),
          fromX: -200,
          toX: 200,
        ),
      ),
    );
  }
}

移动XY带循环 (Move XY with Loop)

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: PhloxAnimations(
          duration: const Duration(seconds: 2),
          child: Container(
            height: 250,
            width: 250,
            decoration: BoxDecoration(
              color: Colors.blue.shade900,
              borderRadius: BorderRadius.circular(32)
            ),
          ),
          fromX: -200,
          toX: 200,
          fromY: -200,
          toY: 200,
          loop: true,
        ),
      ),
    );
  }
}

使用控制器控制动画 (PhloxAnimationsController)

class Example4 extends StatefulWidget {
  const Example4({Key? key}) : super(key: key);

  @override
  State<Example4> createState() => _Example4State();
}

class _Example4State extends State<Example4> {
  PhloxAnimationsController controller = PhloxAnimationsController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (controller.animationStatus == AnimationStatus.dismissed ||
              controller.animationStatus == AnimationStatus.reverse) {
            controller.forward();
          } else if (controller.animationStatus == AnimationStatus.completed ||
              controller.animationStatus == AnimationStatus.forward) {
            controller.reverse();
          }
        },
      ),
      body: Center(
        child: PhloxAnimations(
          auto: false,
          controller: controller,
          duration: const Duration(seconds: 2),
          child: Container(
            height: 250,
            width: 250,
            decoration: BoxDecoration(
              color: Colors.blue.shade900,
              borderRadius: BorderRadius.circular(32)
            ),
          ),
          fromX: -100,
          toX: 100,
          toY: 200,
          fromY: -200,
          toDegrees: 90,
        ),
      ),
    );
  }
}

自定义动画 (Custom Animations)

class Example5 extends StatefulWidget {
  const Example5({Key? key}) : super(key: key);

  @override
  State<Example5> createState() => _Example5State();
}

class _Example5State extends State<Example5> {
  PhloxAnimationsController controller = PhloxAnimationsController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: PhloxAnimations.custom(
          auto: false,
          controller: controller,
          duration: const Duration(seconds: 2),
          fromColor: Colors.redAccent,
          toColor: Colors.lightBlueAccent,
          builder: (animations) {
            return Transform.translate(
              offset: Offset(animations.moveX!, animations.moveY!),
              child: Transform.rotate(
                angle: controller.degreeToRadian(animations.rotate!),
                child: Transform.scale(
                  scale: animations.scale,
                  child: MaterialButton(
                    onPressed: () {
                      if (controller.animationStatus == AnimationStatus.dismissed ||
                          controller.animationStatus == AnimationStatus.reverse) {
                        controller.forward();
                      } else if (controller.animationStatus == AnimationStatus.completed ||
                          controller.animationStatus == AnimationStatus.forward) {
                        controller.reverse();
                      }
                    },
                    color: animations.color,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(animations.radius!)
                    ),
                    child: const Text("Animate me"),
                  ),
                ),
              ),
            );
          },
          fromX: -100,
          toX: 100,
          toY: 100,
          fromY: -100,
          toScale: 3,
          fromRadius: 8,
          toRadius: 24,
        ),
      ),
    );
  }
}

曲线动画 (Curves)

可以通过设置曲线来改变动画的效果,例如:

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(32),
        child: Row(
          children: [
            PhloxAnimations(
              toX: 400,
              moveXCurve: Curves.easeInQuart, // curve
              toDegrees: 700,
              duration: const Duration(seconds: 3),
              child: Image.network(
                "https://pngimg.com/uploads/football/football_PNG28467.png",
                width: 100,
                height: 100,
              ),
            )
          ],
        ),
      ),
    );
  }
}

以上是关于 phlox_animations 插件的基本使用方法和示例代码,更多详细信息可以参考 Phlox Animations GitHub官方文档


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

1 回复

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


当然,以下是如何在Flutter中使用phlox_animations插件来实现一些基本动画效果的代码示例。phlox_animations是一个强大的Flutter插件,它提供了一系列预定义的动画效果,可以很方便地集成到你的应用中。

首先,确保你已经在pubspec.yaml文件中添加了phlox_animations依赖:

dependencies:
  flutter:
    sdk: flutter
  phlox_animations: ^最新版本号  # 请替换为最新版本号

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

示例代码

以下是一个简单的Flutter应用示例,展示了如何使用phlox_animations来实现一些动画效果。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Phlox Animations Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    )..repeat(reverse: true);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Phlox Animations Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // 使用PhloxScaleAnimation
            PhloxScaleAnimation(
              child: Container(
                color: Colors.red,
                width: 100,
                height: 100,
                child: Center(child: Text('Scale')),
              ),
              animationController: _controller,
              scale: 1.5,
            ),
            SizedBox(height: 20),

            // 使用PhloxRotateAnimation
            PhloxRotateAnimation(
              child: Container(
                color: Colors.green,
                width: 100,
                height: 100,
                child: Center(child: Text('Rotate')),
              ),
              animationController: _controller,
              rotateAngle: pi, // 180 degrees
            ),
            SizedBox(height: 20),

            // 使用PhloxFadeAnimation
            PhloxFadeAnimation(
              child: Container(
                color: Colors.blue,
                width: 100,
                height: 100,
                child: Center(child: Text('Fade')),
              ),
              animationController: _controller,
            ),
          ],
        ),
      ),
    );
  }
}

解释

  1. 依赖安装:首先,在pubspec.yaml文件中添加phlox_animations依赖。
  2. 动画控制器:在_HomeScreenState类中,我们创建了一个AnimationController来控制动画的持续时间和循环。
  3. 动画组件
    • PhloxScaleAnimation:用于缩放动画。
    • PhloxRotateAnimation:用于旋转动画。
    • PhloxFadeAnimation:用于淡入淡出动画。
  4. 动画绑定:每个动画组件都绑定到了同一个AnimationController,这意味着它们将同步进行动画。

你可以根据需要调整动画的参数,例如持续时间、缩放比例、旋转角度等,以实现不同的动画效果。phlox_animations插件提供了丰富的动画组件,你可以查阅其官方文档了解更多高级用法。

回到顶部