2

I'm trying to finding a way to access to flutter_map controller from outside the class the idea is that i have markers on the map screen . when you tap on the marker the map should move so that spicefic marker be on the middle

the Problem that the marker class Initial in different class so i can not access to map control immediately

how can i make the map move ?

  ./screens/homeScreen.dart

  class HomeScreen extends StatefulWidget {
  static const routeName = '/home';
  @override
  _HomeScreenState createState() => _HomeScreenState();
  }

  class _HomeScreenState extends State<HomeScreen> {
  Widget build(BuildContext context) {
        return  Scaffold(
            body:FlutterMap(
                          mapController: _mapController,
                          MarkerClusterLayerOptions(
                            onMarkerTap: (_) {
                            },                           //  Cluster tab
                            markers:externalReports.items // get the markers list 
                            centerMarkerOnClick: true,    // Cluster center
                              ....),);}}




./externalReports.dart  // where The Items Initial

     _items.add(
          Marker(
              optionalDataContainer: data,
              width: 45.0,
              height: 45.0,
              point: LatLng(
                latitude,
                longitude,),
              builder: (context) => Container(
                        child: Container(
                          height: 45,
                          width: 45,
                          child: GestureDetector(
                            onTap: () {
 
                       // _mapController.move(LatLng(latitude,longitude), 15.0);

                            }))))
Dennis Kozevnikoff
  • 2,078
  • 3
  • 19
  • 29
M.Akyuzlu
  • 185
  • 2
  • 13

2 Answers2

0

The most simple option that I see is to transform it into a global instance. (or you can make it static).

But I must say that those options are not very 'beautiful'...

Gaspard Merten
  • 1,063
  • 7
  • 14
0

You can expose a onClick callback for Marker

Something like this:

class YourMarker extends StatelessWidget {
  final double latitude;
  final double longitude;
  final Function(double, double) onClick;
  YourMarker({
    this.latitude,
    this.longitude,
    this.onClick,
  });

  @override
  Widget build(BuildContext context) {
    return Marker(
      optionalDataContainer: data,
      width: 45.0,
      height: 45.0,
      point: LatLng(
        latitude,
        longitude,
      ),
      builder: (context) => Container(
        child: Container(
          height: 45,
          width: 45,
          child: GestureDetector(
            onTap: 
            () {
              onClick(latitude, longitude);
              // _mapController.move(LatLng(latitude,longitude), 15.0);
            },
          ),
        ),
      ),
    );
  }
}
JerryZhou
  • 4,566
  • 3
  • 37
  • 60