2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 13:36:50 +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

@ -39,14 +39,6 @@ class _CompanyDetailState extends RefreshableState<CompanyDetailWidget> {
// TODO // TODO
} }
void _saveCompany(Map<String, String> values) async {
Navigator.of(context).pop();
await company.update(values: values);
refresh();
}
void editCompanyDialog() { void editCompanyDialog() {
// Values which can be edited // Values which can be edited
@ -54,55 +46,7 @@ class _CompanyDetailState extends RefreshableState<CompanyDetailWidget> {
var _description; var _description;
var _website; var _website;
showFormDialog(L10().edit, // TODO - API form
key: _editCompanyKey,
actions: <Widget>[
TextButton(
child: Text(L10().cancel),
onPressed: () {
Navigator.pop(context);
},
),
TextButton(
child: Text(L10().save),
onPressed: () {
if (_editCompanyKey.currentState!.validate()) {
_editCompanyKey.currentState!.save();
_saveCompany({
"name": _name,
"description": _description,
"website": _website,
});
}
},
),
],
fields: <Widget>[
StringField(
label: L10().name,
initial: company.name,
onSaved: (value) {
_name = value;
},
),
StringField(
label: L10().description,
initial: company.description,
onSaved: (value) {
_description = value;
},
),
StringField(
label: L10().website,
initial: company.website,
allowEmpty: true,
onSaved: (value) {
_website = value;
},
)
]
);
} }
List<Widget> _companyTiles() { List<Widget> _companyTiles() {

View File

@ -184,62 +184,47 @@ Future<void> showTimeoutError() async {
await showServerError(L10().timeout, L10().noResponse); 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 _accept = acceptText ?? L10().save;
String _cancel = cancelText ?? L10().cancel; 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 ?? []; List<Widget> _fields = fields ?? [];
OneContext().showDialog( OneContext().showDialog(
builder: (BuildContext context) { builder: (BuildContext context) {
dialogContext = context;
return AlertDialog( return AlertDialog(
title: Text(title), 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( content: Form(
key: key, key: key,
child: SingleChildScrollView( child: SingleChildScrollView(