Flutter-在Dart中,如何从一个类到另一个类(Diff文件)访问变量和函数?

时间:2020-01-03 09:36:05

标签: flutter dart

我有一个main.dart文件,其中包含两个函数和head:build widget的变量。 build widget的主体写在另一个名为food_List的文件中,

main.dart

class MyApp extends StatefulWidget {

  @override

  State<StatefulWidget> createState() {

    return _MrTabs();

  }
}
class _MrTabs extends State<MyApp> {


  int _cartNumber = 10;

  @override
  void initState() {
    super.initState();
  }


  _cartNumIncrement() {
    _cartNumber += 1;
  }

  _cartNumDecrement() {
    _cartNumber -= 1;
  }

 @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home:......
      body:......
}

food_List

class FoodListState extends State<FoodList> {

  @override
  void initState() {
    super.initState();
    }


  Widget _buildItemsForListView(BuildContext context, int index) {
    return Card(
        child: Row(

                          child: new Row(
                              children: <Widget>[
                                new FlatButton(
                                    onPressed: () {//this is where I need to invoke the **_cartNumDecrement()**  },
                                    child: new Text("-", style: TextStyle(
                                      color: Colors.white, fontSize: 14,
                                    ))),
                                new Text(// here the _cartNumber,
                                    style: TextStyle(
                                      color: Colors.white, fontSize: 14,
                                    )),
                                new FlatButton(
                                    onPressed: (//this is where I need to invoke the **_cartNumIncrement()** ) {},
                                    child: new Text("+", style: TextStyle(
                                      color: Colors.white, fontSize: 14,
                                    ))),
                              ]
                          ),
                        ),
                      ),

        ));
  }



   @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: ListView.builder(
          itemCount: 10,
          itemBuilder: _buildItemsForListView,
        ));
  }

}

class FoodList extends StatefulWidget {
  @override
  createState() => FoodListState();
}

当相应按钮在food_List中单击时,我需要调用2函数,并且需要为{{1}中的文本提取在_cartNumber中声明的变量main.dart }}。

如何实现?任何建议都会有所帮助:)

1 个答案:

答案 0 :(得分:1)

您可以按如下所示将函数传递给food_list小部件:

    class FooldList extends StatefulWidget{
     final Function incCart;
     final Function decCart;
   //pass these in the constructor of FoodList}

    class FoodListState extends State<FoodList> {

      @override
      void initState() {
        super.initState();
        }


      Widget _buildItemsForListView(BuildContext context, int index) {
        return Card(
            child: Row(

                              child: new Row(
                                  children: <Widget>[
                                    new FlatButton(
                                        onPressed: () => widget.decCart,
                                        child: new Text("-", style: TextStyle(
                                          color: Colors.white, fontSize: 14,
                                        ))),
                                    new Text(// here the _cartNumber,
                                        style: TextStyle(
                                          color: Colors.white, fontSize: 14,
                                        )),
                                    new FlatButton(
                                        onPressed: () => widget.incCart,
                                        child: new Text("+", style: TextStyle(
                                          color: Colors.white, fontSize: 14,
                                        ))),
                                  ]
                              ),
                            ),
                          ),

            ));
      }



       @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: ListView.builder(
              itemCount: 10,
              itemBuilder: _buildItemsForListView,
            ));
      }

    }

    class FoodList extends StatefulWidget {
      @override
      createState() => FoodListState();
    }

然后在main中仅传递如下功能:

class _MrTabs extends State<MyApp> {


  int _cartNumber = 10;

  @override
  void initState() {
    super.initState();
  }


  _cartNumIncrement() {
    _cartNumber += 1;
  }

  _cartNumDecrement() {
    _cartNumber -= 1;
  }

 @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home:......
      body:FooldList(incCart: _cartNumIncrement, decCart:_cartNumDecrement );
}