在Dart中,如何从一个类中访问另一个类(不同文件)的变量和函数?

huangapple go评论77阅读模式
英文:

Flutter - In Dart, How to access a variable and function from a class to another class(Diff file)?

问题

food_List中,当相应的按钮被点击时,你需要调用两个函数,并且需要获取main.dart中声明的名为_cartNumber的变量来显示在food_List中的文本。要实现这个目标,你可以按照以下步骤进行操作:

  1. 首先,在food_List文件中,你需要引入main.dart文件中的内容,以便能够访问其中声明的变量和函数。在文件的开头添加以下导入语句:
import 'main.dart';
  1. 确保food_List中的_buildItemsForListView方法能够访问_cartNumIncrement_cartNumDecrement函数以及_cartNumber变量。这可以通过将main.dart中的方法和变量设置为static来实现,以便它们可以在其他类中访问。修改main.dart如下:
class _MrTabs extends State<MyApp> {
  static int _cartNumber = 10; // 设置为static以供其他类访问

  // ...

  static void _cartNumIncrement() {
    _cartNumber += 1;
  }

  static void _cartNumDecrement() {
    _cartNumber -= 1;
  }

  // ...
}
  1. _buildItemsForListView方法中,你可以调用_cartNumIncrement_cartNumDecrement函数来处理按钮的点击事件。同时,你可以使用_MrTabs._cartNumber来获取_cartNumber的值并显示在文本中。修改_buildItemsForListView如下:
Widget _buildItemsForListView(BuildContext context, int index) {
  return Card(
    child: Row(
      children: <Widget>[
        FlatButton(
          onPressed: () {
            _MrTabs._cartNumDecrement(); // 调用_cartNumDecrement
            setState(() {}); // 更新UI
          },
          child: Text("-", style: TextStyle(color: Colors.white, fontSize: 14)),
        ),
        Text(_MrTabs._cartNumber.toString(), style: TextStyle(color: Colors.white, fontSize: 14)),
        FlatButton(
          onPressed: () {
            _MrTabs._cartNumIncrement(); // 调用_cartNumIncrement
            setState(() {}); // 更新UI
          },
          child: Text("+", style: TextStyle(color: Colors.white, fontSize: 14)),
        ),
      ],
    ),
  );
}

这样,当你点击按钮时,_cartNumIncrement_cartNumDecrement函数将被调用,同时UI也会更新以反映_cartNumber的变化。请确保在相应的位置调用setState来触发UI的刷新。

这些步骤将允许你在food_List中调用main.dart中的函数并获取_cartNumber的值来更新UI。希望这些信息对你有所帮助!

英文:

I have a main.dart file contain two functions and variable for the head: in build widget. The body of the build widget written in another file called food_List,

main.dart

class MyApp extends StatefulWidget {

  @override

  State&lt;StatefulWidget&gt; createState() {

    return _MrTabs();

  }
}
class _MrTabs extends State&lt;MyApp&gt; {
 

  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&lt;FoodList&gt; {

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


  Widget _buildItemsForListView(BuildContext context, int index) {
    return Card(
        child: Row(
       
                          child: new Row(
                              children: &lt;Widget&gt;[
                                new FlatButton(
                                    onPressed: () {//this is where I need to invoke the **_cartNumDecrement()**  },
                                    child: new Text(&quot;-&quot;, 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(&quot;+&quot;, 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() =&gt; FoodListState();
}

I need to invoke the 2 function when the corresponding button clicks in food_List, and the variable declared, called _cartNumber in main.dart need to be fetched for the text in food_List.

How to achieve this? any suggestion would be helpful:)

答案1

得分: 1

以下是您要翻译的内容:

你可以将这些函数传递给food_list小部件,如下所示:

class FooldList extends StatefulWidget {
 final Function incCart;
 final Function decCart;
 //将这些函数传递给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(// 在这里是_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();
 }

 然后在主函数中就像这样传递这些函数:

 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 );
 }

希望这对您有所帮助!如果您需要任何进一步的翻译或帮助,请告诉我。

英文:

you could pass the functions to the food_list widget as follows:

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

    class FoodListState extends State&lt;FoodList&gt; {
    
      @override
      void initState() {
        super.initState();
        }
    
    
      Widget _buildItemsForListView(BuildContext context, int index) {
        return Card(
            child: Row(
    
                              child: new Row(
                                  children: &lt;Widget&gt;[
                                    new FlatButton(
                                        onPressed: () =&gt; widget.decCart,
                                        child: new Text(&quot;-&quot;, style: TextStyle(
                                          color: Colors.white, fontSize: 14,
                                        ))),
                                    new Text(// here the _cartNumber,
                                        style: TextStyle(
                                          color: Colors.white, fontSize: 14,
                                        )),
                                    new FlatButton(
                                        onPressed: () =&gt; widget.incCart,
                                        child: new Text(&quot;+&quot;, 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() =&gt; FoodListState();
    }

then in main just pass the functions like this :

class _MrTabs extends State&lt;MyApp&gt; {


  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 );
}

huangapple
  • 本文由 发表于 2020年1月3日 17:36:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定