flutter - flutter 中基于拖动 handle 的旋转图像

标签 flutter dart image-processing

我的最终目标是实现这样的目标:
enter image description here
如您所见,旋转此图像需要拖动 handle 。
我有以下代码:

import 'package:flutter/material.dart';

double ballRadius = 7.5;

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  double _angle = 0.0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: Stack(
            children: [
              Positioned(
                top: 100,
                left: 100,
                child: Transform.rotate(
                  angle: _angle,
                  child: Column(
                    children: [
                      Container(
                        width: 30,
                        height: 30,
                        decoration: BoxDecoration(
                          color: Colors.black,
                          borderRadius: BorderRadius.circular(30),
                        ),
                        child: LayoutBuilder(
                          builder: (context, constraints) {
                            return GestureDetector(
                              behavior: HitTestBehavior.translucent,
                              onPanUpdate: (DragUpdateDetails details) {
                                Offset centerOfGestureDetector = Offset(
                                  constraints.maxWidth / 2,
                                  constraints.maxHeight / 2,
                                );
                                final touchPositionFromCenter =
                                    details.localPosition -
                                        centerOfGestureDetector;
                                print(touchPositionFromCenter.direction);
                                setState(() {
                                  _angle = touchPositionFromCenter.direction;
                                });
                              },
                            );
                          },
                        ),
                      ),
                      Container(
                        height: 30,
                        width: 5,
                        color: Colors.black,
                      ),
                      Container(
                        height: 200,
                        width: 200,
                        color: Colors.red,
                      ),
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

这是工作。但有时它太快或太慢。请帮我解决这个问题。

最佳答案

我对代码做了一些修改,特别是

  • 对待“真实”centerOfGestureDetector作为您要旋转的所有项目的中心
  • 使用 onPanStart 确定和跟踪角度变化, onPanEndonPanUpdate方法

  • import 'package:flutter/material.dart';
    
    double ballRadius = 7.5;
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      double _angle = 0.0;
      double _oldAngle = 0.0;
      double _angleDelta = 0.0;
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: SafeArea(
              child: Stack(
                children: [
                  Positioned(
                    top: 100,
                    left: 100,
                    child: Transform.rotate(
                      angle: _angle,
                      child: Column(
                        children: [
                          Container(
                            width: 30,
                            height: 30,
                            decoration: BoxDecoration(
                              color: Colors.black,
                              borderRadius: BorderRadius.circular(30),
                            ),
                            child: LayoutBuilder(
                              builder: (context, constraints) {
                                //   Offset centerOfGestureDetector = Offset(
                                // constraints.maxWidth / 2, constraints.maxHeight / 2);
                                /**
                               * using center of positioned element instead to better fit the
                               * mental map of the user rotating object.
                               * (height = container height (30) + container height (30) + container height (200)) / 2
                               */
                                Offset centerOfGestureDetector =
                                    Offset(constraints.maxWidth / 2, 130);
                                return GestureDetector(
                                  behavior: HitTestBehavior.translucent,
                                  onPanStart: (details) {
                                    final touchPositionFromCenter =
                                        details.localPosition -
                                            centerOfGestureDetector;
                                    _angleDelta = _oldAngle -
                                        touchPositionFromCenter.direction;
                                  },
                                  onPanEnd: (details) {
                                    setState(
                                      () {
                                        _oldAngle = _angle;
                                      },
                                    );
                                  },
                                  onPanUpdate: (details) {
                                    final touchPositionFromCenter =
                                        details.localPosition -
                                            centerOfGestureDetector;
    
                                    setState(
                                      () {
                                        _angle = touchPositionFromCenter.direction +
                                            _angleDelta;
                                      },
                                    );
                                  },
                                );
                              },
                            ),
                          ),
                          Container(
                            height: 30,
                            width: 5,
                            color: Colors.black,
                          ),
                          Container(
                            height: 200,
                            width: 200,
                            color: Colors.red,
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        );
      }
    }
    
    

    关于flutter - flutter 中基于拖动 handle 的旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64275810/

    相关文章:

    flutter - 向下滑动后窗口小部件不可见

    flutter - 在 Flutter 中计算和显示 map 项总数时出错

    c# - 从图像中清除元数据的简单方法?

    java - 图像处理应用程序的内存使用情况

    image-processing - 识别数据模式的最佳方法是什么,以及了解有关该主题的更多信息的最佳方法是什么?

    Flutter:如何在 RichText 中显示 TextSpan 的工具提示

    dart - 如何在 flutter App 中使用 flutter_map 插件更新 Camera Position

    dart - 我如何才能同时等待嵌套在 map 内的多个 future?

    Flutter - CupertinoActionSheet/CupertinoActionSheetAction 背景颜色在设备上与模拟器中不同

    dart - 如何避免为 dart/flutter 中的每个文件编写导入?