Animation 颤振:捕捉下的多个动画

Animation 颤振:捕捉下的多个动画,animation,flutter,flare,Animation,Flutter,Flare,如何让同一动画同时在不同的地方播放。目前,每当我点击屏幕,动画就会在那个地方开始播放3秒钟,但如果我再次按下另一个地方,动画就会跳到那个地方,但我希望它能让旧的那个地方结束。因此,如果按五次,屏幕上应该有五个动画,而不是现在一次只播放一个 这是我的密码 class HomePage extends StatefulWidget{ @override _HomePageState createState() => _HomePageState(); } class _HomePag

如何让同一动画同时在不同的地方播放。目前,每当我点击屏幕,动画就会在那个地方开始播放3秒钟,但如果我再次按下另一个地方,动画就会跳到那个地方,但我希望它能让旧的那个地方结束。因此,如果按五次,屏幕上应该有五个动画,而不是现在一次只播放一个

这是我的密码

class HomePage extends StatefulWidget{
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {


   double posx = -100.0;
  double posy = -100.0;

void onLongPressMoveUpdate(BuildContext context, LongPressMoveUpdateDetails details)

  {
    final RenderBox box = context.findRenderObject();
    final Offset localOffset = box.globalToLocal(details.globalPosition);

    var tempName = "tryck";
    setState(() {
      _tryckanimation = tempName;
      posx = localOffset.dx;
      posy = localOffset.dy;
    });

  }
  void onTapDown(BuildContext context, TapDownDetails details)

  {



    final RenderBox box = context.findRenderObject();
    final Offset localOffset = box.globalToLocal(details.globalPosition);

              var tempName = "tryck";
    setState(() {


      _tryckanimation = tempName;
      posx = localOffset.dx;
      posy = localOffset.dy;
    });

  }
 void onTapUp(BuildContext context, TapUpDetails details) 
 {
    final RenderBox box = context.findRenderObject();
    final Offset localOffset = box.globalToLocal(details.globalPosition);

              var tempName = "tryck";
    Future.delayed(const Duration(milliseconds: 3000), () {
  setState(() {
    setState(() {
      _tryckanimation = tempName;
      posx = -100.0;
      posy = -100.0;
       });
});
 });

  }

new GestureDetector(
      onTapDown: (TapDownDetails details) => onTapDown(context, details),
       onTapUp: (TapUpDetails details) => onTapUp(context, details),
       onLongPressMoveUpdate: (LongPressMoveUpdateDetails details) => onLongPressMoveUpdate(context, details),


      child: new Stack(fit: StackFit.expand, children: <Widget>[

        new Container(color: Colors.transparent,
        ),

            new Positioned(
            height: 100.0,
            width: 100.0,
            child: FlareActor(
              "assets/images/tryck2.flr",
             animation: _tryckanimation
            ),

            left: posx -50.0,
            top: posy- 50.0,



            ),
类主页扩展StatefulWidget{
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
双posx=-100.0;
双posy=-100.0;
void onLongPressMoveUpdate(构建上下文,LongPressMoveUpdateDetails详细信息)
{
final RenderBox=context.findenderObject();
最终偏移量localOffset=box.globalToLocal(details.globalPosition);
var tempName=“tryck”;
设置状态(){
_tryckanimation=tempName;
posx=localOffset.dx;
posy=localOffset.dy;
});
}
void onTapDown(BuildContext上下文,tappdownDetails)
{
final RenderBox=context.findenderObject();
最终偏移量localOffset=box.globalToLocal(details.globalPosition);
var tempName=“tryck”;
设置状态(){
_tryckanimation=tempName;
posx=localOffset.dx;
posy=localOffset.dy;
});
}
void onTapUp(BuildContext上下文、TapUpDetails)
{
final RenderBox=context.findenderObject();
最终偏移量localOffset=box.globalToLocal(details.globalPosition);
var tempName=“tryck”;
延迟(常数持续时间(毫秒:3000),(){
设置状态(){
设置状态(){
_tryckanimation=tempName;
posx=-100.0;
posy=-100.0;
});
});
});
}
新手势检测器(
onTapDown:(tappdownDetails)=>onTapDown(上下文,细节),
onTapUp:(TapUpDetails细节)=>onTapUp(上下文,细节),
onLongPressMoveUpdate:(LongPressMoveUpdateDetails)=>onLongPressMoveUpdate(上下文,详细信息),
子项:新堆栈(拟合:StackFit.expand,子项:[
新容器(颜色:Colors.transparent,
),
新定位(
高度:100.0,
宽度:100.0,
儿童:FlareActor(
“资产/图像/tryck2.flr”,
动画:_tryckanimation
),
左:posx-50.0,
顶部:posy-50.0,
),

您必须为所有动画小部件设置相同的
AnimationController

试试这个

import 'dart:math';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: HomePage(),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  final tappedPositions = <Offset>[];

  AnimationController _animationController;

  @override
  void initState() {
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 2),
    );
    _animationController.repeat();
    super.initState();
  }

  final x = const CircularProgressIndicator();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("App Bar"),
      ),
      body: Stack(
        children: <Widget>[
          GestureDetector(
            onTapUp: (tabDetails) {
              setState(() {
                tappedPositions.add(tabDetails.localPosition);
              });
            },
            child: Container(
              color: Colors.white,
            ),
          ),
          for (final position in tappedPositions)
            Positioned(
              top: position.dy,
              left: position.dx,
              child: MyAnimatedWidget(
                animation: _animationController,
              ),
            ),
        ],
      ),
    );
  }
}

class MyAnimatedWidget extends StatelessWidget {
  final Animation animation;

  const MyAnimatedWidget({Key key, this.animation}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: animation,
      child: Icon(
        Icons.phone,
        color: Colors.blue,
        size: 30.0,
      ),
      builder: (context, child) {
        return Transform(
          alignment: Alignment.center,
          transform: Matrix4.identity()
            ..translate(animation.value)
            ..rotateY(pi * 2 * animation.value),
          child: child,
        );
      },
    );
  }
}
import'dart:math';
进口“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
家:脚手架(
正文:主页(),
),
);
}
}
类主页扩展了StatefulWidget{
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态
使用SingleTickerProviderStateMixin{
最终攻丝位置=[];
AnimationController _AnimationController;
@凌驾
void initState(){
_animationController=animationController(
vsync:这个,,
持续时间:常数持续时间(秒数:2),
);
_animationController.repeat();
super.initState();
}
最终x=常量循环前进指示器();
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“应用程序栏”),
),
主体:堆栈(
儿童:[
手势检测器(
onTapUp:(选项卡详细信息){
设置状态(){
tappedPositions.add(tabDetails.localPosition);
});
},
子:容器(
颜色:颜色,白色,
),
),
用于(攻丝位置的最终位置)
定位(
顶部:position.dy,
左:position.dx,
子:MyAnimatedWidget(
动画:_animationController,
),
),
],
),
);
}
}
类MyAnimatedWidget扩展了无状态Widget{
最终动画;
constMyAnimatedWidget({Key-Key,this.animation}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回动画生成器(
动画:动画,
子:图标(
Icons.phone,
颜色:颜色,蓝色,
尺寸:30.0,
),
生成器:(上下文,子对象){
返回变换(
对齐:对齐.center,
转换:Matrix4.identity()
…翻译(动画.值)
..rotateY(pi*2*animation.value),
孩子:孩子,
);
},
);
}
}