Android 在flatter中,如何将一个小部件中的任意点与另一个小部件中的任意点对齐?

Android 在flatter中,如何将一个小部件中的任意点与另一个小部件中的任意点对齐?,android,flutter,dart,Android,Flutter,Dart,我正在Flitter中开发一个应用程序,它可以拍摄谷歌地图的快照(这样我就可以离线显示图像),并在上面显示一个位置图标 除了地图快照,我还捕获了地图四个角的纬度和经度。因此,通过当前的GPS读数,我可以在地图上标出相对位置,以显示位置图标 问题是我希望图标的尖头部分位于GPS位置。这意味着图标底部边缘的中点 目前,我正在使用一个堆栈,将地图图像作为第一层,并将图标堆叠在顶部,使用分馏偏移小部件定位: // Calculate the relative location of the icon

我正在Flitter中开发一个应用程序,它可以拍摄谷歌地图的快照(这样我就可以离线显示图像),并在上面显示一个位置图标

除了地图快照,我还捕获了地图四个角的纬度和经度。因此,通过当前的GPS读数,我可以在地图上标出相对位置,以显示位置图标

问题是我希望图标的尖头部分位于GPS位置。这意味着图标底部边缘的中点

目前,我正在使用一个堆栈,将地图图像作为第一层,并将图标堆叠在顶部,使用分馏偏移小部件定位:

  // Calculate the relative location of the icon over the map
  // by finding the ratio of our current location in the
  // map
  double locationMarkerLeft = (_currentLocation.longitude - bounds.southwest.longitude) /
        (bounds.northeast.longitude - bounds.southwest.longitude);
  double locationMarkerTop = (_currentLocation.latitude - bounds.northeast.latitude) /
        (bounds.southwest.latitude - bounds.northeast.latitude);

  return Stack(children: <Widget>[
    Image.memory(_mapImage),
    Align(child: Icon(Icons.location_on),
        alignment: FractionalOffset(locationMarkerLeft, locationMarkerTop)
    )
  ]);
//计算图标在地图上的相对位置
//通过查找当前位置在
//地图
double locationMarkerLeft=(_currentLocation.longitude-bounds.soutwest.longitude)/
(bounds.northeast.longitude-bounds.soutwest.longitude);
double locationMarkerTop=(_currentLocation.latitude-bounds.northeast.latitude)/
(bounds.southwest.latitude-bounds.Northwest.latitude);
返回堆栈(子级:[
内存(_-mapImage),
对齐(子:图标(图标位置在上),
对齐:分数偏移(位置标记左、位置标记顶)
)
]);
我的理解是,分数偏移将相同的相对偏移应用于父对象和子对象。因此,虽然这大致定位了图标,但并不精确

那么,如何将图标底部边缘的中点定位在地图图像顶部的特定点上


谢谢。

最简单的解决方案是使用图标的大小(默认为24)进行计算

下面是您修改的代码:

    // Calculate the relative location of the icon over the map
    // by finding the ratio of our current location in the
    // map
    double locationMarkerLeft = (_currentLocation.longitude - 24/2 - bounds.southwest.longitude) /
        (bounds.northeast.longitude - bounds.southwest.longitude);
    double locationMarkerTop = (_currentLocation.latitude - bounds.northeast.latitude) /
        (bounds.southwest.latitude - bounds.northeast.latitude);

    return Stack(children: <Widget>[
      Image.memory(_mapImage),
      Align(child: Icon(Icons.location_on),
          alignment: FractionalOffset(locationMarkerLeft, locationMarkerTop)
      )
    ]);
//计算图标在地图上的相对位置
//通过查找当前位置在
//地图
double locationMarkerLeft=(_currentLocation.longitude-24/2-bounds.soutwest.longitude)/
(bounds.northeast.longitude-bounds.soutwest.longitude);
double locationMarkerTop=(_currentLocation.latitude-bounds.northeast.latitude)/
(bounds.southwest.latitude-bounds.Northwest.latitude);
返回堆栈(子级:[
内存(_-mapImage),
对齐(子:图标(图标位置在上),
对齐:分数偏移(位置标记左、位置标记顶)
)
]);

但是24/2除以地图的经度。结果毫无意义。如果我能得到小部件的大小,我就能计算出一些有用的东西。但在构建小部件时,您无法获得这些信息。