Flutter 颤振-在Dart中,如何将变量和函数从一个类访问到另一个类(Diff文件)?
我有一个main.dart文件,其中包含两个函数和head变量:内置小部件。构建小部件的主体写在另一个名为food_List的文件中 主飞镖 食物清单 当相应的按钮在food_列表中单击时,我需要调用2函数,并且需要为food_列表中的文本获取声明的变量main.dart中的_cartNumberFlutter 颤振-在Dart中,如何将变量和函数从一个类访问到另一个类(Diff文件)?,flutter,dart,Flutter,Dart,我有一个main.dart文件,其中包含两个函数和head变量:内置小部件。构建小部件的主体写在另一个名为food_List的文件中 主飞镖 食物清单 当相应的按钮在food_列表中单击时,我需要调用2函数,并且需要为food_列表中的文本获取声明的变量main.dart中的_cartNumber 如何做到这一点?任何建议都会很有帮助:您可以按如下方式将函数传递给食物列表小部件: class FooldList extends StatefulWidget{ final Fun
如何做到这一点?任何建议都会很有帮助:您可以按如下方式将函数传递给食物列表小部件:
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();
}
然后,大体上只需传递如下函数:
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 );
}
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();
}
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 );
}