add validation in input form field

This commit is contained in:
sma
2024-02-28 17:07:23 +06:30
parent c06da8a60c
commit e00545a3d7
8 changed files with 278 additions and 177 deletions

View File

@@ -24,6 +24,7 @@ class _CargoEditorState extends State<CargoEditor> {
bool _isLoading = false;
late CargoType _cargo;
bool _isNew = false;
final _cargoFormKey = GlobalKey<FormState>();
@override
void initState() {
@@ -47,11 +48,26 @@ class _CargoEditorState extends State<CargoEditor> {
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<CargoEditor> {
)
],
),
body: Container(
padding: EdgeInsets.all(18),
child: Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
typeBox,
rateBox,
SizedBox(height: 30),
],
body: Form(
key: _cargoFormKey,
child: Container(
padding: EdgeInsets.all(18),
child: Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
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<CargoEditor> {
}
_save() async {
if (_rateController.text == "") {
showMsgDialog(context, "Error", "Please insert rate");
if (!_cargoFormKey.currentState!.validate()) {
return;
}
setState(() {