diff --git a/lib/pages/customer/invitation_create.dart b/lib/pages/customer/invitation_create.dart index a148e81..f937e7c 100644 --- a/lib/pages/customer/invitation_create.dart +++ b/lib/pages/customer/invitation_create.dart @@ -19,6 +19,7 @@ class _InvitationCreateState extends State { bool _isLoading = false; late String dialCode; + final _inviteFormKey = GlobalKey(); @override void initState() { @@ -34,9 +35,17 @@ class _InvitationCreateState extends State { @override Widget build(BuildContext context) { final nameBox = InputText( - labelTextKey: 'customer.name', - iconData: Icons.person, - controller: _nameController); + labelTextKey: 'customer.name', + iconData: Icons.person, + controller: _nameController, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) { + if (value == null || value.isEmpty) { + return "Please insert name"; + } + return null; + }, + ); return LocalProgress( inAsyncCall: _isLoading, @@ -56,73 +65,88 @@ class _InvitationCreateState extends State { } }, ), - body: Container( - padding: EdgeInsets.all(18), - child: ListView( - children: [ - nameBox, - SizedBox(height: 10), - Row( - children: [ - Padding( - padding: const EdgeInsets.all(8.0), - child: Icon( - Icons.phone, - color: primaryColor, + body: Form( + key: _inviteFormKey, + child: Container( + padding: EdgeInsets.all(18), + child: ListView( + children: [ + nameBox, + SizedBox(height: 10), + Row( + children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: Icon( + Icons.phone, + color: primaryColor, + ), ), - ), - Container( - decoration: BoxDecoration( - border: - Border.all(color: Colors.grey.shade400, width: 1), - borderRadius: BorderRadius.all(Radius.circular(12.0))), - child: CountryCodePicker( - onChanged: _countryChange, - initialSelection: dialCode, - countryFilter: ['mm', 'us'], - showCountryOnly: false, - showOnlyCountryWhenClosed: false, - alignLeft: false, - textStyle: TextStyle(fontSize: 16, color: Colors.black87), - searchDecoration: InputDecoration( - focusedBorder: UnderlineInputBorder( - borderSide: - BorderSide(color: Colors.black, width: 1.0))), + Container( + decoration: BoxDecoration( + border: + Border.all(color: Colors.grey.shade400, width: 1), + borderRadius: + BorderRadius.all(Radius.circular(12.0))), + child: CountryCodePicker( + onChanged: _countryChange, + initialSelection: dialCode, + countryFilter: ['mm', 'us'], + showCountryOnly: false, + showOnlyCountryWhenClosed: false, + alignLeft: false, + textStyle: + TextStyle(fontSize: 16, color: Colors.black87), + searchDecoration: InputDecoration( + focusedBorder: UnderlineInputBorder( + borderSide: BorderSide( + color: Colors.black, width: 1.0))), + ), ), - ), - SizedBox( - width: 10, - ), - Flexible( - child: Container( - padding: EdgeInsets.only(top: 10, bottom: 10), - child: TextFormField( - controller: _phoneController, - cursorColor: primaryColor, - textAlign: TextAlign.left, - keyboardType: TextInputType.phone, - style: TextStyle( - fontSize: 18, - ), - decoration: InputDecoration( - fillColor: Colors.white, - labelText: getLocalString(context, "customer.phone"), - labelStyle: - TextStyle(fontSize: 16, color: Colors.grey), - filled: true, - focusedBorder: UnderlineInputBorder( - borderSide: - BorderSide(color: Colors.grey, width: 1.0)), + SizedBox( + width: 10, + ), + Flexible( + child: Container( + padding: EdgeInsets.only(top: 10, bottom: 10), + child: TextFormField( + controller: _phoneController, + cursorColor: primaryColor, + textAlign: TextAlign.left, + keyboardType: TextInputType.phone, + style: TextStyle( + fontSize: 18, + ), + decoration: InputDecoration( + fillColor: Colors.white, + labelText: + getLocalString(context, "customer.phone"), + labelStyle: + TextStyle(fontSize: 16, color: Colors.grey), + filled: true, + focusedBorder: UnderlineInputBorder( + borderSide: BorderSide( + color: Colors.grey, width: 1.0)), + errorStyle: TextStyle( + color: dangerColor, + )), + validator: (value) { + if (value == null || value.isEmpty) { + return "Please insert phone no"; + } + return null; + }, + autovalidateMode: AutovalidateMode.onUserInteraction, ), ), ), - ), - ], - ), - SizedBox(height: 20), - fcsButton(context, getLocalString(context, "invite.btn"), - callack: _invite), - ], + ], + ), + SizedBox(height: 20), + fcsButton(context, getLocalString(context, "invite.btn"), + callack: _invite), + ], + ), ), ), ), @@ -138,9 +162,9 @@ class _InvitationCreateState extends State { _invite() async { String userName = _nameController.text; String phoneNumber = dialCode + _phoneController.text; - if (userName == "" || phoneNumber == "") { - showMsgDialog(context, "Error", "Invalid name or phone number"); - return; + + if (!_inviteFormKey.currentState!.validate()) { + return null; } setState(() { _isLoading = true; diff --git a/lib/pages/fcs_shipment/fcs_shipment_editor.dart b/lib/pages/fcs_shipment/fcs_shipment_editor.dart index a5f3448..92985e8 100644 --- a/lib/pages/fcs_shipment/fcs_shipment_editor.dart +++ b/lib/pages/fcs_shipment/fcs_shipment_editor.dart @@ -121,6 +121,7 @@ class _FcsShipmentEditorState extends State { labelTextKey: "FCSshipment.number", iconData: Ionicons.ios_airplane, controller: _shipmentNumberController, + autovalidateMode: AutovalidateMode.onUserInteraction, validator: (value) { if (value!.isEmpty) { return "Enter shipment number"; @@ -132,6 +133,7 @@ class _FcsShipmentEditorState extends State { labelTextKey: "FCSshipment.cutoff_date", iconData: Icons.date_range, controller: _cutoffDateController, + autovalidateMode: AutovalidateMode.onUserInteraction, validator: (value) { if (value!.isEmpty) { return "Select cutoff date"; @@ -143,6 +145,7 @@ class _FcsShipmentEditorState extends State { labelTextKey: "FCSshipment.ETA", iconData: Icons.date_range, controller: _arrivalDateController, + autovalidateMode: AutovalidateMode.onUserInteraction, validator: (value) { if (value!.isEmpty) { return "Select ETA date"; diff --git a/lib/pages/rates/cargo_editor.dart b/lib/pages/rates/cargo_editor.dart index e9db900..51a5407 100644 --- a/lib/pages/rates/cargo_editor.dart +++ b/lib/pages/rates/cargo_editor.dart @@ -24,6 +24,7 @@ class _CargoEditorState extends State { bool _isLoading = false; late CargoType _cargo; bool _isNew = false; + final _cargoFormKey = GlobalKey(); @override void initState() { @@ -47,11 +48,26 @@ class _CargoEditorState extends State { final typeBox = InputText( labelTextKey: 'cargo.type', iconData: Icons.text_format, - controller: _descController); + controller: _descController, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value){ + if(value==null || value.isEmpty){ + return "Please insert cargo type"; + } + return null; + },); final rateBox = InputText( - labelTextKey: 'cargo.rate', - iconData: Icons.attach_money, - controller: _rateController); + labelTextKey: 'cargo.rate', + iconData: Icons.attach_money, + controller: _rateController, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) { + if (value == null || value.isEmpty) { + return "Please insert rate"; + } + return null; + }, + ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( @@ -78,23 +94,26 @@ class _CargoEditorState extends State { ) ], ), - body: Container( - padding: EdgeInsets.all(18), - child: Column( - children: [ - Expanded( - child: ListView( - children: [ - typeBox, - rateBox, - SizedBox(height: 30), - ], + body: Form( + key: _cargoFormKey, + child: Container( + padding: EdgeInsets.all(18), + child: Column( + children: [ + Expanded( + child: ListView( + children: [ + typeBox, + rateBox, + SizedBox(height: 30), + ], + ), ), - ), - fcsButton(context, getLocalString(context, "btn.save"), - callack: _save), - SizedBox(height: 10) - ], + fcsButton(context, getLocalString(context, "btn.save"), + callack: _save), + SizedBox(height: 10) + ], + ), ), ), ), @@ -102,8 +121,7 @@ class _CargoEditorState extends State { } _save() async { - if (_rateController.text == "") { - showMsgDialog(context, "Error", "Please insert rate"); + if (!_cargoFormKey.currentState!.validate()) { return; } setState(() { diff --git a/lib/pages/rates/custom_editor.dart b/lib/pages/rates/custom_editor.dart index cde6be0..43d4dad 100644 --- a/lib/pages/rates/custom_editor.dart +++ b/lib/pages/rates/custom_editor.dart @@ -26,6 +26,7 @@ class _CustomEditorState extends State { bool _isLoading = false; CargoType _custom = new CargoType(); bool _isNew = false; + final _customFormKey = GlobalKey(); @override void initState() { @@ -50,16 +51,39 @@ class _CustomEditorState extends State { final productBox = InputText( labelTextKey: 'rate.cutom.product_type', iconData: FontAwesomeIcons.weightHanging, - controller: _productController); + autovalidateMode: AutovalidateMode.onUserInteraction, + controller: _productController, + validator: (value){ + if(value==null || value.isEmpty){ + return "Please insert product type"; + } + return null; + },); final feeBox = InputText( - labelTextKey: 'rate.custom.fee', - iconData: Icons.attach_money, - controller: _feeController); + labelTextKey: 'rate.custom.fee', + iconData: Icons.attach_money, + controller: _feeController, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) { + if (value == null || value.isEmpty) { + return "Please insert fee"; + } + return null; + }, + ); final shipmentRateBox = InputText( - labelTextKey: 'rate.custom.shipment_rate', - iconData: Icons.attach_money, - controller: _shipmentRateController); + labelTextKey: 'rate.custom.shipment_rate', + iconData: Icons.attach_money, + controller: _shipmentRateController, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) { + if (value == null || value.isEmpty) { + return "Please insert shipment rate"; + } + return null; + }, + ); return LocalProgress( inAsyncCall: _isLoading, child: Scaffold( @@ -86,24 +110,27 @@ class _CustomEditorState extends State { ) ], ), - body: Container( - padding: EdgeInsets.all(18), - child: Column( - children: [ - Expanded( - child: ListView( - children: [ - productBox, - feeBox, - shipmentRateBox, - SizedBox(height: 30), - ], + body: Form( + key: _customFormKey, + child: Container( + padding: EdgeInsets.all(18), + child: Column( + children: [ + Expanded( + child: ListView( + children: [ + productBox, + feeBox, + shipmentRateBox, + SizedBox(height: 30), + ], + ), ), - ), - fcsButton(context, getLocalString(context, "btn.save"), - callack: _save), - SizedBox(height: 10) - ], + fcsButton(context, getLocalString(context, "btn.save"), + callack: _save), + SizedBox(height: 10) + ], + ), ), ), ), @@ -111,13 +138,7 @@ class _CustomEditorState extends State { } _save() async { - if (_feeController.text == "") { - showMsgDialog(context, "Error", "Please insert fee"); - return; - } - - if (_shipmentRateController.text == "") { - showMsgDialog(context, "Error", "Please insert shipment rate"); + if (!_customFormKey.currentState!.validate()) { return; } diff --git a/lib/pages/rates/discount_by_weight_editor.dart b/lib/pages/rates/discount_by_weight_editor.dart index 53791a8..5a6308f 100644 --- a/lib/pages/rates/discount_by_weight_editor.dart +++ b/lib/pages/rates/discount_by_weight_editor.dart @@ -24,6 +24,7 @@ class _DiscountByWeightEditorState extends State { bool _isLoading = false; bool _isNew = false; DiscountByWeight _discountByWeight = new DiscountByWeight(); + final _discountFormKey = GlobalKey(); @override void initState() { @@ -46,13 +47,29 @@ class _DiscountByWeightEditorState extends State { @override Widget build(BuildContext context) { final weightBox = InputText( - labelTextKey: 'rate.discount.weight', - iconData: FontAwesomeIcons.weightHanging, - controller: _weightController); + labelTextKey: 'rate.discount.weight', + iconData: FontAwesomeIcons.weightHanging, + controller: _weightController, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) { + if (value == null || value.isEmpty) { + return "Please inset weight"; + } + return null; + }, + ); final discountRateBox = InputText( - labelTextKey: 'rate.discount.rate', - iconData: Icons.attach_money, - controller: _discountController); + labelTextKey: 'rate.discount.rate', + iconData: Icons.attach_money, + controller: _discountController, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) { + if (value == null || value.isEmpty) { + return "Please insert discount rate"; + } + return null; + }, + ); return LocalProgress( inAsyncCall: _isLoading, @@ -80,23 +97,26 @@ class _DiscountByWeightEditorState extends State { ) ], ), - body: Container( - padding: EdgeInsets.all(18), - child: Column( - children: [ - Expanded( - child: ListView( - children: [ - weightBox, - discountRateBox, - SizedBox(height: 30), - ], + body: Form( + key: _discountFormKey, + child: Container( + padding: EdgeInsets.all(18), + child: Column( + children: [ + Expanded( + child: ListView( + children: [ + weightBox, + discountRateBox, + SizedBox(height: 30), + ], + ), ), - ), - fcsButton(context, getLocalString(context, "btn.save"), - callack: _save), - SizedBox(height: 10) - ], + fcsButton(context, getLocalString(context, "btn.save"), + callack: _save), + SizedBox(height: 10) + ], + ), ), ), ), @@ -104,13 +124,7 @@ class _DiscountByWeightEditorState extends State { } _save() async { - if (_weightController.text == "") { - showMsgDialog(context, "Error", "Please insert weight"); - return; - } - - if (_discountController.text == "") { - showMsgDialog(context, "Error", "Please insert discount rate"); + if (!_discountFormKey.currentState!.validate()) { return; } diff --git a/lib/pages/receiving/receiving_editor.dart b/lib/pages/receiving/receiving_editor.dart index 2529530..78e9b92 100644 --- a/lib/pages/receiving/receiving_editor.dart +++ b/lib/pages/receiving/receiving_editor.dart @@ -30,6 +30,7 @@ class _ReceivingEditorState extends State { bool _isLoading = false; late bool _isNew; User? user; + final _receiveFormKey=GlobalKey(); TextEditingController _trackingIDCtl = new TextEditingController(); TextEditingController _remarkCtl = new TextEditingController(); MultiImgController _multiImgController = MultiImgController(); @@ -93,6 +94,14 @@ class _ReceivingEditorState extends State { iconData: MaterialCommunityIcons.barcode_scan, labelTextKey: "receiving.tracking.id", controller: _trackingIDCtl, + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value){ + if(value==null || value.isEmpty){ + return "invalid tracking ID"; + } + return null; + }, + )), InkWell( onTap: _scan, @@ -159,29 +168,32 @@ class _ReceivingEditorState extends State { ), body: Padding( padding: const EdgeInsets.only(left: 12.0, right: 12), - child: ListView( - children: [ - trackingIDBox, - SizedBox( - height: 10, - ), - remarkBox, - Divider(), - img, - Divider(), - SizedBox( - height: 10, - ), - fcsIDBox, - namebox, - SizedBox( - height: 20, - ), - _isNew ? createButton : updateButton, - SizedBox( - height: 10, - ), - ], + child: Form( + key: _receiveFormKey, + child: ListView( + children: [ + trackingIDBox, + SizedBox( + height: 10, + ), + remarkBox, + Divider(), + img, + Divider(), + SizedBox( + height: 10, + ), + fcsIDBox, + namebox, + SizedBox( + height: 20, + ), + _isNew ? createButton : updateButton, + SizedBox( + height: 10, + ), + ], + ), ), ), )); @@ -216,8 +228,11 @@ class _ReceivingEditorState extends State { Package _p = Package(trackingID: _trackingIDCtl.text, remark: _remarkCtl.text); - if (_p.trackingID == null || _p.trackingID == "") { - showMsgDialog(context, "Error", "Invalid tracking ID!"); + // if (_p.trackingID == null || _p.trackingID == "") { + // showMsgDialog(context, "Error", "Invalid tracking ID!"); + // return; + // } + if (!_receiveFormKey.currentState!.validate()) { return; } diff --git a/lib/pages/widgets/input_date.dart b/lib/pages/widgets/input_date.dart index 6024790..491eb41 100644 --- a/lib/pages/widgets/input_date.dart +++ b/lib/pages/widgets/input_date.dart @@ -16,6 +16,7 @@ class InputDate extends StatelessWidget { final TextInputType? textInputType; final bool autoFocus; final String dateFormatString; + final AutovalidateMode? autovalidateMode; const InputDate( {Key? key, @@ -23,6 +24,7 @@ class InputDate extends StatelessWidget { required this.iconData, required this.controller, this.validator, + this.autovalidateMode, this.maxLines = 1, this.withBorder = false, this.borderColor, @@ -126,7 +128,8 @@ class InputDate extends StatelessWidget { borderSide: BorderSide( color: borderColor ?? dangerColor, width: 1.0)), ), - validator: validator), + validator: validator, + autovalidateMode: autovalidateMode,), ); } } diff --git a/lib/pages/widgets/input_text.dart b/lib/pages/widgets/input_text.dart index 9d09b07..f6f4397 100644 --- a/lib/pages/widgets/input_text.dart +++ b/lib/pages/widgets/input_text.dart @@ -9,6 +9,7 @@ class InputText extends StatelessWidget { final IconData? iconData; final TextEditingController? controller; final FormFieldValidator? validator; + final AutovalidateMode? autovalidateMode; final int maxLines; final bool withBorder; final Color? borderColor; @@ -26,6 +27,7 @@ class InputText extends StatelessWidget { this.iconData, this.controller, this.validator, + this.autovalidateMode, this.maxLines = 1, this.withBorder = false, this.borderColor, @@ -114,7 +116,8 @@ class InputText extends StatelessWidget { borderSide: BorderSide( color: borderColor ?? dangerColor, width: 1.0)), ), - validator: validator), + validator: validator, + autovalidateMode: autovalidateMode,), ); } }