0

所以这就是我想要实现的目标:我希望当我点击我的 SearchDelegate 的建议时,它会关闭搜索并将我在地图上移动到搜索的位置

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 是我尝试传递给 SearchDelegate 以实现我想要的功能

我的地图如下所示:

@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 但从未发现如何使其工作,谢谢您的时间。

4

1 回答 1

0

我终于找到了解决方案,我只需要在 Map 状态下声明函数,并使用全局键从父窗口小部件访问它。

于 2021-06-15T16:08:52.323 回答