英文:
How to get width of GestureDetector in flutter?
问题
我正在制作一个Web应用,它看起来像这样:
Web应用的图像以更好地理解,您可以选择您的热水应该有多热(只是一个愚蠢的示例,用来说明我的问题)。
用于实现这一目标的代码如下:
Scaffold(
backgroundColor: Colors.white,
body: Center(
child: GestureDetector(
onTapDown: (val) {
print(val.localPosition.dx);
},
child: Container(
decoration: const BoxDecoration(gradient: LinearGradient(colors: [Colors.blue, Colors.red])),
height: 100,
margin: const EdgeInsets.all(20),
)),
),
);
我试图检测用户从左边缘按下手势检测器的位置有多远。我添加了一个打印语句,它告诉我像素的数量。
但我需要的是像百分比一样的东西,例如,当用户按下容器的中间部分时,它应该返回50%或0.5之类的值。只有像素的数量不能帮助我进一步设置温度。由于容器的宽度会根据窗口大小而改变,所以我不能简单地将位置的dx值除以容器的宽度。
有什么解决方法吗?
英文:
I'm doing a web-app and it looks like this:
Image of the web app for better understanding, where you can choose how hot your water should be (just a silly example to illustrate my problem).
the code to that looks like the following:
Scaffold(
backgroundColor: Colors.white,
body: Center(
child: GestureDetector(
onTapDown: (val) {
print(val.localPosition.dx);
},
child: Container(
decoration: const BoxDecoration(gradient: LinearGradient(colors: [Colors.blue, Colors.red])),
height: 100,
margin: const EdgeInsets.all(20),
)),
),
);
I'm trying to detect how fare from the left edge the user has pressed the gesture Detector. I add a print statement and it tells me the number of pixels.
But what i need is something like a percentage, for example when the user presses the Container in the middle it should return 50% or 0.5 or something like that. Just the numbers of pixels doesnt help me to further set the temperature. Since the width of the container is changing by the window size, I can not simply divide the dx value of the positoin by the width of the container.
Any ideas how to solve that?
答案1
得分: 0
如果你想创建一个与“选择值在x和y之间”相关的漂亮用户界面,应该使用滑块(Slider)而不是GestureDetector
- 这样会更容易。如果你想获取屏幕的宽度,可以使用MediaQuery.of(context).size.width
,GestureDetector
的大小也等于其子部件的大小(想象一下,如果你移除GestureDetector
,那么Container
的大小是多少)。如果有内边距(padding),可以将屏幕宽度减去内边距...
英文:
For me, if you want to create a beautiful UI relates to "choose value between x & y", you should use slider instead of GestureDetector
- much easier. If you want to get width of screen, use MediaQuery.of(context).size.width
, the size of GestureDetector
is also the size of it child (well, imagine you remove GestureDetector
, what is that Container
size). If you have padding, take the screen width minus the padding...
答案2
得分: 0
只需将所有内容放入新的小部件中,并使用构建方法的构建上下文:
class Thermometer extends StatelessWidget {
const Thermometer({super.key});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTapDown: (x) {
print(context.size?.width);
print("--> ${100 * x.localPosition.dx / (context.size?.width ?? 1)}%");
},
child: Container(
decoration: const BoxDecoration(gradient: LinearGradient(colors: [Colors.blue, Colors.red])),
height: 100,
));
}
}
这会在您点击它时打印类似以下内容:
779.111083984375
--> 20.764405690078366%
英文:
Just put everything in a new widget and use the build context of the build method:
class Thermometer extends StatelessWidget {
const Thermometer({super.key});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTapDown: (x) {
print(context.size?.width);
print("--> ${100 * x.localPosition.dx / (context.size?.width ?? 1)}%");
},
child: Container(
decoration: const BoxDecoration(gradient: LinearGradient(colors: [Colors.blue, Colors.red])),
height: 100,
));
}
}
That prints something like
779.111083984375
--> 20.764405690078366%
when you tap on it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论