Flutter动画创建插件flutter_animation_factory的使用
Flutter动画创建插件flutter_animation_factory
的使用
demo_animation
Flutter动画插件,用于轻松创建和管理动画效果。
Getting Started
使用方式非常简单,只需设定起始坐标、目标位置以及需要变化的空间即可。
以下是一个简单的示例代码:
AudioAllMicGift(
endOffset: RandomScreenOffset.generateRandomScreenCoordinates(context),
durationTime: 3000,
offSetY: 100,
childWidget: const Icon(Icons.accessibility),
)
直接上效果
运行后的动画效果如下图所示:
完整示例代码
以下是一个完整的示例代码,展示如何在Flutter应用中使用该插件。
import 'package:demo_animation/audio_all_mic_gift.dart';
import 'package:demo_animation/tools/random_screen_offset.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.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({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Widget> widgetList = [];
bool isOnClick = false;
void _incrementCounter() async {
if (!isOnClick) {
isOnClick = true;
for (int i = 0; i < 100000; i++) {
await Future.delayed(const Duration(milliseconds: 150)); // 模拟异步操作
setState(() {
widgetList.add(AudioAllMicGift(
endOffset: RandomScreenOffset.generateRandomScreenCoordinates(context),
// childWidget: const Image(image: AssetImage("images/xiayu.gif"), width: 100.0),
childWidget: const Icon(Icons.accessibility),
));
});
}
} else {
return;
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Stack(alignment: Alignment.center, children: widgetList),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
1 回复
更多关于Flutter动画创建插件flutter_animation_factory的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_animation_factory
是一个用于简化 Flutter 动画创建的插件。它提供了一种声明式的方式来创建和管理动画,使得开发者可以更容易地实现复杂的动画效果。以下是使用 flutter_animation_factory
的基本步骤和示例。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 flutter_animation_factory
依赖:
dependencies:
flutter:
sdk: flutter
flutter_animation_factory: ^1.0.0 # 请检查最新版本
然后运行 flutter pub get
来获取依赖。
2. 基本用法
flutter_animation_factory
提供了一个 AnimationFactory
类,你可以使用它来创建和管理动画。
示例:创建一个简单的缩放动画
import 'package:flutter/material.dart';
import 'package:flutter_animation_factory/flutter_animation_factory.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Flutter Animation Factory Example')),
body: Center(
child: ScaleAnimationExample(),
),
),
);
}
}
class ScaleAnimationExample extends StatefulWidget {
@override
_ScaleAnimationExampleState createState() => _ScaleAnimationExampleState();
}
class _ScaleAnimationExampleState extends State<ScaleAnimationExample>
with SingleTickerProviderStateMixin {
AnimationController _controller;
Animation<double> _animation;
@override
void initState() {
super.initState();
// 创建 AnimationController
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 2),
);
// 使用 AnimationFactory 创建动画
final animationFactory = AnimationFactory(_controller);
_animation = animationFactory.animate(
begin: 0.0,
end: 1.0,
curve: Curves.easeInOut,
);
// 启动动画
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Transform.scale(
scale: _animation.value,
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
);
},
);
}
}
3. 解释
- AnimationController: 用于控制动画的播放、暂停、停止等操作。
- AnimationFactory: 提供了简单的方法来创建动画。你可以指定动画的起始值、结束值、曲线等。
- AnimatedBuilder: 用于在动画值发生变化时重建 UI。
4. 更多动画类型
flutter_animation_factory
支持多种动画类型,例如透明度动画、旋转动画、位移动画等。你可以根据需要创建不同的动画效果。
示例:创建一个旋转动画
final rotationAnimation = animationFactory.animate(
begin: 0.0,
end: 2 * pi, // 旋转360度
curve: Curves.easeInOut,
);
return Transform.rotate(
angle: rotationAnimation.value,
child: Container(
width: 100,
height: 100,
color: Colors.red,
),
);
5. 组合动画
你可以将多个动画组合在一起,创建更复杂的动画效果。
示例:组合缩放和旋转动画
final scaleAnimation = animationFactory.animate(
begin: 0.0,
end: 1.0,
curve: Curves.easeInOut,
);
final rotationAnimation = animationFactory.animate(
begin: 0.0,
end: 2 * pi,
curve: Curves.easeInOut,
);
return Transform(
transform: Matrix4.identity()
..scale(scaleAnimation.value)
..rotateZ(rotationAnimation.value),
child: Container(
width: 100,
height: 100,
color: Colors.green,
),
);