您可以使用Flutter的AnimationController和Tween来创建动画控制器,以下是一个简单的示例:

首先,在State类中创建一个AnimationController和Tween对象:

AnimationController _controller;
Tween<double> _tween;

然后,在initState方法中初始化这些对象,并为动画设置状态转换:

@override
void initState() {
  super.initState();
  _controller = AnimationController(
      vsync: this, duration: Duration(milliseconds: 2000));
  _tween = Tween<double>(begin: 0, end: 1);
  _controller.addListener(() {
    setState(() {});
  });
}

接下来,在build方法中,使用AnimatedBuilder小部件来创建动画:

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: AnimatedBuilder(
        animation: _controller,
        builder: (context, child) {
          return Transform.translate(
            offset: Offset(0, -100 * _tween.evaluate(_controller)),
            child: child,
          );
        },
        child: Container(
          width: 100,
          height: 100,
          color: Colors.blue,
        ),
      ),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: () {
        _controller.reset();
        _controller.forward();
      },
      child: Icon(Icons.arrow_upward),
    ),
  );
}

在这个示例中,AnimatedBuilder小部件使用_transform.translate_来实现垂直移动,并将Tween对象与AnimationController对象一起使用来控制动画的状态转换。

您可以在onPressed方法中使用_controller.reset()和_controller.forward()方法来启动动画

给我添加一个动画控制器这个动画有起飞保持飞翔降落行走四种状态我需要在点击按钮时起飞并保持飞翔2s然后降落在行走 floatingActionButton Positioned bottom 50 right 50

原文地址: http://www.cveoy.top/t/topic/fly9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录