2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-06-14 19:25:27 +00:00

Refactor showFormDialog function

This commit is contained in:
Oliver
2021-07-30 11:09:46 +10:00
parent 07017c4745
commit d290f98445
2 changed files with 31 additions and 102 deletions

View File

@ -184,62 +184,47 @@ Future<void> showTimeoutError() async {
await showServerError(L10().timeout, L10().noResponse);
}
void showFormDialog(String title, {String? acceptText, String? cancelText, GlobalKey<FormState>? key, List<Widget>? fields, List<Widget>? actions, Function? callback}) {
void showFormDialog(String title, {String? acceptText, String? cancelText, GlobalKey<FormState>? key, List<Widget>? fields, Function? callback}) {
BuildContext? dialogContext;
String _accept = acceptText ?? L10().save;
String _cancel = cancelText ?? L10().cancel;
// Undefined actions = OK + Cancel
if (actions == null) {
actions = <Widget>[
TextButton(
child: Text(_cancel),
onPressed: () {
// Close the form
var _ctx = dialogContext;
if (_ctx != null) {
Navigator.pop(_ctx);
}
}
),
TextButton(
child: Text(_accept),
onPressed: () {
var _key = key;
if (_key != null && _key.currentState != null) {
if (_key.currentState!.validate()) {
_key.currentState!.save();
// Close the dialog
var _ctx = dialogContext;
if (_ctx != null) {
Navigator.pop(_ctx);
}
// Callback
if (callback != null) {
callback();
}
}
}
}
)
];
}
List<Widget> _fields = fields ?? [];
OneContext().showDialog(
builder: (BuildContext context) {
dialogContext = context;
return AlertDialog(
title: Text(title),
actions: actions,
actions: <Widget>[
TextButton(
child: Text(_cancel),
onPressed: () {
// Close the form
Navigator.pop(context);
}
),
TextButton(
child: Text(_accept),
onPressed: () {
var _key = key;
if (_key != null && _key.currentState != null) {
if (_key.currentState!.validate()) {
_key.currentState!.save();
Navigator.pop(context);
// Callback
if (callback != null) {
callback();
}
}
}
}
)
],
content: Form(
key: key,
child: SingleChildScrollView(