Flutter 如何从另一个类中定义的平面按钮的按下调用在主类(';&u MyHomePageState()';)中定义的方法?
我有一个主类Flutter 如何从另一个类中定义的平面按钮的按下调用在主类(';&u MyHomePageState()';)中定义的方法?,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我有一个主类\u MyHomePageState(),其中为主页定义了脚手架,并且我定义了一个新类中进入脚手架的小部件。现在我必须从主类中FlatButton的onPressed调用主类中定义的函数/方法 主类中的函数触发底部工作表,底部工作表的代码写入新的dart文件 当我正常地在scaffold内部编写平面按钮代码并调用函数时,它确实会触发底部表单 下面是代码片段: class MyHomePage extends StatefulWidget { @override _MyHome
\u MyHomePageState()
,其中为主页定义了脚手架,并且我定义了一个新类中进入脚手架的小部件。现在我必须从主类中FlatButton的onPressed
调用主类中定义的函数/方法
主类中的函数触发底部工作表,底部工作表的代码写入新的dart文件
当我正常地在scaffold内部编写平面按钮代码并调用函数时,它确实会触发底部表单
下面是代码片段:
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(),
],
),
]),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
最终GlobalKey脚手架键=GlobalKey();
///下面是函数
void openBottomSheet(){
var sheetController=scaffoldKey.currentState
.showBottomSheet((上下文)=>BottomSheetWidget());
sheetController.closed.then((值){
打印(“底页关闭”);
});
}
@凌驾
小部件构建(构建上下文){
//TODO:实现构建
返回脚手架(
钥匙:脚手架钥匙,
appBar:appBar(
标题:文本(“你好,世界”),
),
背景颜色:颜色。灰色[800],
主体:堆栈(子对象:[
容器(
高度:MediaQuery.of(context).size.height,
宽度:MediaQuery.of(context).size.width,
颜色:颜色。蓝灰色,
),
纵队(
儿童:[
TopMenu(),
ButtonClass(),
],
),
]),
);
}
}
下面是button类:
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,
),
),
),
],
),
);
}
}
class按钮class扩展StatefulWidget{
_ButtonClassState createState()=>\u ButtonClassState();
}
类_按钮类状态扩展状态{
@凌驾
小部件构建(构建上下文){
返回中心(
子:列(
儿童:[
//容器(颜色:Colors.blue,child:Text(“Hello,World”),
容器(
身高:50,
宽度:100,
保证金:所有边缘套(10.0),
孩子:扁平按钮(
已按下:(){
///我试图在这里调用这个函数,但不起作用
_MyHomePageState().openBottomSheet();
},
子:容器(
颜色:颜色,红色,
),
),
),
],
),
);
}
}
您可以在子按钮类中获取函数参数,然后将所需函数从父类\u MyHomePageState传递给它
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(onPressed: ()=> openBottomSheet() ),
],
),
]),
);
}
}
class ButtonClass extends StatefulWidget {
Function onPressed;
ButtonClass({this.onPressed});
_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: () => widget.onPressed,
child: Container(
color: Colors.red,
),
),
),
],
),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
最终GlobalKey脚手架键=GlobalKey();
///下面是函数
void openBottomSheet(){
var sheetController=scaffoldKey.currentState
.showBottomSheet((上下文)=>BottomSheetWidget());
sheetController.closed.then((值){
打印(“底页关闭”);
});
}
@凌驾
小部件构建(构建上下文){
//TODO:实现构建
返回脚手架(
钥匙:脚手架钥匙,
appBar:appBar(
标题:文本(“你好,世界”),
),
背景颜色:颜色。灰色[800],
主体:堆栈(子对象:[
容器(
高度:MediaQuery.of(context).size.height,
宽度:MediaQuery.of(context).size.width,
颜色:颜色。蓝灰色,
),
纵队(
儿童:[
TopMenu(),
ButtonClass(按下时:()=>openBottomSheet()),
],
),
]),
);
}
}
类按钮类扩展StatefulWidget{
功能按下;
ButtonClass({this.onPressed});
_ButtonClassState createState()=>\u ButtonClassState();
}
类_按钮类状态扩展状态{
@凌驾
小部件构建(构建上下文){
返回中心(
子:列(
儿童:[
//容器(颜色:Colors.blue,child:Text(“Hello,World”),
容器(
身高:50,
宽度:100,
保证金:所有边缘套(10.0),
孩子:扁平按钮(
onPressed:()=>widget.onPressed,
子:容器(
颜色:颜色,红色,
),
),
),
],
),
);
}
}
我相信您必须将该功能从主页传递到按钮。。像这样的
class ButtonClass extends StatefulWidget {
final Function onPress;
const ButtonClass({this.onPress});
//...
//...
child: FlatButton(
onPressed: onPress,
child: Container(
color: Colors.red,
),
),
),
],
),
);
}
}
在你的主课上叫它
ButtonClass(onPress: ()=>openBottomSheet())