flutter 弹出动画
时间: 2023-10-13 08:05:16 浏览: 211
Flutter提供了许多内置的动画效果,包括弹出动画。您可以使用Flutter的动画框架来创建自定义弹出动画。以下是一个简单的示例,演示如何使用Flutter的动画框架创建一个简单的弹出动画:
```dart
import 'package:flutter/material.dart';
class PopupAnimation extends StatefulWidget {
final Widget child;
PopupAnimation({required this.child});
@override
_PopupAnimationState createState() => _PopupAnimationState();
}
class _PopupAnimationState extends State<PopupAnimation>
with SingleTickerProviderStateMixin {
late AnimationController _animationController;
late Animation<double> _scaleAnimation;
@override
void initState() {
super.initState();
_animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500));
_scaleAnimation = Tween<double>(begin: 0, end: 1).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeInOut));
_animationController.forward();
}
@override
Widget build(BuildContext context) {
return ScaleTransition(
scale: _scaleAnimation,
child: widget.child,
);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
}
```
然后,您可以在需要弹出动画的地方使用`PopupAnimation`小部件,例如:
```dart
showDialog(
context: context,
builder: (BuildContext context) {
return PopupAnimation(
child: AlertDialog(
title: Text('Dialog title'),
content: Text('Dialog content'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text('Close'),
),
],
),
);
},
);
```
在上面的示例中,我们使用`ScaleTransition`小部件和`AnimationController`创建一个简单的弹出动画。在`initState`中,我们创建了一个`AnimationController`,并使用`Tween`和`CurvedAnimation`来创建一个`Animation`。然后,我们在`build`方法中将`ScaleTransition`小部件包装在`PopupAnimation`小部件中,并将`Animation`应用于`ScaleTransition`的比例属性。最后,在`dispose`方法中释放`AnimationController`。
阅读全文