我在脚手架中使用了抖动映射,并且在脚手架小部件类中有一个名为“ showMyBottomSheet”的方法。点击地图并打开底部工作表时,用于执行此方法的地图。
但是现在我已经将这两个文件分开了,我无法从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(),
],
),
);
}
}
地图小部件-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']),
],
),
);
}
}
答案 0 :(得分:1)
首先,并不是所有以下划线开头的方法在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']),
],
),
);
}
}