gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:44:03 25 4
gpt4 key购买 nike

我在脚手架中使用了 flutter map,并且在脚手架小部件类中有一个名为“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']),
],
),
);
}
}

最佳答案

首先,请注意,任何以下划线开头的方法在 Dart 中都将被视为私有(private)方法。 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']),
],
),
);
}
}

关于class - 如何在 flutter (dart) 中访问其他类的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51926349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com