Flutter 如何显示整个购物车的总数
如何显示整个购物车的总数 我想显示整个购物车的总价,但当我增加商品数量时,我会得到商品的总价。我为购物车中的每件商品获得不同的价值。总计计算的逻辑在totalPrice()方法的cart类中 cart类Flutter 如何显示整个购物车的总数,flutter,dart,Flutter,Dart,如何显示整个购物车的总数 我想显示整个购物车的总价,但当我增加商品数量时,我会得到商品的总价。我为购物车中的每件商品获得不同的价值。总计计算的逻辑在totalPrice()方法的cart类中 cart类 import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'dish_object.dart'; class Cart extends StatefulWidg
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dish_object.dart';
class Cart extends StatefulWidget {
final List<Dish> _cart;
Cart(this._cart);
@override
_CartState createState() => _CartState(this._cart);
}
class _CartState extends State<Cart> {
_CartState(this._cart);
List<Dish> _cart;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Cart'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.send_rounded),
tooltip: "Confirm Order",
onPressed: () {
if (_cart.isNotEmpty) {
setState(() {
Fluttertoast.showToast(
msg: "Order Confirmed",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.grey,
textColor: Colors.white,
fontSize: 16.0);
});
}
if (_cart.isEmpty) {
setState(() {
Fluttertoast.showToast(
msg: "Cart Empty",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
});
}
}),
if (_cart.length > 0)
Padding(
padding: const EdgeInsets.only(left: 0.0),
child: CircleAvatar(
radius: 10.0,
backgroundColor: Colors.red,
foregroundColor: Colors.white,
child: Text(
_cart.length.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 12.0,
),
),
),
),
],
),
body: ListView.builder(
itemCount: _cart.length,
itemBuilder: (context, index) {
var item = _cart[index];
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 2.0),
child: Card(
elevation: 4.0,
child: ListTile(
//Leading
leading: Text(("Total: R") +
item.totalPrice().toString() +
"\n" +
item.category +
"\n" +
"R" +
item.price.toString()),
//Title
title: Text(item.brandName +
"\n" +
"(" +
item.counter.toString() +
")"),
//Subtitle
subtitle: GestureDetector(
child: Icon(
Icons.add,
color: Colors.green,
),
onTap: () {
setState(() {
item.incrementCounter();
});
},
),
//Trailing
trailing: GestureDetector(
child: Icon(
Icons.remove_circle,
color: Colors.red,
),
onTap: () {
setState(() {
item.decrementCounter();
});
},
),
isThreeLine: true,
),
),
);
},
),
);
}
}
要计算购物车的总数,可以将
盘的列表折叠起来以计算总数:
_cart
.fold<int>(0, (a, b) => a + b.totalPrice());
\u购物车
.fold(0,(a,b)=>a+b.totalPrice());
以下是指定小部件的完整示例:
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dish_object.dart';
class Cart extends StatefulWidget {
final List<Dish> _cart;
Cart(this._cart);
@override
_CartState createState() => _CartState(this._cart);
}
class _CartState extends State<Cart> {
_CartState(this._cart);
List<Dish> _cart;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Cart'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.send_rounded),
tooltip: "Confirm Order",
onPressed: () {
if (_cart.isNotEmpty) {
setState(() {
Fluttertoast.showToast(
msg: "Order Confirmed",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.grey,
textColor: Colors.white,
fontSize: 16.0);
});
}
if (_cart.isEmpty) {
setState(() {
Fluttertoast.showToast(
msg: "Cart Empty",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
});
}
}),
if (_cart.length > 0)
Padding(
padding: const EdgeInsets.only(left: 0.0),
child: CircleAvatar(
radius: 10.0,
backgroundColor: Colors.red,
foregroundColor: Colors.white,
child: Text(
_cart.length.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 12.0,
),
),
),
),
],
),
body: ListView.builder(
itemCount: _cart.length + 1,
itemBuilder: (context, index) {
if (index == _cart.length)
return Padding(
padding:
const EdgeInsets.symmetric(horizontal: 8.0, vertical: 2.0),
child: Card(
elevation: 4.0,
child: Text("Cart Total: R" +
_cart
.fold<int>(0, (a, b) => a + b.totalPrice())
.toString()),
),
);
var item = _cart[index];
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 2.0),
child: Card(
elevation: 4.0,
child: ListTile(
//Leading
leading: Text(("Total: R") +
item.totalPrice().toString() +
"\n" +
item.category +
"\n" +
"R" +
item.price.toString()),
//Title
title: Text(item.brandName +
"\n" +
"(" +
item.counter.toString() +
")"),
//Subtitle
subtitle: GestureDetector(
child: Icon(
Icons.add,
color: Colors.green,
),
onTap: () {
setState(() {
item.incrementCounter();
});
},
),
//Trailing
trailing: GestureDetector(
child: Icon(
Icons.remove_circle,
color: Colors.red,
),
onTap: () {
setState(() {
item.decrementCounter();
});
},
),
isThreeLine: true,
),
),
);
},
),
);
}
}
导入“包装:颤振/材料.省道”;
进口“包装:fluttoast/fluttoast.dart”;
导入“dish_object.dart”;
类Cart扩展StatefulWidget{
最终清单(购物车),;
购物车(本车);
@凌驾
_CartState createState();
}
类_CartState扩展了状态{
_CartState(本车);
列表-购物车;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“购物车”),
行动:[
图标按钮(
图标:图标(图标。发送),
工具提示:“确认订单”,
已按下:(){
如果(_cart.isNotEmpty){
设置状态(){
烤面包片(
消息:“订单已确认”,
烤面包片长度:烤面包片长度,
重力:ToastGravity.BOTTOM,
TimeInSecureForiosWeb:1,
背景颜色:颜色。灰色,
textColor:Colors.white,
体积:16.0),;
});
}
如果(_cart.isEmpty){
设置状态(){
烤面包片(
消息:“购物车空”,
烤面包片长度:烤面包片长度,
重力:ToastGravity.BOTTOM,
TimeInSecureForiosWeb:1,
背景颜色:Colors.red,
textColor:Colors.white,
体积:16.0),;
});
}
}),
如果(_.length>0)
填充物(
填充:仅限常量边集(左:0.0),
孩子:圆环星(
半径:10.0,
背景颜色:Colors.red,
前底色:颜色。白色,
子:文本(
_cart.length.toString(),
样式:TextStyle(
fontWeight:fontWeight.bold,
字体大小:12.0,
),
),
),
),
],
),
正文:ListView.builder(
itemCount:_cart.length+1,
itemBuilder:(上下文,索引){
如果(索引==\u购物车长度)
返回填充(
衬垫:
常数边集对称(水平:8.0,垂直:2.0),
孩子:卡片(
标高:4.0,
子项:文本(“购物车总计:R”+
_推车
.fold(0,(a,b)=>a+b.totalPrice())
.toString()),
),
);
var项目=_购物车[索引];
返回填充(
填充:常量边集。对称(水平:8.0,垂直:2.0),
孩子:卡片(
标高:4.0,
孩子:ListTile(
//领先的
前导:文本((“总计:R”)+
item.totalPrice().toString()+
“\n”+
项目.类别+
“\n”+
“R”+
item.price.toString()),
//头衔
标题:文本(item.brandName)+
“\n”+
"(" +
item.counter.toString()+
")"),
//副标题
字幕:手势检测器(
子:图标(
Icons.add,
颜色:颜色。绿色,
),
onTap:(){
设置状态(){
item.incrementCounter();
});
},
),
//拖尾
跟踪:手势检测器(
子:图标(
图标。删除“”圆,
颜色:颜色,红色,
),
onTap:(){
设置状态(){
item.decrementCounter();
});
},
),
伊斯特里琳:是的,
),
),
);
},
),
);
}
}
我在哪里实现_cart.reduce方法?根据闭包上下文的要求,我得到一个错误,它说返回类型“int”不是一个“Dish”。
你可以在答案的第二个片段中找到完整的示例,更具体地说是从第71行到第83行,在ListView.builder()中。
我得到一个错误,它说返回类型“int”不是一个“Dish”,根据闭包上下文的要求。我是否初始化a
和b
变量?如果是在哪里?我已经更新了答案中的代码,请让我知道它是否解决了问题
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dish_object.dart';
class Cart extends StatefulWidget {
final List<Dish> _cart;
Cart(this._cart);
@override
_CartState createState() => _CartState(this._cart);
}
class _CartState extends State<Cart> {
_CartState(this._cart);
List<Dish> _cart;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Cart'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.send_rounded),
tooltip: "Confirm Order",
onPressed: () {
if (_cart.isNotEmpty) {
setState(() {
Fluttertoast.showToast(
msg: "Order Confirmed",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.grey,
textColor: Colors.white,
fontSize: 16.0);
});
}
if (_cart.isEmpty) {
setState(() {
Fluttertoast.showToast(
msg: "Cart Empty",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
});
}
}),
if (_cart.length > 0)
Padding(
padding: const EdgeInsets.only(left: 0.0),
child: CircleAvatar(
radius: 10.0,
backgroundColor: Colors.red,
foregroundColor: Colors.white,
child: Text(
_cart.length.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 12.0,
),
),
),
),
],
),
body: ListView.builder(
itemCount: _cart.length + 1,
itemBuilder: (context, index) {
if (index == _cart.length)
return Padding(
padding:
const EdgeInsets.symmetric(horizontal: 8.0, vertical: 2.0),
child: Card(
elevation: 4.0,
child: Text("Cart Total: R" +
_cart
.fold<int>(0, (a, b) => a + b.totalPrice())
.toString()),
),
);
var item = _cart[index];
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 2.0),
child: Card(
elevation: 4.0,
child: ListTile(
//Leading
leading: Text(("Total: R") +
item.totalPrice().toString() +
"\n" +
item.category +
"\n" +
"R" +
item.price.toString()),
//Title
title: Text(item.brandName +
"\n" +
"(" +
item.counter.toString() +
")"),
//Subtitle
subtitle: GestureDetector(
child: Icon(
Icons.add,
color: Colors.green,
),
onTap: () {
setState(() {
item.incrementCounter();
});
},
),
//Trailing
trailing: GestureDetector(
child: Icon(
Icons.remove_circle,
color: Colors.red,
),
onTap: () {
setState(() {
item.decrementCounter();
});
},
),
isThreeLine: true,
),
),
);
},
),
);
}
}