[英]Flutter - In Dart, How to access a variable and function from a class to another class(Diff file)?
I have a main.dart
file contain two functions and variable for the head:
in build widget
.我有一个
main.dart
文件,其中包含两个函数和head:
变量head:
在build widget
。 The body of the build widget
written in another file called food_List
, build widget
的主体写在另一个名为food_List
文件中,
main.dart 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();
}
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
.我需要调用2功能时,相应的按钮点击在
food_List
和声明的变量,称为_cartNumber
在main.dart
需要取文本中的food_List
。
How to achieve this?如何实现这一目标? any suggestion would be helpful:)
任何建议都会有所帮助:)
you could pass the functions to the food_list widget as follows:您可以将函数传递给 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();
}
then in main just pass the functions like this :然后在 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 );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.