Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 颤振:获取手势检测器的局部位置_Dart_Flutter - Fatal编程技术网

Dart 颤振:获取手势检测器的局部位置

Dart 颤振:获取手势检测器的局部位置,dart,flutter,Dart,Flutter,我无法使用pan更新在手势检测器小部件上获得真实的本地位置 这是我的示例代码 new Center( child new Container( height: 150.0, width: 150.0, decoration: new BoxDecoration( borderRadius: new BorderRadius.circular(5.0), color: Colors.white,

我无法使用pan更新在手势检测器小部件上获得真实的本地位置

这是我的示例代码

new Center(    
  child new Container(
       height: 150.0,
       width: 150.0,
       decoration: new BoxDecoration(
         borderRadius: new BorderRadius.circular(5.0),
         color: Colors.white,
      ),
      child: new GestureDetector(
      onPanUpdate: (details) => onPanUpdate(context, details),
      child: new CustomPaint(
         painter: new MyPainter(
             points: points,
             limitBottom:height - 125.0,
             limitTop: 10.0,
             limitLeft: 5.0,
             limitRight: width - 55.0
         ),
       ),
    ),
  ),  
)

当我打印全局位置和局部位置的偏移量时,它们的值是相同的。结果,它被画在我的容器小部件的外面。有什么我错过了获得本地职位的机会吗

我假设您正在使用类似的方法来获取本地位置:

RenderBox getBox = context.findRenderObject();
var local = getBox.globalToLocal(start.globalPosition);
执行此操作后获得错误偏移量的原因与用于查找本地位置的
上下文有关

如果您使用的是整个小部件的上下文,那么实际上您所做的就是计算整个小部件内的偏移量。i、 e

YourWidget <-- context
  Center
   Container
     GestureDetector
     ...

YourWidget用
无状态widget
包装它对我来说很好,多亏了@rmtmckenzie

这是我的代码:

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(),
      body: _Foo(),
    );
  }
}

class _Foo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onLongPressDragStart: (details) => _onLongPressDragStart(details, context),
      onLongPressDragUpdate: (details) => _onLongPressDragUpdate(details, context),
      onLongPressDragUp: (details) => _onLongPressDragUp(details, context),
      child: FlareActor(
        "assets/test.flr",
        alignment: Alignment.center,
        fit: BoxFit.contain,
      ),
    );
  }

  void _onLongPressDragStart(GestureLongPressDragStartDetails details, BuildContext context) {
    print('_onLongPressDragStart details: ${details.globalPosition}');
  }

  void _onLongPressDragUpdate(GestureLongPressDragUpdateDetails details, BuildContext context) {
    var localTouchPosition = (context.findRenderObject() as RenderBox).globalToLocal(details.globalPosition);
    print('_onLongPressDragUpdate details: ${details.globalPosition} - localTouchPosition: $localTouchPosition');
  }

  void _onLongPressDragUp(GestureLongPressDragUpDetails details, BuildContext context) {
    print('_onLongPressDragUp details: ${details.globalPosition}');
  }
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:Colors.black,
appBar:appBar(),
正文:_Foo(),
);
}
}
类Foo扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回手势检测器(
onLongPressDragStart:(详细信息)=>onLongPressDragStart(详细信息,上下文),
onLongPressDragUpdate:(详细信息)=>onLongPressDragUpdate(详细信息、上下文),
onLongPressDragUp:(详细信息)=>onLongPressDragUp(详细信息,上下文),
儿童:FlareActor(
“资产/测试.flr”,
对齐:对齐.center,
适合:BoxFit.contain,
),
);
}
void _onLongPressDragStart(GestureLongPressDragStart详细信息,构建上下文){
打印(“仅限长按DragStart详细信息:${details.globalPosition}”);
}
void _onLongPressDragUpdate(GestureLongPressDragUpdate详细信息,构建上下文){
var localTouchPosition=(context.findenderobject()作为RenderBox.globalToLocal(details.globalPosition);
打印(“\u onLongPressDragUpdate详细信息:${details.globalPosition}-localTouchPosition:$localTouchPosition”);
}
void _onLongPressDragUp(手势LongPressDragUp详细信息,构建上下文){
打印(“\u onLongPressDragUp details:${details.globalPosition}”);
}
}

重要的事情发生在
\u Foo
类中。

我的“点击位置”偏移了,这是一个问题。我相信这和上面提到的是同一个问题。通过阅读有关键的内容并在此处找到小部件的位置/大小,我能够解决类似的问题:

因此,我不必像上面提到的那样创建另一个小部件或布局生成器,但我可以添加一个全局键,然后通过使用键中的“currentContext”执行以下操作来获取点击的位置。很酷

  void _handleLongPress(LongPressStartDetails details) {
    final RenderBox referenceBox = _keyRink.currentContext.findRenderObject();
    var x = referenceBox.globalToLocal(details.globalPosition);
    if (x == tapPosition) {
      Vibration.vibrate(duration: 50);
      setState(() {
        _shots[_homeShotCount] = tapPosition;
      });
    }
  }
这是我的主脚手架,你可以看到我加钥匙的地方

   return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: GestureDetector(
        key: _keyRink,
        onTapDown: _getTapPosition,
        onLongPressStart: _handleLongPress,
        child: Stack(
          children: <Widget>[
            AnimatedOpacity(
              duration: Duration(milliseconds: 2500),
              opacity: _rinkOpacity,
              child: Center(
                child: Image.asset('assets/rink.png',
                    width: size.width, height: size.height, fit: BoxFit.fill),
              ),
            ),
            Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Transform.rotate(
                    angle: animationRotate.value,
                    child: Transform.scale(
                        scale: animationDrop.value,
                        child: FlatButton(
                            onPressed: null,
                            padding: EdgeInsets.all(0.0),
                            child: Image.asset('assets/puck.png'))),
                  )
                ],
              ),
            ),
            CustomPaint(
              painter: ShapesPainter(),
              child: FractionallySizedBox(heightFactor: 1.0,widthFactor: 1.0,),
            ),
          ],
        ),
      ),
      drawer: Drawer(
        child: drawerItems,
      ),
    );
  }
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:手势检测器(
钥匙:_keyRink,
onTapDown:_getapposition,
仅长按开始:_手动长按,
子:堆栈(
儿童:[
动产能力(
持续时间:持续时间(毫秒:2500),
不透明度:_rinkOpacity,
儿童:中心(
子级:Image.asset('assets/rink.png',
宽度:size.width,height:size.height,fit:BoxFit.fill),
),
),
居中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
变换。旋转(
角度:animationRotate.value,
子:Transform.scale(
缩放:animationDrop.value,
孩子:扁平按钮(
onPressed:null,
填充:所有边缘设置(0.0),
子项:Image.asset('assets/puck.png')),
)
],
),
),
定制油漆(
画家:ShapePaint(),
子项:部分物理化数据库(高度因子:1.0,宽度因子:1.0,),
),
],
),
),
抽屉(
孩子:是的,
),
);
}

如果您将代码包含在实际检查本地和全球位置的地方,这将非常有用。但是我可能仍然可以告诉你发生了什么。当我打印全局位置和局部位置的偏移量时,我得到了它们的相同值。我写的这些值是一样的,因为在泛更新事件中,我将全局的东西转换为局部的东西。它仍然具有相同的值。您是否将您的手势检测器包装在生成器中,或者为其创建了一个新的无状态小部件?因为根据我的解释,这是预期的行为。如果您确实更改了某些内容,但仍然存在相同的问题,请使用新代码更新您的问题。谢谢您非常感谢!
   return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: GestureDetector(
        key: _keyRink,
        onTapDown: _getTapPosition,
        onLongPressStart: _handleLongPress,
        child: Stack(
          children: <Widget>[
            AnimatedOpacity(
              duration: Duration(milliseconds: 2500),
              opacity: _rinkOpacity,
              child: Center(
                child: Image.asset('assets/rink.png',
                    width: size.width, height: size.height, fit: BoxFit.fill),
              ),
            ),
            Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Transform.rotate(
                    angle: animationRotate.value,
                    child: Transform.scale(
                        scale: animationDrop.value,
                        child: FlatButton(
                            onPressed: null,
                            padding: EdgeInsets.all(0.0),
                            child: Image.asset('assets/puck.png'))),
                  )
                ],
              ),
            ),
            CustomPaint(
              painter: ShapesPainter(),
              child: FractionallySizedBox(heightFactor: 1.0,widthFactor: 1.0,),
            ),
          ],
        ),
      ),
      drawer: Drawer(
        child: drawerItems,
      ),
    );
  }