英文:
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
中的文本。要实现这个目标,你可以按照以下步骤进行操作:
- 首先,在
food_List
文件中,你需要引入main.dart
文件中的内容,以便能够访问其中声明的变量和函数。在文件的开头添加以下导入语句:
import 'main.dart';
- 确保
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;
}
// ...
}
- 在
_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<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
.
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<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 :
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 );
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论