如何使图像在颤动中向上移动

How to make the image shift up in flutter

最近在研究flutter中的动画,比如SlideTransition。我想做的是从屏幕页面的中心显示图像,然后 3 秒后我想让图像从中心向上移动几厘米。有办法吗?

请查看以下代码:

import 'package:flutter/material.dart';

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

  @override
  State<TransitionUpAnimation> createState() => _TransitionUpAnimationState();
}

class _TransitionUpAnimationState extends State<TransitionUpAnimation>
    with TickerProviderStateMixin {
  AnimationController? _controller;
  Animation<Offset>? _animation;
  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    );
    _animation = Tween<Offset>(
      begin: const Offset(0.0, 0.0),
      end: const Offset(0.0, -2),
    ).animate(CurvedAnimation(
      parent: _controller!,
      curve: Curves.easeInCubic,
    ));
    Future.delayed(const Duration(milliseconds: 3000), () {
      _controller!.forward();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SlideTransition(
          position: _animation!,
          child: Container(
            width: 100,
            height: 100,
            color: Colors.red,
          ),
        ),
      ),
    );
  }
}

输出: