【发布时间】:2020-11-21 06:30:20
【问题描述】:
第一页
所以我想将这个 ListTile 发送到另一个页面,我们将其称为“购物车”页面以进行演示。它位于第一页的单独函数中,位于构建器上方,稍后在构建器中作为函数调用。这是它的代码:
return ListTile(
trailing: Container(
padding: EdgeInsets.only(top: 15.0),
child: IconButton(
icon: Icon(Icons.add),
onPressed: () => MaterialPageRoute(
this i am not sure how to access cart page
builder: (context) =>
Cart(food.name, food.price),
)),
),
注意 onPressed 上没有 Navigator.push(),因为我只想将数据发送到购物车页面。
这是将数据发送到另一个页面上的“购物车”屏幕的功能,它可以工作。我遇到的问题是我还希望在 AppBar 上(在构建器下方)上有一个按钮,该按钮只进入该页面而不传递任何数据(上面的图标应该只传递数据 onPressed,它是第一页上的 ListTile 项.)
这是第一页 AppBar 图标的代码:
return Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: Icon(Icons.add_shopping_cart, color: Colors.white),
onPressed: () => print('This needs to lead to cart page.'),
//I tried doing the Navigator.push route, but it asks for arguments, but I just want
//to go to the page and not pass any arguments from here
//this needs to lead to cart page
),
],)
购物车页面
这是应该推送数据的“购物车”页面,理想情况下,我只想将该 ListTile 项目添加到购物车中,作为与第 1 页相同的列表图块。
class Cart extends StatefulWidget {
final String foodName;
final String foodPrice;
Cart(this.foodName, this.foodPrice);
@override
_CartState createState() => _CartState();
}
class _CartState extends State<Cart> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Cart'),
centerTitle: true,
),
body: Center(
child: Container(
child: Text('${widget.foodName}, ${widget.foodPrice}'))),
);
}
}
问题是我想对食物有不同的选择,有时会添加例如“food.description”或类似的东西,甚至是像“drink.name”这样的不同类。这是我关于这个问题的精彩画图演示。
【问题讨论】:
-
可能,使用本地存储对你来说是个不错的选择..
-
您应该为购物车创建一个模型类,在购物车页面中,您可以使用名称参数 Cart({this.foodName, this.foodPrice}); .