problem in updating selection in List of RadioListTile showed in AlertDialogue

335 views Asked by At

I have a problem trying to solve it in weeks and still can't, please help me i when user press add to cart it show an AlertDialogue with list of RadioListTile i does't make any selection and i press again it gives me the last selection i press on it

i call it as 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)));
          }

        }

    );
  }
}
1

There are 1 answers

0
M.M.Hasibuzzaman On BEST ANSWER

Alert Dialog is StateLess Widget, so it will not update the ui...i suggest wrapping the alert dialog in a StatefullBuilder it will solve your problem.