flutter 平移動畫

import 'dart:async';

import 'package:flutter/material.dart';

class FlatAnimationPageextends StatefulWidget{

? final Duration pauseDuration, forwardDuration;

? final double scrollSpeed; //滾動速度(時間單位是秒)

? 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();

? }

? //初始化平移動畫控制器

? 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í)行平移動畫

? 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,

? ? ? ? );

? ? ? },

? ? );

? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容