【发布时间】:2020-06-26 17:52:09
【问题描述】:
我有主类_MyHomePageState(),其中为主页定义了脚手架,并且我定义了非常小部件,它将进入新类中的脚手架。现在我必须从主类中的 FlatButton 的onPressed 调用在主类中定义的函数/方法。
主类中的函数触发BottomSheet,底部sheet的代码写在一个新的dart文件中。
当我在脚手架内正常编写平面按钮代码并调用该函数时,它会触发底部工作表。
这里是sn-p的代码:
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
///This below is the function
void openBottomSheet() {
var sheetController = scaffoldKey.currentState
.showBottomSheet((context) => BottomSheetWidget());
sheetController.closed.then((value) {
print("Bottom Sheet Closed");
});
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
key: scaffoldKey,
appBar: AppBar(
title: Text("Hello,World"),
),
backgroundColor: Colors.grey[800],
body: Stack(children: <Widget>[
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
color: Colors.blueGrey,
),
Column(
children: <Widget>[
TopMenu(),
ButtonClass(),
],
),
]),
);
}
}
这里是按钮类:
class ButtonClass extends StatefulWidget {
_ButtonClassState createState() => _ButtonClassState();
}
class _ButtonClassState extends State<ButtonClass> {
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: <Widget>[
//Container(color: Colors.blue, child: Text("Hello,World")),
Container(
height: 50,
width:100,
margin: EdgeInsets.all(10.0),
child: FlatButton(
onPressed: (){
///And I am trying to call that function here, but is not working
_MyHomePageState().openBottomSheet();
},
child: Container(
color: Colors.red,
),
),
),
],
),
);
}
}
【问题讨论】:
标签: flutter dart flutter-layout