首页 文章

如何在flutter(dart)中访问其他类方法?

提问于
浏览
2

我在脚手架中使用了颤振贴图,并且在scaffold widget类中有一个名为“showMyBottomSheet”的方法 . 用于在点击 Map 并打开底部工作表时执行此方法的 Map .

但是现在我已经将这两个文件分开了,我无法从 Map 类访问脚手架状态小部件中的“showMyBottomSheet”方法 .

我该怎么做?我不应该将它们分开吗?

我的主页 - 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 小部件 - 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']),
        ],
      ),
    );
  }
}

1 回答

  • 0

    首先,并不是任何以下划线开头的方法在Dart中都会被视为私有 . look here .

    在这种情况下,您应该将该方法传递给子窗口小部件并稍后调用它 . 检查这个例子:

    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),
            ],
          ),
        );
      }
    }
    

    并在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']),
            ],
          ),
        );
      }
    }
    

相关问题