import 'package:diplomaticquarterapp/core/model/pharmacies/ShoppingCart.dart'; import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart'; import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart'; import 'package:diplomaticquarterapp/widgets/data_display/text.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class ProductOrderItem extends StatefulWidget { final ShoppingCart item; final VoidCallback changeCartItems; final VoidCallback deleteCartItems; ProductOrderItem(this.item, this.changeCartItems, this.deleteCartItems); @override _ProductOrderItemState createState() => _ProductOrderItemState(); } class _ProductOrderItemState extends State { TextEditingController _quantityController = new TextEditingController(); String _totalPrice = "0"; @override void initState() { _quantityController.text = "${widget.item.quantity}"; _totalPrice = "${(widget.item.product.price * widget.item.quantity).toStringAsFixed(2)}"; super.initState(); } @override Widget build(BuildContext context) { ProjectViewModel projectProvider = Provider.of(context); return Column( children: [ ListTile( leading: InkWell( onTap: () => {widget.deleteCartItems()}, child: Icon( Icons.delete_outline_sharp, color: Colors.grey.shade700, ), ), title: Container( child: Row( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.max, children: [ (widget.item.product.images != null && widget.item.product.images.length > 0) ? Image.network( widget.item.product.images[0].src, fit: BoxFit.cover, height: 80, ) : Image.asset( "assets/images/no_image.png", fit: BoxFit.cover, height: 80, ), Expanded( child: Container( margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 8), child: Container( child: Column( mainAxisSize: MainAxisSize.max, crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( child: Texts( projectProvider.isArabic ? widget.item.product.namen : "${widget.item.product.name}", regular: true, textAlign: TextAlign.justify, fontSize: 12, fontWeight: FontWeight.w400, ), margin: const EdgeInsets.only(bottom: 4), ), Container( child: Texts( "${(widget.item.product.price).toStringAsFixed(2)} ${projectProvider.isArabic ? widget.item.currencyn : widget.item.currency}", fontSize: 14, fontWeight: FontWeight.bold, ), margin: const EdgeInsets.only(bottom: 4), ), Container( margin: EdgeInsets.symmetric(vertical: 4), child: Row( children: [ InkWell( onTap: () => {_quantityOnChangeClick(Operation.dec)}, child: Container( width: 25, height: 25, child: Icon( Icons.remove, color: Colors.grey.shade400, size: 20, ), decoration: BoxDecoration( border: Border.all( color: Colors.grey.shade400, width: 1.0, ), ), ), ), Container( margin: const EdgeInsets.symmetric(horizontal: 4), width: 25, height: 25, color: Colors.grey.shade300, child: Center( child: TextField( cursorColor: Colors.black, keyboardType: TextInputType.number, controller: _quantityController, textAlign: TextAlign.center, onChanged: (text) { setState(() { var value = int.tryParse(text); if (value == null) { widget.item.quantity = 0; } else { widget.item.quantity = int.parse(text); } _totalPrice = "${(widget.item.product.price * widget.item.quantity).toStringAsFixed(2)}"; }); }, )), ), InkWell( onTap: () => {_quantityOnChangeClick(Operation.inc)}, child: Container( width: 25, height: 25, child: Icon( Icons.add, color: Colors.grey.shade400, size: 20, ), decoration: BoxDecoration( border: Border.all( color: Colors.grey.shade400, width: 1.0, ), ), ), ), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ Texts( TranslationBase.of(context).total, color: Colors.grey.shade500, fontWeight: FontWeight.bold, fontSize: 12, ), Texts( "$_totalPrice ${projectProvider.isArabic ? widget.item.currencyn : widget.item.currency}", fontSize: 12, fontWeight: FontWeight.bold, ) ], ), ) ], ), ) ], ), ), ), ) ], ), ), ), const Divider( color: Color(0xFFD6D6D6), height: 15, thickness: 1, indent: 0, endIndent: 0, ), ], ); } _quantityOnChangeClick(Operation operation) { int newValue = 0; setState(() { switch (operation) { case Operation.inc: { newValue = widget.item.quantity + 1; } break; case Operation.dec: { newValue = widget.item.quantity - 1; } break; default: { //statements; } break; } if (newValue > 0) { widget.item.quantity = newValue; _quantityController.text = "${widget.item.quantity}"; _totalPrice = "${(widget.item.product.price * widget.item.quantity).toStringAsFixed(2)}"; } }); if (newValue > 0) { widget.changeCartItems(); } } @override void dispose() { // Clean up the controller when the widget is removed from the // widget tree. _quantityController.dispose(); super.dispose(); } } enum Operation { inc, dec }