0

数週間で解決しようとしても問題がありますが、まだ解決できません。ユーザーがカートに追加を押すと、RadioListTile のリストを含むアラートダイアログが表示されます。何も選択せず、もう一度押すと最後になります選択 私はそれを押します

私はそれを 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)));
          }

        }

    );
  }
}
4

1 に答える 1