2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 05:26:47 +00:00

Form updates

This commit is contained in:
Oliver 2021-07-20 19:14:44 +10:00
parent fd818740f7
commit ca6226a596
3 changed files with 81 additions and 49 deletions

View File

@ -63,6 +63,7 @@ class APIFormField {
Widget constructField() {
switch (type) {
case "string":
case "url":
return _constructString();
case "boolean":
return _constructBoolean();
@ -86,8 +87,9 @@ class APIFormField {
data["value"] = val;
},
validator: (value) {
// TODO - Custom field validation
if (required && (value == null || value.isEmpty)) {
return L10().valueCannotBeEmpty;
}
},
);
}
@ -193,14 +195,39 @@ Future<void> launchApiForm(String title, String url, Map<String, dynamic> fields
formFields.add(APIFormField(fieldName, remoteField));
}
List<Widget> widgets = [];
List<Widget> buildWidgets() {
List<Widget> widgets = [];
for (var ff in formFields) {
if (ff.hidden) {
continue;
}
for (var ff in formFields) {
if (!ff.hidden) {
widgets.add(ff.constructField());
if (ff.hasErrors()) {
for (String error in ff.errorMessages()) {
widgets.add(
ListTile(
title: Text(
error,
style: TextStyle(color: Color.fromRGBO(250, 50, 50, 1))
),
)
);
}
}
}
return widgets;
}
List<Widget> _widgets = buildWidgets();
void sendRequest(BuildContext context) async {
// Package up the form data
@ -252,44 +279,51 @@ Future<void> launchApiForm(String title, String url, Map<String, dynamic> fields
OneContext().showDialog(
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
actions: <Widget>[
// Cancel button
TextButton(
child: Text(L10().cancel),
onPressed: () {
Navigator.pop(context);
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text(title),
actions: <Widget>[
// Cancel button
TextButton(
child: Text(L10().cancel),
onPressed: () {
Navigator.pop(context);
if (onCancel != null) {
onCancel();
}
},
),
// Save button
TextButton(
child: Text(L10().save),
onPressed: () {
// Validate the form
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
if (onCancel != null) {
onCancel();
}
},
),
// Save button
TextButton(
child: Text(L10().save),
onPressed: () {
// Validate the form
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
sendRequest(context);
}
},
)
],
content: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: widgets
)
)
)
setState(() {
sendRequest(context);
_widgets = buildWidgets();
});
}
},
)
],
content: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: _widgets,
)
)
)
);
}
);
}
);

@ -1 +1 @@
Subproject commit 84f6ed3faf63cbf371016e134196400e5f822759
Subproject commit 0870256fb97a27ccf0ab73b4665b886ca0abb4aa

View File

@ -179,21 +179,19 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
var _keywords;
var _link;
launchApiForm(
"Edit Part",
L10().editPart,
part.url,
{
"name": {},
"description": {},
"IPN": {
"hidden": true,
"label": "My custom label!",
},
"IPN": {},
"keywords": {},
"active": {},
"link": {},
},
modelData: part.jsondata,
onSuccess: refresh,
onSuccess: refresh,
);
return;