【问题标题】:How to call a method that is defined in the main class('_MyHomePageState()') from a onPressed of a Flat Button defined in a another class?如何从另一个类中定义的平面按钮的 onPressed 调用主类中定义的方法('_MyHomePageState()')?
【发布时间】: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


    【解决方案1】:

    您可以在您的子按钮类中获取一个函数参数,然后将所需的函数从您的父类 _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,
                  ),
                ),
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      我相信您必须将该功能从主页传递到按钮.. 像这样

          class ButtonClass extends StatefulWidget {
          final Function onPress;
          const ButtonClass({this.onPress});
                 //...
                 //...
                  child: FlatButton(
                    onPressed: onPress,
                    child: Container(
                      color: Colors.red,
                    ),
                  ),
                ),
              ],
              ),
             );
            }
              }
      

      在你的主类中调用它

      ButtonClass(onPress: ()=>openBottomSheet())
      

      【讨论】:

        猜你喜欢
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-20
        相关资源
        最近更新 更多