简体   繁体   English

如何在颤振中使堆栈响应?

[英]How to make a Stack responsive in flutter?

I'd like to implement a geografic map with a position indicator using the Stack widget in flutter.我想使用 flutter 中的 Stack 小部件实现带有位置指示器的地理地图。 My problem is that when I want to indicate a point P of my map, I specify the coordinates (x,y) but these coordinates are valid only when use a phone with a certain dimensions, but if I use a phone with a different display dimensions it will also changes the position of my position indicator that will not indicate my point P. My question is: using the Stack widget is correct for this type of problem?我的问题是,当我想指示地图上的一个点 P 时,我指定坐标 (x,y) 但这些坐标仅在使用具有特定尺寸的手机时才有效,但如果我使用具有不同显示屏的手机尺寸它也会改变我的位置指示器的位置,而不会指示我的点 P。我的问题是:使用 Stack 小部件对此类问题是否正确? If not what can I use?如果不是,我可以使用什么?

Here a snippet of my code:这是我的代码片段:

          Stack(
                children: [
                  Image.asset(
                    'assets/images/map.png',
                     height: mediaQuery.size.height * 0.5,
                     width: mediaQuery.size.width,
                  ),
                  if (x != null && y != null)
                    Align(
                      heightFactor: 2,
                      widthFactor: 2,

                      alignment: Alignment(x, y),
                      child: Image.asset(
                        'assets/images/picker.png',
                        scale: 1.4,
                      ),
                    ),
                ],

You need to put in a scaling factor with the new X and Y coordinates that have been scaled from the original image.您需要使用从原始图像缩放的新 X 和 Y 坐标输入缩放因子。 Also, I tend to think that using a Positioned widget is better than an Align widget when you're using a stack.此外,我倾向于认为,当您使用堆栈时,使用 Positioned 小部件比使用 Align 小部件更好。

The scaled X and Y values would look something like this:缩放后的 X 和 Y 值将如下所示:

double scaleX(originalx, originalwidth, currentwidth){
  return(originalx * currentwidth / originalwidth);
}

double scaleY(originaly, originalheight, currentheight){
  return(originaly * currentheight / originalheight);
}

You can then use those scaled values just like the original x and y values on the now-scaled map.然后,您可以像现在缩放的地图上的原始 x 和 y 值一样使用这些缩放值。

In order to make it more clear, I created a dartpad here so you can play around with it: http://dartpad.dev/34ec25e551a58127b8635fc56744ff29为了更清楚,我在这里创建了一个飞镖板,以便您可以使用它: http ://dartpad.dev/34ec25e551a58127b8635fc56744ff29

The green container is meant to simulate your map, and the yellow dot your desired point.绿色容器用于模拟您的地图,黄色圆点是您想要的点。 You can change the scalingFactorHeight and scalingFactorWidth in the code in order to simulate different screen sizes.您可以更改代码中的 scalingFactorHeight 和 scalingFactorWidth 以模拟不同的屏幕尺寸。 You'll see that the yellow dot stays at the center, even though the x and y values are constant.您会看到黄点位于中心,即使 x 和 y 值不变。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM