[英]How to call the Map Controller's move method of Flutter Map from its parent widget
所以這就是我想要實現的目標:我希望當我點擊我的 SearchDelegate 的建議時,它會關閉搜索並將我從 map 移動到搜索的 position
return FutureBuilder(
future: chargerMonument(),
builder: (context, snaphot) {
if (snaphot.hasData) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: Icon(Icons.search),
onPressed: () {
showSearch(
context: context,
delegate: CustomSearchDelegate(
monuments, updatelocation));
}),
],
key: _scaffoldKey,
body: Map(),
}
updatelocation 是 function 我嘗試傳遞給 SearchDelegate 以實現我想要的
我的 map 看起來像這樣:
@override
Widget build(BuildContext context)
{
FutureBuilder(
future: test(),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
List<Widget> children;
if (snapshot.hasData) {
children = [
Flexible(
child: FlutterMap(
mapController: mapController,
children: [
LocationMarkerLayerWidget(
plugin: LocationMarkerPlugin(
centerCurrentLocationStream:
_centerCurrentLocationStreamController
.stream,
//_centerOnLocationUpdate
centerOnLocationUpdate:
_centerOnLocationUpdate),
)
],
options: MapOptions(
onPositionChanged:
(MapPosition position, bool hasGesture) {
if (hasGesture) {
setState(() => _centerOnLocationUpdate =
CenterOnLocationUpdate.never);
}
},
plugins: [
PopupMarkerPlugin(),
ZoomButtonsPlugin(),
LocationPlugin(),
TappablePolylineMapPlugin()
],
interactiveFlags:
InteractiveFlag.all & ~InteractiveFlag.rotate,
zoom: 18.0,
center: LatLng(0, 0),
onTap: (_) => _popupLayerController
.hidePopup(), // Hide popup when the map is tapped.
),
layers: [
TileLayerOptions(
urlTemplate:
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c'],
),
TappablePolylineLayerOptions(
// Will only render visible polylines, increasing performance
polylineCulling: true,
pointerDistanceTolerance: 20,
polylines: [
TaggedPolyline(
points: polylines,
isDotted: true,
color: Colors.blue[300],
strokeWidth: 4.0)
],
onTap: (TaggedPolyline polyline) async {
await showDialog(
context: context,
builder: (_) => new AlertDialog(
title: Text(destination),
content: Text("Vous etes à " +
distance.round().toString() +
"km de votre distination !"),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("D'accord !")),
TextButton(
onPressed: () {
setState(() {
polylines = [];
refresh = 360000;
});
Navigator.of(context).pop();
},
child:
Text("Effacer ce chemin"))
]));
},
onMiss: () {
print('No polyline was tapped');
}),
PopupMarkerLayerOptions(
markers: _markers,
popupSnap: PopupSnap.markerTop,
popupController: _popupLayerController,
popupBuilder: (BuildContext _, Marker marker) =>
ExamplePopup(
marker, marker.point, drawpolyline)),
ZoomButtonsPluginOption(
minZoom: 4,
maxZoom: 22,
mini: true,
padding: 10,
alignment: Alignment.bottomRight),
LocationOptions(onLocationUpdate: (LatLngData ld) {
}, onLocationRequested: (LatLngData ld) {
if (ld == null) {
return;
}
mapController.move(ld.location, 16.0);
},
/* onLocationUpdate: (LatLngData ld) {},
onLocationRequested: (LatLngData ld) {
if (ld == null || ld.location == null) {
return;
}
/* return;
}
mapController?.move(ld.location, 16.0);*/
mapController.onReady.then((result) {
print("I AM READY");
mapController.move(ld.location, 16);
});
*/
buttonBuilder: (BuildContext context,
ValueNotifier<LocationServiceStatus> status,
Function onPressed) {
return Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.only(
bottom: 14.0, right: 60.0),
child: Container(
height: 38,
width: 38,
child: FloatingActionButton(
backgroundColor: Colors.grey,
child: ValueListenableBuilder<
LocationServiceStatus>(
valueListenable: status,
builder: (BuildContext context,
LocationServiceStatus value,
Widget child) {
switch (value) {
case LocationServiceStatus
.disabled:
case LocationServiceStatus
.permissionDenied:
case LocationServiceStatus
.unsubscribed:
return const Icon(
Icons.location_disabled,
color: Colors.white,
);
break;
default:
return const Icon(
Icons.my_location,
color: Colors.white,
);
break;
}
}),
onPressed: () {
setState(() =>
_centerOnLocationUpdate =
CenterOnLocationUpdate
.always);
_centerCurrentLocationStreamController
.add(18);
}),
)));
})
],
),
),
];
} else if (snapshot.hasError) {
children = <Widget>[
Icon(
Icons.error_outline,
color: Colors.red,
size: 60,
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Text('Error: ${snapshot.error}'),
)
];
} else {
children = <Widget>[
SizedBox(
child: CircularProgressIndicator(),
width: 60,
height: 60,
)
];
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: children,
),
);
});
});
}
我想要 updatelocation 做的方法是這樣的:
void update(double lat,double long){
setState(() {
_centerOnLocationUpdate = CenterOnLocationUpdate.never;
});
mapController.move(LatLng(lat,long), 18);
}
我如何從父級更改子級值 _centerOnLocationUpdate 並調用 mapController 移動
我嘗試使用 ValueListenableBuilder 但從未發現如何使其工作,謝謝您的時間。
我終於找到了解決方案,我只需要在 Map state 中聲明 function,並使用全局密鑰從父窗口小部件訪問它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.