import 'dart:async';
import 'package:flutter/material.dart';
class FlatAnimationPageextends StatefulWidget{
final Duration pauseDuration, forwardDuration;
final double scrollSpeed; //滚动速度(时间单位是秒)。
final Widget child; //子视图。
///注: 构造函数入参的默认值必须是常量。
const FlatAnimationPage(
{Key? key,
this.pauseDuration =const Duration(milliseconds:100),
this.forwardDuration =const Duration(milliseconds:3000),
this.scrollSpeed =40.0,
required this.child})
:super(key: key);
@override
_FlatAnimationPageStatecreateState() => _FlatAnimationPageState();
}
class _FlatAnimationPageStateextends State
with SingleTickerProviderStateMixin{
bool _validFlag= true;
double _boxWidth= 0;
final ScrollController_controller = ScrollController();
AnimationController? animationController;
Animation
@override
void dispose() {
debugPrint('Track_MarqueeView_dispose');
_validFlag= false;
_controller.dispose();
animationController?.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
initController();
initAnimation();
forward();
}
//初始化平移动画控制器
void initController(){
if(animationController!=null){
animationController?.dispose();
}
animationController=
AnimationController(duration:Duration(milliseconds:3000), vsync:this)
..addStatusListener((status) {
if (status ==AnimationStatus.completed) {
//animationController?.reverse();
reset();
//forward();
}else if (status ==AnimationStatus.dismissed) {
//animationController?.forward();
forward();
}
});
}
initAnimation(){
animation =Tween(begin:Offset(1.5,0) , end:Offset.zero).animate(animationController as AnimationController);
}
//执行平移动画
forward()async{
animationController?.forward();
}
//回到原点
reset()async{
animationController?.reset();
}
@override
Widgetbuild(BuildContextcontext) {
/// 使用LayoutBuilder获取组件的大小。
return LayoutBuilder(
builder: (BuildContext context,BoxConstraints constraints) {
_boxWidth = constraints.maxWidth;
return SlideTransition(
position:animation as Animation,
child:widget.child,
);
},
);
}
}