Moein Hosseini
Moein Hosseini

Reputation: 1596

How to access other classes method in flutter (dart)?

I used flutter map in a scaffold and I have a method named "showMyBottomSheet" in scaffold widget class. the map used to execute this method when the map is tapped and the bottom sheet opened.

but now that I have separated these two in separate files, I can not access the "showMyBottomSheet" method in scaffold stateful widget from the map class.

how can I do it? shouldn't I separate them?

My Home Page - home.dart

class MyHomePage extends StatefulWidget {
  static const String route = '/';

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const String route = '/';

  // Bottom sheet
  final _scaffoldKey = new GlobalKey<ScaffoldState>();

  dynamic showMyBottomSheet(LatLng latlang) {
    _scaffoldKey.currentState.showBottomSheet((context) {
      return new CustomBottomSheet();
    });
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
      drawer: buildDrawer(context, route),
      body: new Column(
        children: [
          new CustomMap(),
        ],
      ),
    );
  }
}

Map widget - flutterMap.dart

class Cuenter code herestomMap extends StatefulWidget {
  @override
  _CustomMapState createState() => new _CustomMapState();
}

class _CustomMapState extends State<CustomMap> {
  var markers = <Marker>[];

  @override
  Widget build(BuildContext context) {
    return new Flexible(
      child: new FlutterMap(
        options: new MapOptions(
            center: new LatLng(51.25, 30.5),
            zoom: 15.0,
            onTap: // I want to call showMyBottomSheet method (from home page) here
        ),
        layers: [
          new TileLayerOptions(
              urlTemplate:
              "https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png",
              subdomains: ['a', 'b', 'c']),
        ],
      ),
    );
  }
}

Upvotes: 4

Views: 7122

Answers (1)

Yamin
Yamin

Reputation: 3008

First, note that any method that starts with an underscore will be considered private in the Dart. look here .

In this case, you should pass the method to the child widget and call it later. check this example:

class MyHomePage extends StatefulWidget {
  static const String route = '/';

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const String route = '/';

  // Bottom sheet
  final _scaffoldKey = new GlobalKey<ScaffoldState>();

  dynamic showMyBottomSheet(LatLng latlang) {
    _scaffoldKey.currentState.showBottomSheet((context) {
      return new CustomBottomSheet();
    });
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
      drawer: buildDrawer(context, route),
      body: new Column(
        children: [
          new CustomMap(onMapTap: showMyBottomSheet),
        ],
      ),
    );
  }
}

And in the flutterMap.dart:

class CustomMap extends StatefulWidget {
  Function onMapTap;
  CustomMap({this.onMapTap});

  @override
  _CustomMapState createState() => new _CustomMapState();
}

class _CustomMapState extends State<CustomMap> {
  var markers = <Marker>[];

  @override
  Widget build(BuildContext context) {
    return new Flexible(
      child: new FlutterMap(
        options: new MapOptions(
            center: new LatLng(51.25, 30.5),
            zoom: 15.0,
            onTap: (){
              widget.onMapTap(LatLng(34.12312,56.1323));
            }
        ),
        layers: [
          new TileLayerOptions(
              urlTemplate:
              "https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png",
              subdomains: ['a', 'b', 'c']),
        ],
      ),
    );
  }
}

Upvotes: 3

Related Questions