【发布时间】:2020-02-13 06:04:04
【问题描述】:
我已经实现了google_maps_fltuter,每当用户点击边框内任意位置的不同路线时,我需要用新坐标更新地图上的路线(见下图)。最初渲染视图时,地图会安装第一条路线的坐标,但我还需要能够显示不同的路线。
我有一个 GestureDetector 小部件来允许这种情况发生,但它不会触发 RouteMap 类。
class _PlanJourney extends State<PlanJourney> {
...
GestureDetector(
onTap: () => RouteMap(from, to, routeCoordinates, i),
child: Container(...)
)
...
}
这是根据传递给类的坐标创建地图的类。
class RouteMap extends StatefulWidget {
final fromCoordinate;
final toCoordinate;
final routeCoordinates;
final routeIndex;
RouteMap(this.fromCoordinate, this.toCoordinate, this.routeCoordinates, this.routeIndex);
@override
_RouteMap createState() => _RouteMap(fromCoordinate, toCoordinate, routeCoordinates, routeIndex);
}
class _RouteMap extends State<RouteMap> {
final fromCoordinate;
final toCoordinate;
final routeCoordinates;
final routeIndex;
_RouteMap(this.fromCoordinate, this.toCoordinate, this.routeCoordinates, this.routeIndex);
Completer<GoogleMapController> _controller = Completer();
GoogleMapController mapController;
LatLng _setCenter() {
return LatLng(double.parse(this.toCoordinate.split(',').toList()[1]),
double.parse(this.toCoordinate.split(',').toList()[0]));
}
final Set<Marker> _markers = {};
Map<PolylineId, Polyline> _mapPolylines = {};
int _polylineIdCounter = 1;
void _onMapCreated(GoogleMapController controller) {
_setCenter();
mapController = controller;
_controller.complete(controller);
// the coordinates coming from the API are in LonLat format
// Google map only accepts LatLon format so we have to swap the around
LatLng latLng_1 = LatLng(double.parse(this.fromCoordinate.split(',').toList()[1]),
double.parse(this.fromCoordinate.split(',').toList()[0]));
LatLng latLng_2 = LatLng(double.parse(this.toCoordinate.split(',').toList()[1]),
double.parse(this.toCoordinate.split(',').toList()[0]));
setState(() {
_markers.clear();
addMarker(latLng_1, "Title", "description");
addMarker(latLng_2, "Title", "description");
});
_add(routeIndex);
}
...
更新
我稍微更改了代码,但问题仍然存在。 SetState 更新了我需要发送到 RouteMap 类的值,但类本身没有更新。
class _PlanJourney extends State<PlanJourney> {
...
GestureDetector(
onTap: () {
setState(() {
routeIndex = i; // this is the value I need to send to RouteMap class
// print(routeIndex) -> the routeIndex changes
});
},
child: Container(...) // *note the map is not in this container
)
...
}
...
// the map is initially created here
Container(
margin: EdgeInsets.only(bottom: 10),
height: 400,
child: RouteMap(widget.from, widget.to, routeCoordinates[routeIndex], journeyData['routes'][routeIndex])
)
...
...
class RouteMap extends StatefulWidget {
final fromCoordinate;
final toCoordinate;
final routeCoordinates;
final routeIndex;
RouteMap(this.fromCoordinate, this.toCoordinate, this.routeCoordinates, this.routeIndex);
@override
_RouteMap createState() => _RouteMap();
}
class _RouteMap extends State<RouteMap> {
Completer<GoogleMapController> _controller = Completer();
GoogleMapController mapController;
final Set<Marker> _markers = {};
Map<PolylineId, Polyline> _mapPolylines = {};
int _polylineIdCounter = 1;
...
}
...
【问题讨论】:
-
RouteMap的父级应该是一个StatefulWidget,它将坐标保存在其状态并将它们传递给RouteMap。每当您点击卡片时,必须更新该州的坐标,然后RouteMap也会更新。 -
使用 Inkwell 或 Gesture Detector 然后实现他们的 onTap() 方法并将你的变量放在 setState() 中。当值改变状态自动改变
-
@HugoPassos setstate 会更新值,但视图不会改变。你能看看我添加的更新部分吗?谢谢
标签: flutter dart state statefulwidget google-maps-flutter