[英]Adding items from class from one page to another in Flutter
I have this separate screen with 'FoodIcon' class and a list inside of it that is calling it:我有一个单独的屏幕,上面有“FoodIcon”class,里面有一个调用它的列表:
food_icon.dart food_icon.dart
class FoodIcon {
String name;
IconButton foodIcon;
FoodIcon(this.name, this.foodIcon);
List foods = [
FoodIcon('Beef',
IconButton(icon: Image.asset('assets/beef.png'), onPressed: null)),
];
}
Now how would I access the List 'foods' from the FoodIcon class from food_icon.dart on another screen (main.dart)?现在,我将如何从另一个屏幕(main.dart)上的 food_icon.dart 的 FoodIcon class 访问列表“食物”? For example if I have a Container with the AlertDialog on another screen:例如,如果我在另一个屏幕上有一个带有 AlertDialog 的容器:
main.dart主要.dart
Widget _buildIcon() {
return AlertDialog(
title: Text('Add Icon'),
content: Container(//For example how do I access elements of the list from another page here, for example I would want a 3x3 row of icons from the FoodIcon icons list (name and then the icon itself besides it)),
I imported the page where the FoodIcon class is on the page I want it accessed on, but how would I go around getting to the List and the elements inside of it?我导入了 FoodIcon class 在我希望访问的页面上的页面,但是我 go 如何访问列表及其中的元素?
Added full code of the page I would need it on:添加了我需要它的页面的完整代码:
class FoodList extends StatefulWidget {
@override
_FoodListState createState() => _FoodListState();
}
class _FoodListState extends State<FoodList> {
@override
void initState() {
super.initState();
DatabaseProvider.db.getFoods().then(
(foodList) {
BlocProvider.of<FoodBloc>(context).add(SetFoods(foodList));
},
);
}
Widget _buildIcon() {
return AlertDialog(
title: Text('Add Icon'),
content: Container(),
actions: [
FlatButton(
child: Text('Cancel'),
onPressed: () => Navigator.pop(context),
),
],
);
}
@override
Widget build(BuildContext context) {
return Container(
child: //some button that would show _buildIcon() alert window on tap}
Nevermind, after some experimentation, I got it to work like this:没关系,经过一些实验,我让它像这样工作:
food_icon.dart food_icon.dart
class FoodIcon {
String name;
IconButton foodIcon;
FoodIcon(this.name, this.foodIcon);
static List<FoodIcon> foods() {
return <FoodIcon>[
FoodIcon('Beef',
IconButton(icon: Image.asset('assets/beef.png'), onPressed: null))
];
}
main.dart主要.dart
initialized it like this:像这样初始化它:
List<FoodIcon> _foodIcons = FoodIcon.foods();
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.