简体   繁体   English

Flutter - 使用 Transform.rotate 旋转容器后,它的子手势检测器 ontap 方法不起作用

[英]Flutter - after rotate container with Transform.rotate, it's child gesturedetector ontap method not working

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

When I rotate the container with angle 45 or other radians, the delete button、expand button not performed the 'onTap', 'onPanUpdate' gesture.当我以 45 角或其他弧度旋转容器时,删除按钮、展开按钮没有执行 'onTap'、'onPanUpdate' 手势。 I found rotate the container with 'RotatedBox' is good, but its param 'quarterTurns' only supports int type value.我发现用 'RotatedBox' 旋转容器很好,但它的参数 'quarterTurns' 只支持 int 类型值。 I know the differences between 'Transform.rotate' and 'RotatedBox'.我知道“Transform.rotate”和“RotatedBox”之间的区别。 'Transform.rotate' only refreshes UI, and 'Rotatedbox' affects its children layout. “Transform.rotate”仅刷新 UI,“Rotatedbox”影响其子布局。 I don't know how to do in my case.我不知道在我的情况下该怎么做。

please help me, thanks.请帮助我,谢谢。

here is my sample code:这是我的示例代码:

@override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: stickersViewNotifier,
      child: Consumer<KSStickersViewNotifier>(
        builder: (context, notifier, child) {
          return Positioned(
              left: stickersViewNotifier._left,
              top: stickersViewNotifier._top,
              child: GestureDetector(
                  onTap: () {
                    stickersViewNotifier.updateShouldShowControl(!stickersViewNotifier.shouldShowControl);
                  },
                  onPanDown: (DragDownDetails e) {
                    print("${e.globalPosition}");
                  },
                  onPanUpdate: (DragUpdateDetails e) {
                    stickersViewNotifier.panContainer(e.delta.dx, e.delta.dy);
                  },
                  onPanEnd: (DragEndDetails e) {
                    //打印滑动结束时在x、y轴上的速度
                    print(e.velocity);
                  },
                  child: Transform.rotate(
                    angle: 45 / 180 * math.pi,
                    child: Container(
                      width: stickersViewNotifier._width,
                      height: stickersViewNotifier._height,
                      child: Stack(
                        children: [
                          Padding(
                            padding: EdgeInsets.all(controlButtonWidth * 0.33),
                            child: DottedBorder(
                              color: stickersViewNotifier.shouldShowControl ? Colors.black : Colors.transparent,
                              strokeWidth: 1,
                              dashPattern: [5, 5, 5, 5],
                              child: Container(
                                width: double.infinity,
                                height: double.infinity,
                                child: Padding(
                                  padding: EdgeInsets.all(controlButtonWidth * 0.67),
                                  child: Center(
                                    child: containerWidget,
                                  ),
                                ),
                              ),
                            ),
                          ),
                          Offstage(
                            offstage: !stickersViewNotifier.shouldShowControl,
                            child: GestureDetector(
                              onTap: () {
                                onDelete(notifier.tag);
                              },
                              child: Container(
                                width: controlButtonWidth,
                                height: controlButtonWidth,
                                decoration: BoxDecoration(color: Colors.redAccent, borderRadius: BorderRadius.circular(controlButtonWidth / 2)),
                                child: Center(
                                  child: Icon(
                                    Icons.close_rounded,
                                    color: Colors.white,
                                    size: controlButtonWidth - 5,
                                  ),
                                ),
                              ),
                            ),
                          ),
                          Positioned(
                            right: 0,
                            child: Offstage(
                              offstage: !stickersViewNotifier.shouldShowControl,
                              child: GestureDetector(
                                child: Container(
                                  width: controlButtonWidth,
                                  height: controlButtonWidth,
                                  decoration: BoxDecoration(color: Colors.redAccent, borderRadius: BorderRadius.circular(controlButtonWidth / 2)),
                                  child: Center(
                                    child: Icon(
                                      Icons.refresh_rounded,
                                      color: Colors.white,
                                      size: controlButtonWidth - 5,
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          ),
                          Positioned(
                            right: 0,
                            bottom: 0,
                            child: Offstage(
                              offstage: !stickersViewNotifier.shouldShowControl,
                              child: GestureDetector(
                                onPanUpdate: (DragUpdateDetails details) {
                                  double dx = details.delta.dx;
                                  double dy = details.delta.dy;
                                  stickersViewNotifier.zoom(dx, dy);
                                },
                                child: Container(
                                  width: controlButtonWidth,
                                  height: controlButtonWidth,
                                  decoration: BoxDecoration(color: Colors.redAccent, borderRadius: BorderRadius.circular(controlButtonWidth / 2)),
                                  child: Center(
                                    child: Icon(
                                      Icons.zoom_out_map_rounded,
                                      color: Colors.white,
                                      size: controlButtonWidth - 8,
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  )));
        },
      ),
    );
  }

here is an demo: hope you will get the idea.这是一个演示:希望你能明白。 move buttons on top level widget.在顶级小部件上移动按钮。 and rotate the full container including buttons.并旋转整个容器,包括按钮。 also use multi-stack for this purpose.也为此目的使用多堆栈。

import 'dart:math';

import 'package:flutter/material.dart';

class DeleteOnRotate extends StatefulWidget {
  DeleteOnRotate({Key? key}) : super(key: key);

  @override
  _DeleteOnRotateState createState() => _DeleteOnRotateState();
}

class _DeleteOnRotateState extends State<DeleteOnRotate> {
  double _angle = 0;
  bool _visible = true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: LayoutBuilder(
        builder: (context, constraints) => Stack(
          children: [
            Align(
              alignment: Alignment.bottomCenter,
              child: TextButton(
                child: Text("ChangeVisibility"),
                onPressed: () {
                  setState(() {
                    _visible = !_visible;
                  });
                },
              ),
            ),

            ///stickerBox
            if (_visible)
              Container(
                height: constraints.maxHeight * .6,
                width: constraints.maxWidth,
                color: Colors.cyanAccent.withOpacity(.2),
                child: Stack(
                  children: [
                    Align(
                      alignment: Alignment(0, 0),
                      child: Transform.rotate(
                        angle: _angle / 180 * pi,
                        child: Container(
                          height: constraints.maxHeight * .35,
                          width: constraints.maxWidth * .6,
                          decoration: BoxDecoration(
                            border: Border.all(
                              style: BorderStyle.solid,
                            ),
                          ),
                          child: Stack(
                            children: [
                              Positioned(
                                top: 0,
                                left: 0,
                                child: IconButton(
                                  onPressed: () {
                                    setState(() {
                                      _visible = !_visible;
                                    });
                                  },
                                  icon: Icon(
                                    Icons.delete,
                                  ),
                                ),
                              ),
                              Positioned(
                                top: 0,
                                right: 0,
                                child: IconButton(
                                  onPressed: () {
                                    setState(() {
                                      _angle += 45;
                                    });
                                  },
                                  icon: Icon(
                                    Icons.rotate_right,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              ),
          ],
        ),
      ),
    );
  }
}


```

@kevin-su have you got the solution for the above issue? @kevin-su 你有解决上述问题的方法吗? If yes, could you please provide the working code.如果是,请您提供工作代码。 I am working on similar feature with Scaling, Moving and rotating the text.我正在研究与缩放、移动和旋转文本类似的功能。 Hope your code helps me.希望你的代码对我有帮助。 Thanks in advance.提前致谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter transform.rotate 不旋转动画生成器中的列表项 - Flutter transform.rotate not rotating list items in animated builder Flutter GestureDetector onTap 不工作 - Flutter GestureDetector onTap not working Flutter:当我通过 Transform.rotate 旋转水平 ListView 时,左右边缘被切断 - Flutter : When I rotate a horizontal ListView by Transform.rotate, the left and right edges are cut off 旋转小部件后,GestureDetector 不在堆栈上工作 - After Rotate Widget, GestureDetector not working on Stack Flutter Transform.rotate按下按钮时如何使其工作,而不是自动 - Flutter Transform.rotate How to make it work when press the button , not automatically flutter Transform.rotate 的角度值不改变也不更新 state - flutter angle value of Transform.rotate dont change and dont update state 如何基于 GestureDetector 句柄在颤动中旋转图像? - How to rotate image in flutter based on a GestureDetector handle? 旋转 object 并使用 GestureDetector 更新 flutter 中的值 - Rotate an object and update a value in flutter with GestureDetector 如何使用 GestureDetector 平滑旋转 Flutter 中的图像? - How to use GestureDetector to rotate images in Flutter smoothly? FutureBuilder 在 GestureDetector 的 OnTap() 中不工作 - FutureBuilder not working in OnTap() of GestureDetector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM