mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 05:26:47 +00:00
Refactor showFormDialog function
This commit is contained in:
parent
07017c4745
commit
d290f98445
@ -39,14 +39,6 @@ class _CompanyDetailState extends RefreshableState<CompanyDetailWidget> {
|
||||
// TODO
|
||||
}
|
||||
|
||||
void _saveCompany(Map<String, String> values) async {
|
||||
Navigator.of(context).pop();
|
||||
|
||||
await company.update(values: values);
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
void editCompanyDialog() {
|
||||
|
||||
// Values which can be edited
|
||||
@ -54,55 +46,7 @@ class _CompanyDetailState extends RefreshableState<CompanyDetailWidget> {
|
||||
var _description;
|
||||
var _website;
|
||||
|
||||
showFormDialog(L10().edit,
|
||||
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;
|
||||
},
|
||||
)
|
||||
]
|
||||
);
|
||||
// TODO - API form
|
||||
}
|
||||
|
||||
List<Widget> _companyTiles() {
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user