如何获取当前小部件的偏移量 [英] How to get offset of a current widget
问题描述
每当用户按下屏幕时,我都试图绘制一个小部件。
当前,我是通过存储小部件列表来完成此操作的,当ontapup在手势上触发时,我将其添加到小部件列表中。
I am trying to draw a widget whenever a user presses the screen. Currently I am doing this by storing a list of widgets and when ontapup is fired on the gesture i am adding to a list of widgets.
Widget build(BuildContext context) {
Widget draw = new Text("A");
List<Widget> children = new List<Widget>();
return new Scaffold(
appBar: new AppBar(
title: const Text('Heading'),
leading: new Icon(Icons.question_answer),
),
body: new GestureDetector(
onTapUp: (details) {
setState(() {
children.add(new Positioned(
left: details.globalPosition.dx,
top: details.globalPosition.dy,
child: draw,
));
});
},
child: new Stack(children: children)
...
所以我的代码正在工作,我正在绘制小部件,当我单击但我的问题是,在添加新的Positioned()进行堆叠时,位置基于不包含appbar偏移量的屏幕。是否可以获取堆叠的初始x / y位置?一种获取appbars高度的方法?我如何获取一个widget的位置或高度/宽度?
So my code is working I am drawing the widget when I click but my problem is that when adding the new Positioned() to stack the position is based on the screen which does not include the appbar offset. Is there a way to get the stacks initial x/y position? Or is there a way to get the appbars height? How do I get a widgets position or height/width?
推荐答案
有我需要的相同问题ed创建自己的窗口小部件并使用
Ok for anyone else who has the same issue I needed to create my own widget and use
context.findRenderObject()
和
globalToLocal()
在一个解决方案中,我只想将FYI全局化为本地是行不通的。
Just FYI global to local did not work while in the one solution I needed to make it its own widget.
这篇关于如何获取当前小部件的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!