import 'dart:async';
import 'package:flutter/material.dart';
class FlatAnimationPageextends StatefulWidget{
? final Duration pauseDuration, forwardDuration;
? final double scrollSpeed; //滾動(dòng)速度(時(shí)間單位是秒)凳兵。
? final Widget child; //子視圖否过。
? ///注: 構(gòu)造函數(shù)入?yún)⒌哪J(rèn)值必須是常量衷咽。
? 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<FlatAnimationPage>
? ? with SingleTickerProviderStateMixin{
? bool _validFlag= true;
? double _boxWidth= 0;
? final ScrollController_controller = ScrollController();
? AnimationController? animationController;
? Animation<Offset>? animation;
? @override
? void dispose() {
? ? debugPrint('Track_MarqueeView_dispose');
? ? _validFlag= false;
? ? _controller.dispose();
? ? animationController?.dispose();
? ? super.dispose();
? }
? @override
? void initState() {
? ? super.initState();
? ? initController();
? ? initAnimation();
? ? forward();
? }
? //初始化平移動(dòng)畫(huà)控制器
? 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);
? }
? //執(zhí)行平移動(dòng)畫(huà)
? forward()async{
? ? animationController?.forward();
? }
? //回到原點(diǎn)
? 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,
? ? ? ? );
? ? ? },
? ? );
? }
}