我在脚手架中使用了颤动贴图,并且在脚手架窗口小部件类中有一个名为"showMyBottomSheet“的方法。当点击地图并打开底部工作表时用于执行此方法的地图。
但是现在我已经将这两个文件分离到单独的文件中,我不能从map类访问scaffold有状态小部件中的"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']),
],
),
);
}
}发布于 2018-08-20 16:26:47
首先,请注意,任何以下划线开头的方法在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']),
],
),
);
}
}https://stackoverflow.com/questions/51926349
复制相似问题