Flutter:显示用户点击的位置

时间:2019-12-14 17:14:35

标签: animation flutter onclick gesturedetector flare

是否可以在用户单击屏幕时显示动画。我希望在用户按下屏幕的位置出现一个小的动画星。

我有这个动画,它现在只是循环播放,但是我只希望它在用户按下屏幕时循环播放。

 new Container(
  alignment: Alignment(0.0, -1.0),

    child: new Container(
     height: 410.0,

            child: FlareActor(
              "assets/images/nyastar.flr",

             animation: _animationName,
            )
    )
      ),

1 个答案:

答案 0 :(得分:0)

来源:https://stackoverflow.com/a/46566392/5882307

import 'package:flutter/material.dart';

class Demo extends StatefulWidget {
  @override
  MyHomePageState createState() => new MyHomePageState();
}

class MyHomePageState extends State<Demo> {
  double posx = 100.0;
  double posy = 100.0;

  void onTapDown(BuildContext context, TapDownDetails details) {
    final RenderBox box = context.findRenderObject();
    final Offset localOffset = box.globalToLocal(details.globalPosition);
    setState(() {
      posx = localOffset.dx;
      posy = localOffset.dy;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new GestureDetector(
      onTapDown: (TapDownDetails details) => onTapDown(context, details),
      child: new Stack(fit: StackFit.expand, children: <Widget>[
        // Hack to expand stack to fill all the space. There must be a better
        // way to do it.
        new Container(color: Colors.white),
        new Positioned(
          child: Text('hello'), //your widget to be shown
          left: posx,
          top: posy,
        )
      ]),
    );
  }
}