import 'package:fcs/domain/constants.dart'; import 'package:fcs/domain/entities/carton.dart'; import 'package:fcs/domain/entities/user.dart'; import 'package:fcs/helpers/theme.dart'; import 'package:fcs/pages/user_search/user_search.dart'; import 'package:fcs/pages/widgets/display_text.dart'; import 'package:fcs/pages/widgets/fcs_id_icon.dart'; import 'package:fcs/pages/widgets/local_app_bar.dart'; import 'package:fcs/pages/widgets/local_radio_buttons.dart'; import 'package:fcs/pages/widgets/local_text.dart'; import 'package:fcs/pages/widgets/local_title.dart'; import 'package:fcs/pages/widgets/progress.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_vector_icons/flutter_vector_icons.dart'; import '../main/util.dart'; import 'carton_package_form.dart'; import 'carton_info.dart'; import 'mix_carton/mix_carton_form.dart'; import 'widget/carton_row.dart'; class CartonEditor extends StatefulWidget { final Carton? carton; CartonEditor({this.carton}); @override _CartonEditorState createState() => _CartonEditorState(); } class _CartonEditorState extends State { List _cartonTypes = [carton_from_packages, carton_mix_carton]; List _cartons = []; bool _isLoading = false; bool _isNew = false; String? _selectedCartonType; User? _consignee; User? _sender; Carton? _carton; @override void initState() { _init(); super.initState(); } _init() { if (widget.carton != null) { _carton = widget.carton; _selectedCartonType = _carton!.cartonType; _isNew = false; _consignee = User( id: _carton!.userID, fcsID: _carton!.fcsID, name: _carton!.userName); _sender = User( id: _carton!.senderID, fcsID: _carton!.senderFCSID, name: _carton!.senderName); } else { _carton = Carton(cargoTypes: [], packages: []); _isNew = true; _selectedCartonType = carton_from_packages; _cartons = []; _sender = User( name: "ptd-phyo44 kaelone", fcsID: "FCS-8X6V", phoneNumber: "+959444444444", id: "48u_4s-HiQeW-HwSqeRd9TSMWh3mLZfSk5rpaUEh_zw"); _consignee = User( id: "HsIwG88K-0_HSazgEy5QR27kcjkOvfv7_Sr1JP18Q1A", name: "One One", phoneNumber: "+959111111111", fcsID: "FCS-EFRF"); } if (mounted) { setState(() {}); } } @override Widget build(BuildContext context) { bool isFromPackages = _selectedCartonType == carton_from_packages; final createBtn = Row( mainAxisAlignment: MainAxisAlignment.center, children: [ InkWell( onTap: () { Navigator.pop(context); }, child: Container( alignment: Alignment.bottomRight, height: 45, width: 130, decoration: BoxDecoration( color: primaryColor, borderRadius: BorderRadius.circular(5), ), child: TextButton( onPressed: null, child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Flexible( child: LocalText(context, 'box.done.btn', color: Colors.white, fontSize: 15), ), const SizedBox(width: 5), const Icon( MaterialCommunityIcons.check_circle_outline, color: Colors.white, ), ], ), ), )), ], ); final cartonTypeBox = LocalRadioButtons( readOnly: !_isNew, values: _cartonTypes, selectedValue: _selectedCartonType, callback: (String? v) { setState(() { _selectedCartonType = v; }); }); final cartonTitleBox = LocalTitle( textKey: _cartons.isEmpty ? "box.shipment.boxes" : "box.cartion.count", translationVariables: _cartons.isEmpty ? null : ["${_cartons.length}"], trailing: Padding( padding: const EdgeInsets.only(right: 5), child: InkResponse( radius: 30, onTap: () { //for packages if (isFromPackages) { if (_sender == null) { showMsgDialog( context, "Error", "Please select sender's FCS ID"); return; } if (_consignee == null) { showMsgDialog( context, "Error", "Please select consignee's FCS ID"); return; } Navigator.push( context, CupertinoPageRoute( builder: (context) => CartonPackageForm( sender: _sender!, consignee: _consignee!, ))); } // for mix cartion else { Navigator.push( context, CupertinoPageRoute( builder: (context) => const MixCartonForm())); } }, child: Icon(Icons.add_circle, color: primaryColor), ), )); final consigneeSearchBox = Row( children: [ Flexible( child: Padding( padding: const EdgeInsets.only(left: 2), child: LocalText(context, "box.consignee.title", color: Colors.black, fontSize: 15), )), IconButton( icon: Icon(Icons.search, color: Colors.black), onPressed: () => searchUser(context, onUserSelect: (u) { setState(() { this._consignee = u; }); }, popPage: true)), ], ); final consigneefcsIDBox = DisplayText( text: _consignee != null ? _consignee!.fcsID : "", labelTextKey: "processing.fcs.id", icon: FcsIDIcon(), ); final consigneePhoneBox = DisplayText( text: _consignee != null ? _consignee!.phoneNumber : "", labelTextKey: "processing.phone", iconData: MaterialCommunityIcons.phone); final consigneeNameBox = DisplayText( text: _consignee != null ? _consignee!.name : "", labelTextKey: "processing.consignee.name", maxLines: 2, iconData: MaterialCommunityIcons.account_arrow_left); final consigneeBox = Container( child: Column( children: [ consigneeSearchBox, consigneefcsIDBox, consigneePhoneBox, consigneeNameBox ], ), ); final senderSearchBox = Row( children: [ Flexible( child: Padding( padding: const EdgeInsets.only(left: 2), child: LocalText(context, "box.sender.title", color: Colors.black, fontSize: 15), )), IconButton( icon: Icon(Icons.search, color: Colors.black), onPressed: () => searchUser(context, onUserSelect: (u) { setState(() { this._sender = u; }); }, popPage: true)), ], ); final senderIDBox = DisplayText( text: _sender != null ? _sender!.fcsID : "", labelTextKey: "processing.fcs.id", icon: FcsIDIcon()); final senderPhoneBox = DisplayText( text: _sender != null ? _sender!.phoneNumber : "", labelTextKey: "processing.phone", iconData: MaterialCommunityIcons.phone, ); final senderNameBox = DisplayText( text: _sender != null ? _sender!.name : "", labelTextKey: "processing.shipper.name", maxLines: 2, iconData: MaterialCommunityIcons.account_arrow_right); final senderBox = Container( child: Column( children: [ senderSearchBox, senderIDBox, senderPhoneBox, senderNameBox, ], ), ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( appBar: LocalAppBar( labelKey: _isNew ? "boxes.new" : "box.edit.title", backgroundColor: Colors.white, arrowColor: primaryColor, labelColor: primaryColor), body: ListView( padding: EdgeInsets.only(left: 10, right: 10), shrinkWrap: true, children: [ LocalTitle(textKey: "box.select.carton_type"), cartonTypeBox, isFromPackages ? Column( children: [ LocalTitle(textKey: "box.select.sender_and_consignee"), Row( children: [ Flexible(child: senderBox), Flexible(child: consigneeBox) ], ), ], ) : Container(), cartonTitleBox, Column(children: _getCartons(context, _cartons)), SizedBox(height: 20), createBtn, SizedBox(height: 20), ], ), ), ); } List _getCartons(BuildContext context, List cartons) { return cartons.map((c) { return InkWell( onTap: () async { Navigator.push( context, CupertinoPageRoute(builder: (context) => CartonInfo(carton: c)), ); }, child: CartonRow(box: c)); }).toList(); } }