75 lines
1.5 KiB
Dart
75 lines
1.5 KiB
Dart
|
// 滑动动画
|
||
|
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<StatefulWidget> createState() {
|
||
|
return _SlideTransitionState();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class _SlideTransitionState extends State<Slide> with TickerProviderStateMixin {
|
||
|
late final AnimationController _controller = AnimationController(
|
||
|
vsync: this,
|
||
|
duration: widget.duration,
|
||
|
)..forward();
|
||
|
|
||
|
late final Animation<double> _animation =
|
||
|
// 补间动画
|
||
|
// Tween<double>(
|
||
|
// 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<double> animation,
|
||
|
required Widget child,
|
||
|
double dx = 0,
|
||
|
double dy = -1,
|
||
|
}) {
|
||
|
return SlideTransition(
|
||
|
position: animation.drive(
|
||
|
Tween<Offset>(
|
||
|
begin: Offset(dx, dy),
|
||
|
end: Offset.zero,
|
||
|
).chain(CurveTween(curve: Curves.easeInOutSine)),
|
||
|
),
|
||
|
child: child,
|
||
|
);
|
||
|
}
|