【问题标题】:How can I spin the image along Z axis in flutter?如何在颤动中沿 Z 轴旋转图像?
【发布时间】:2018-11-30 11:29:41
【问题描述】:

我在颤振代码中使用 Transform 小部件来旋转屏幕

Offset _offset = Offset.zero;

return new Transform(
        transform: Matrix4.identity()
               ..setEntry(3, 2, 0.001)
               ..rotateX(0.01 * _offset.dy)
               ..rotateY(-0.01 * _offset.dx)
               ..rotateZ(- 0.01 * _offset.),
        alignment: FractionalOffset.center,
        child: new Scaffold(
          appBar: AppBar(
            title: Text("The 3D Matrix"),
          ),
          body: GestureDetector(
              onPanUpdate: (details) => setState(() => _offset += details.delta),
              onDoubleTap: () => setState(() => _offset = Offset.zero),
              child: Content())
        ),);

现在我想要以一定的速度沿 z 轴旋转小部件,并在几秒钟后将其速度减慢到零。

可能我需要使用动画控制器。我们如何才能达到这种状态?

现在我做到了这么多:

【问题讨论】:

    标签: dart flutter flutter-layout flutter-animation


    【解决方案1】:

    只需将AnimationController 添加到您的页面小部件。然后将您的Transform 包装成AnimatedBuilder

    当您需要启动动画时,请致电animationController.forward()

    class MyHome extends StatefulWidget {
      @override
      _MyHomeState createState() => _MyHomeState();
    }
    
    class _MyHomeState extends State<MyHome> with SingleTickerProviderStateMixin {
      AnimationController animationController;
    
      @override
      void initState() {
        animationController = new AnimationController(
          duration: const Duration(seconds: 2),
          vsync: this,
        );
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return new AnimatedBuilder(
          animation: animationController,
          builder: (context, child) {
            return new Transform(
              transform: Matrix4.identity()
                ..setEntry(3, 2, 0.001)
                ..rotateZ(animationController.value * 45.0),
              child: child,
            );
          },
          child: new Scaffold(
            appBar: AppBar(
              title: Text("The 3D Matrix"),
            ),
            body: new Center(
              child: new RaisedButton(
                onPressed: () => animationController.forward(),
                child: new Text("Start anim"),
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 我只想在我的手势小部件中添加一项旋转功能,如 onTap 或 onLongPressed 并保持所有功能相同。你刚刚展示了我的旋转功能。
    • 你还需要什么?
    • 我希望 GestureDetector 的两个功能相同,以及如何添加一个 onTap() => 的属性,它开始旋转活动,然后慢慢恢复到原始状态。
    • 你能解释一下 setEntry 是做什么的吗?
    猜你喜欢
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2011-10-30
    • 2011-03-29
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多