import 'package:fcs/pages/widgets/local_radio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_vector_icons/flutter_vector_icons.dart'; import 'package:provider/provider.dart'; import '../../constants.dart'; import '../../../domain/entities/carton_size.dart'; import '../../../domain/entities/fcs_shipment.dart'; import '../../../helpers/theme.dart'; import '../../domain/entities/user.dart'; import '../carton_size/carton_size_list.dart'; import '../carton_size/model/carton_size_model.dart'; import '../fcs_shipment/model/fcs_shipment_model.dart'; import '../main/util.dart'; import '../widgets/box_size_picker.dart'; import '../widgets/continue_button.dart'; import '../widgets/display_text.dart'; import '../widgets/local_dropdown.dart'; import '../widgets/local_text.dart'; import '../widgets/local_title.dart'; import '../widgets/previous_button.dart'; typedef OnPrevious = Function(); typedef OnContinue = Function(String deliveryType, String billType, FcsShipment shipment, String cartonSizeType, {CartonSize? standardSize, double? length, double? width, double? height}); class CartonSizeWidget extends StatefulWidget { final OnPrevious? onPrevious; final OnContinue? onContinue; final User sender; final User consignee; final String lastMile; final String billType; final FcsShipment? shipment; final String cartonSizeType; final CartonSize? standardSize; final double? length; final double? width; final double? height; const CartonSizeWidget( {Key? key, this.onPrevious, this.onContinue, this.shipment, required this.cartonSizeType, this.standardSize, this.length, this.width, this.height, required this.sender, required this.consignee, required this.lastMile, required this.billType}) : super(key: key); @override State createState() => _CartonSizeWidgetState(); } class _CartonSizeWidgetState extends State { // List _deliveryTypes = [delivery_caton, pickup_carton]; FcsShipment? _shipment; String _cartonSizeType = standardCarton; List _shipments = []; CartonSize? _selectStandardSize; late String _selectedLastmile; late String _billToValue; TextEditingController _widthController = new TextEditingController(); TextEditingController _heightController = new TextEditingController(); TextEditingController _lengthController = new TextEditingController(); @override void initState() { _init(); super.initState(); } _init() async { _selectedLastmile = widget.lastMile; _billToValue = widget.billType; _cartonSizeType = widget.cartonSizeType; _lengthController.text = widget.length == null ? "0" : removeTrailingZeros(widget.length ?? 0); _widthController.text = widget.width == null ? "0" : removeTrailingZeros(widget.width ?? 0); _heightController.text = widget.height == null ? "0" : removeTrailingZeros(widget.height ?? 0); _getStandardCartonSize(); _loadShipment(); if (mounted) { setState(() {}); } } _getStandardCartonSize() { List cartonSizes = context.read().cartonSizes; _selectStandardSize = widget.standardSize ?? cartonSizes.first; if (mounted) { setState(() {}); } } _loadShipment() async { var fcsShipments = await context.read().getActiveFcsShipments(); _shipments = fcsShipments; if (_shipments.contains(widget.shipment)) { _shipment = widget.shipment; } else { _shipment = null; } if (mounted) { setState(() {}); } } @override void didUpdateWidget(covariant CartonSizeWidget oldWidget) { _loadShipment(); _getStandardCartonSize(); super.didUpdateWidget(oldWidget); } @override Widget build(BuildContext context) { List cartonSizes = context.watch().getCartonSizes; bool isStandardSize = _cartonSizeType == standardCarton; bool isCustomSize = _cartonSizeType == customCarton; bool isNoneDefinedSize = _cartonSizeType == packageCarton; final senderBox = DisplayText( text: widget.sender.name, labelTextKey: "box.sender.title", iconData: MaterialCommunityIcons.account_arrow_right, subText: Text(widget.sender.fcsID ?? "", style: TextStyle(fontSize: 13, color: labelColor)), ); final consigneeBox = DisplayText( text: widget.consignee.name, labelTextKey: "box.consignee.title", iconData: MaterialCommunityIcons.account_arrow_left, subText: Text(widget.consignee.fcsID ?? '', style: TextStyle(fontSize: 13, color: labelColor)), ); final userRow = Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: senderBox, flex: 2, ), Flexible(child: consigneeBox) ], ); final deliveryTypeBox = Container( child: Row( children: [ Flexible( child: InkWell( onTap: () { setState(() { _selectedLastmile = delivery_caton; }); }, child: Row(children: [ LocalRadio( value: delivery_caton, groupValue: _selectedLastmile, onChanged: (p0) { setState(() { _selectedLastmile = delivery_caton; }); }, ), Flexible( child: Padding( padding: const EdgeInsets.only(left: 10), child: LocalText(context, 'box.delivery', fontSize: 15, color: _selectedLastmile == delivery_caton ? primaryColor : Colors.black), ), ) ]), )), Flexible( child: InkWell( onTap: () { setState(() { _selectedLastmile = pickup_carton; }); }, child: Row(children: [ LocalRadio( value: pickup_carton, groupValue: _selectedLastmile, onChanged: (p0) { setState(() { _selectedLastmile = pickup_carton; }); }, ), Flexible( child: Padding( padding: const EdgeInsets.only(left: 10), child: LocalText(context, 'box.pickup', fontSize: 15, color: _selectedLastmile == pickup_carton ? primaryColor : Colors.black), ), ) ]), ), ) ], ), ); final billRadioBox = Container( child: Row( children: [ Flexible( child: InkWell( onTap: () { setState(() { _billToValue = billToSender; }); }, child: Row(children: [ LocalRadio( value: billToSender, groupValue: _billToValue, onChanged: (p0) { setState(() { _billToValue = billToSender; }); }, ), Flexible( child: Padding( padding: const EdgeInsets.only(left: 10), child: LocalText(context, 'box.sender.title', fontSize: 15, color: _billToValue == billToSender ? primaryColor : Colors.black), ), ) ]), )), Flexible( child: InkWell( onTap: () { setState(() { _billToValue = billToConsignee; }); }, child: Row(children: [ LocalRadio( value: billToConsignee, groupValue: _billToValue, onChanged: (p0) { setState(() { _billToValue = billToConsignee; }); }, ), Flexible( child: Padding( padding: const EdgeInsets.only(left: 10), child: LocalText(context, 'box.consignee.title', fontSize: 15, color: _billToValue == billToConsignee ? primaryColor : Colors.black), ), ) ]), ), ) ], ), ); final continueBtn = ContinueButton(onTap: () { double l = double.tryParse(_lengthController.text) ?? 0; double w = double.tryParse(_widthController.text) ?? 0; double h = double.tryParse(_heightController.text) ?? 0; if (_shipment == null) { showMsgDialog(context, "Error", "Please select shipment"); return; } if (isStandardSize && _selectStandardSize == null && !isCustomSize && !isNoneDefinedSize) { showMsgDialog( context, "Error", "Please select the standard carton size"); return; } if (isCustomSize && !isStandardSize && !isNoneDefinedSize && (l == 0 || w == 0 || h == 0)) { showMsgDialog(context, "Error", "Please add the carton size"); return; } if (widget.onContinue != null) { widget.onContinue!( _selectedLastmile, _billToValue, _shipment!, _cartonSizeType, standardSize: _selectStandardSize, length: l, width: w, height: h); } }); final previousBtn = PreviousButton(onTap: () { if (widget.onPrevious != null) { widget.onPrevious!(); } }); final standardSizeBox = Padding( padding: const EdgeInsets.only(left: 34.0, top: 8), child: IgnorePointer( ignoring: !isStandardSize, child: DropdownButton( isDense: true, value: _selectStandardSize, style: TextStyle(color: Colors.black, fontSize: 14), underline: Container(height: 1, color: Colors.grey), onChanged: (newValue) { setState(() { if (newValue?.name == MANAGE_CARTONSIZE) { _selectStandardSize = null; _manageCartonSize(); return; } _selectStandardSize = newValue!; }); }, isExpanded: true, items: cartonSizes.map>((CartonSize value) { return DropdownMenuItem( value: value, child: value.name == MANAGE_CARTONSIZE ? Text("${value.name}", style: TextStyle( color: isStandardSize ? secondaryColor : labelColor)) : Row( children: [ Text("${value.name} - ", style: TextStyle( color: isStandardSize ? Colors.black : labelColor)), Text( "${value.length.toInt()}”x${value.width.toInt()}”x${value.height.toInt()}”", style: TextStyle(color: labelColor)), ], ), ); }).toList(), ), ), ); final lengthBox = BoxSizePicker( lableKey: 'box.length', controller: _lengthController, enable: isCustomSize); final widthBox = BoxSizePicker( lableKey: 'box.width', controller: _widthController, enable: isCustomSize); final heightBox = BoxSizePicker( lableKey: 'box.height', controller: _heightController, enable: isCustomSize); final customSizeBox = Padding( padding: const EdgeInsets.only(left: 34.0), child: Row( children: [ Flexible(child: lengthBox), Flexible(child: widthBox), Flexible(child: heightBox) ], ), ); final cartonSizedBox = Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // standard carton size InkWell( onTap: () { setState(() { _cartonSizeType = standardCarton; }); }, child: Row(children: [ LocalRadio( value: standardCarton, groupValue: _cartonSizeType, onChanged: (p0) { setState(() { _cartonSizeType = standardCarton; }); }, ), Flexible( child: Padding( padding: const EdgeInsets.only(left: 10), child: LocalText(context, 'box.standard_carton_size', fontSize: 15, color: isStandardSize ? primaryColor : labelColor), ), ) ]), ), standardSizeBox, const SizedBox(height: 20), // custom size InkWell( onTap: () { setState(() { _cartonSizeType = customCarton; }); }, child: Row(children: [ LocalRadio( value: customCarton, groupValue: _cartonSizeType, onChanged: (p0) { setState(() { _cartonSizeType = customCarton; }); }, ), Flexible( child: Padding( padding: const EdgeInsets.only(left: 10), child: LocalText(context, 'box.custom_size', fontSize: 15, color: isCustomSize ? primaryColor : Colors.black54), ), ) ]), ), customSizeBox, const SizedBox(height: 10), // not defined size InkWell( onTap: () { setState(() { _cartonSizeType = packageCarton; }); }, child: Row(children: [ LocalRadio( value: packageCarton, groupValue: _cartonSizeType, onChanged: (p0) { setState(() { _cartonSizeType = packageCarton; }); }, ), Flexible( child: Padding( padding: const EdgeInsets.only(left: 10), child: LocalText(context, 'box.package_size', fontSize: 15, color: isNoneDefinedSize ? primaryColor : Colors.black54), ), ) ]), ), Padding( padding: const EdgeInsets.only(left: 34.0), child: Text( "No defined size", style: TextStyle( fontSize: 14, color: isNoneDefinedSize ? Colors.black : labelColor), ), ) ], ); final fcsShipmentsBox = Container( padding: EdgeInsets.only(top: 10), child: LocalDropdown( callback: (v) { setState(() { _shipment = v; }); }, labelKey: "box.shipment", iconData: Ionicons.ios_airplane, display: (u) => u.shipmentNumber, selectedValue: _shipment, values: _shipments, )); return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: ListView( padding: EdgeInsets.only(left: 10, right: 10), children: [ const SizedBox(height: 8), userRow, LocalTitle(textKey: "box.bill_to", topPadding: 8), const SizedBox(height: 5), billRadioBox, const SizedBox(height: 8), LocalTitle(textKey: "box.select.delivery"), const SizedBox(height: 5), deliveryTypeBox, const SizedBox(height: 8), LocalTitle(textKey: "box.select_carton_size"), const SizedBox(height: 8), cartonSizedBox, const SizedBox(height: 8), LocalTitle(textKey: "box.select_shipment"), const SizedBox(height: 5), fcsShipmentsBox, const SizedBox(height: 30) ], )), Padding( padding: const EdgeInsets.only(left: 15, right: 15), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ widget.onPrevious == null ? const SizedBox() : previousBtn, continueBtn // warehouse != null ? continueBtn : const SizedBox(), ], ), ), const SizedBox( height: 20, ), ], ); } _manageCartonSize() { Navigator.push( context, CupertinoPageRoute(builder: (context) => CartonSizeList()), ); } }