// 滑动动画 import 'package:flutter/material.dart'; class Slide extends StatefulWidget { final Duration duration; final Widget child; const Slide({ super.key, required this.child, this.duration = const Duration(milliseconds: 1000), }); @override State createState() { return _SlideTransitionState(); } } class _SlideTransitionState extends State with TickerProviderStateMixin { late final AnimationController _controller = AnimationController( vsync: this, duration: widget.duration, )..forward(); late final Animation _animation = // 补间动画 // Tween( // begin: 0, // end: 1, // ).animate(_controller); // 线性动画 CurvedAnimation(parent: _controller, curve: Curves.decelerate); @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: Center( child: _slide( animation: _animation, child: widget.child, dy: 0, dx: 1 ), ), ); } } AnimatedWidget _slide({ required Animation animation, required Widget child, double dx = 0, double dy = -1, }) { return SlideTransition( position: animation.drive( Tween( begin: Offset(dx, dy), end: Offset.zero, ).chain(CurveTween(curve: Curves.easeInOutSine)), ), child: child, ); }