我有一个问题试图在几周内解决但仍然无法解决,请帮助我,当用户按下添加到购物车时,它会显示一个带有 RadioListTile 列表的 AlertDialogue 我没有做出任何选择,我再次按下它给了我最后一个选择我按下它
我称它为 IconButton: AddProductToCartButton(product);
class AddProductToCartButton extends StatefulWidget {
final ProductModel product ;
AddProductToCartButton(this.product);
@override
_AddProductToCartButtonState createState() => _AddProductToCartButtonState();
}
class _AddProductToCartButtonState extends State<AddProductToCartButton> {
ProductSCIQPModel selectedSize = new ProductSCIQPModel();
String selectedTextSize;
bool addToCartFlag = false;
List<ProductSCIQPModel>_tempSizeDetailsList = [];
var _widgets = List<Widget>();
@override
void initState() {
_tempSizeDetailsList = widget.product.size_color_price;
}
Future _addProductDetailsToCart() async{
addToCartFlag = true;
CartProvider cartProvider = Provider.of<CartProvider>(
context, listen: false);
CartProductModel _tempCartProductModel;
_tempCartProductModel = new CartProductModel();
_tempCartProductModel.productid = widget.product.id;
_tempCartProductModel.name = widget.product.name;
_tempCartProductModel.namear = widget.product.namear;
_tempCartProductModel.price = selectedSize.price ;
_tempCartProductModel.quantity = 1;
_tempCartProductModel.productSize = selectedSize.product_size;
_tempCartProductModel.image = widget.product.images[0];
cartProvider.addProductToCart(_tempCartProductModel);
await addProductToCart(_tempCartProductModel);
}
List<Widget> createRadioListSizes() {
_widgets.clear();
_tempSizeDetailsList.where((element) => element.available != false).forEach((ProductSCIQPModel element) {
_widgets.add(
RadioListTile(
value: element,
groupValue: selectedSize,
activeColor: Colors.blue,
title: Row(
children: [
Text(element.product_size != null ?
' ' + element.product_size
: 'no size'),
Text(' - '),
Text(element.price.toString()),
],
),
onChanged: (ProductSCIQPModel value){
setState(() {
selectedSize = value;
});
print('selected : ' + selectedSize.product_size);
},
)
);});
return _widgets;
}
Future<String> SelectSizeDialogue(BuildContext context){
return showDialog(context: context, builder: (context){
return AlertDialog(
title: Text(AppLocalizations.of(context).translate('SELECTSIZE')),
content:
Column(
children: createRadioListSizes(),//_widgets ,
),
actions: [
MaterialButton(
elevation: 5.0,
child: Text('Add to cart'),
onPressed: () {
Navigator.of(context).pop();
_addProductDetailsToCart();
}),
MaterialButton(
elevation: 5.0,
child: Text('Cancel'),
onPressed: (){
addToCartFlag= false;
Navigator.of(context).pop();
},
)
],
);
});
}
@override
Widget build(BuildContext context) {
return IconButton(
icon:
Icon(Icons.add_shopping_cart, color: Colors.black54,),
onPressed: () async {
if (widget.product.size_color_price.length > 1){
await SelectSizeDialogue(context);
}else {
selectedSize.product_size = widget.product.size_color_price[0].product_size;
selectedSize.price = widget.product.size_color_price[0].price;
await _addProductDetailsToCart();
}
if(addToCartFlag) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(widget.product.name + ' size : ' + selectedSize.product_size.toString() +
" added to cart"),
backgroundColor: Colors.green,
duration: Duration(seconds: 2)));
}
}
);
}
}