Fix: Behebt einen Fehler, welcher beim Öffnen der Tastatur die Werte löscht
This commit is contained in:
@@ -60,6 +60,8 @@ class DynamicDialog {
|
|||||||
|
|
||||||
final Map<String, dynamic> _values;
|
final Map<String, dynamic> _values;
|
||||||
|
|
||||||
|
final Map<String, TextEditingController> _controllers = {};
|
||||||
|
|
||||||
BuildContext? _dialogContext;
|
BuildContext? _dialogContext;
|
||||||
|
|
||||||
/// Zeigt den vorher zusammengebauten Dialog an
|
/// Zeigt den vorher zusammengebauten Dialog an
|
||||||
@@ -90,38 +92,38 @@ class DynamicDialog {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return AlertDialog(
|
return SingleChildScrollView(
|
||||||
backgroundColor: backgroundColor,
|
child: AlertDialog(
|
||||||
shape: RoundedRectangleBorder(
|
backgroundColor: backgroundColor,
|
||||||
borderRadius: BorderRadius.circular(borderRadius),
|
shape: RoundedRectangleBorder(
|
||||||
),
|
borderRadius: BorderRadius.circular(borderRadius),
|
||||||
title: Column(
|
|
||||||
mainAxisSize: MainAxisSize.min,
|
|
||||||
children: [
|
|
||||||
if (icon != null)
|
|
||||||
Icon(icon, size: 48, color: theme.colorScheme.primary),
|
|
||||||
if (title != null)
|
|
||||||
Padding(
|
|
||||||
padding: const EdgeInsets.only(top: 8),
|
|
||||||
child: Text(
|
|
||||||
title!,
|
|
||||||
style: theme.textTheme.titleLarge,
|
|
||||||
textAlign: TextAlign.center,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
content: ConstrainedBox(
|
|
||||||
constraints: BoxConstraints(
|
|
||||||
maxHeight: MediaQuery.of(ctx).size.height * 0.7,
|
|
||||||
),
|
),
|
||||||
child: SingleChildScrollView(
|
title: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
if (icon != null)
|
||||||
|
Icon(icon, size: 48, color: theme.colorScheme.primary),
|
||||||
|
if (title != null)
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.only(top: 8),
|
||||||
|
child: Text(
|
||||||
|
title!,
|
||||||
|
style: theme.textTheme.titleLarge,
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
content: ConstrainedBox(
|
||||||
|
constraints: BoxConstraints(
|
||||||
|
maxHeight: MediaQuery.of(ctx).size.height * 0.7,
|
||||||
|
),
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
if (content != null) content!,
|
if (content != null) content!,
|
||||||
...inputFields.map(
|
...inputFields.map(
|
||||||
(final DialogInputField field) => Padding(
|
(final DialogInputField field) => Padding(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 6),
|
padding: const EdgeInsets.symmetric(vertical: 6),
|
||||||
child: _getInputField(field, primaryAction),
|
child: _getInputField(field, primaryAction),
|
||||||
),
|
),
|
||||||
@@ -129,18 +131,8 @@ class DynamicDialog {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
actions: actions.map(_getAction).toList(),
|
||||||
),
|
),
|
||||||
actions: actions
|
|
||||||
.map(
|
|
||||||
(final action) => TextButton(
|
|
||||||
onPressed: () {
|
|
||||||
close();
|
|
||||||
action.onPressed?.call(_values);
|
|
||||||
},
|
|
||||||
child: Text(action.label),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.toList(),
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -152,17 +144,37 @@ class DynamicDialog {
|
|||||||
if (_dialogContext != null) {
|
if (_dialogContext != null) {
|
||||||
Navigator.of(_dialogContext!).pop();
|
Navigator.of(_dialogContext!).pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_values.clear();
|
||||||
|
_controllers.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _submit(final DialogAction action) {
|
||||||
|
final Map<String, dynamic> values = {}
|
||||||
|
..addEntries(_values.entries);
|
||||||
|
|
||||||
|
_controllers.forEach((
|
||||||
|
final String id,
|
||||||
|
final TextEditingController controller,
|
||||||
|
) {
|
||||||
|
values[id] = controller.text;
|
||||||
|
});
|
||||||
|
|
||||||
|
close();
|
||||||
|
action.onPressed?.call(values);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _getInputField(
|
Widget _getInputField(
|
||||||
final DialogInputField inputField,
|
final DialogInputField inputField,
|
||||||
final DialogAction primaryAction,
|
final DialogAction primaryAction,
|
||||||
) {
|
) {
|
||||||
_values[inputField.id] = inputField.initialValue;
|
if (_values[inputField.id] == null) {
|
||||||
|
_values[inputField.id] = inputField.initialValue;
|
||||||
|
}
|
||||||
|
|
||||||
if (inputField.inputType == DialogInputFieldTypeEnum.date) {
|
if (inputField.inputType == DialogInputFieldTypeEnum.date) {
|
||||||
return DynamicDateTimeField(
|
return DynamicDateTimeField(
|
||||||
initialValue: inputField.initialValue,
|
initialValue: _values[inputField.id],
|
||||||
autofocus: inputField.autoFocus,
|
autofocus: inputField.autoFocus,
|
||||||
onChanged: (final value) {
|
onChanged: (final value) {
|
||||||
inputField.onChanged?.call(value);
|
inputField.onChanged?.call(value);
|
||||||
@@ -175,24 +187,20 @@ class DynamicDialog {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
} else if (inputField.inputType == DialogInputFieldTypeEnum.select) {
|
} else if (inputField.inputType == DialogInputFieldTypeEnum.select) {
|
||||||
DialogInputFieldSelectItem? initialValue;
|
if (_values[inputField.id] is Enum) {
|
||||||
|
final Enum inputFieldInitialValue = _values[inputField.id];
|
||||||
if (inputField.initialValue is Enum) {
|
|
||||||
final Enum inputFieldInitialValue = inputField.initialValue;
|
|
||||||
|
|
||||||
for (final DialogInputFieldSelectItem value in inputField.selectItems) {
|
for (final DialogInputFieldSelectItem value in inputField.selectItems) {
|
||||||
if (value.id == inputFieldInitialValue.index) {
|
if (value.id == inputFieldInitialValue.index) {
|
||||||
initialValue = value;
|
_values[inputField.id] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_values[inputField.id] = initialValue;
|
|
||||||
|
|
||||||
return DropdownSearch<DialogInputFieldSelectItem>(
|
return DropdownSearch<DialogInputFieldSelectItem>(
|
||||||
items: (final f, final cs) => inputField.selectItems,
|
items: (final f, final cs) => inputField.selectItems,
|
||||||
itemAsString: (final DialogInputFieldSelectItem value) => value.value,
|
itemAsString: (final DialogInputFieldSelectItem value) => value.value,
|
||||||
selectedItem: initialValue,
|
selectedItem: _values[inputField.id],
|
||||||
onChanged: (final DialogInputFieldSelectItem? value) {
|
onChanged: (final DialogInputFieldSelectItem? value) {
|
||||||
inputField.onChanged?.call(value);
|
inputField.onChanged?.call(value);
|
||||||
_values[inputField.id] = value;
|
_values[inputField.id] = value;
|
||||||
@@ -211,12 +219,14 @@ class DynamicDialog {
|
|||||||
) => v1.id == v2.id,
|
) => v1.id == v2.id,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
if (_controllers[inputField.id] == null) {
|
||||||
|
_controllers[inputField.id] = TextEditingController(
|
||||||
|
text: (_values[inputField.id] ?? '').toString()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return TextField(
|
return TextField(
|
||||||
controller: TextEditingController(
|
controller: _controllers[inputField.id],
|
||||||
text: inputField.initialValue is String
|
|
||||||
? inputField.initialValue
|
|
||||||
: '',
|
|
||||||
),
|
|
||||||
autofocus: inputField.autoFocus,
|
autofocus: inputField.autoFocus,
|
||||||
keyboardType: inputField.keyboardType,
|
keyboardType: inputField.keyboardType,
|
||||||
obscureText: inputField.obscureText,
|
obscureText: inputField.obscureText,
|
||||||
@@ -230,10 +240,27 @@ class DynamicDialog {
|
|||||||
isDense: true,
|
isDense: true,
|
||||||
),
|
),
|
||||||
onSubmitted: (_) {
|
onSubmitted: (_) {
|
||||||
close();
|
_submit(primaryAction);
|
||||||
primaryAction.onPressed?.call(_values);
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget _getAction(final DialogAction action) {
|
||||||
|
if (action.isPrimary) {
|
||||||
|
return ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
_submit(action);
|
||||||
|
},
|
||||||
|
child: Text(action.label),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return TextButton(
|
||||||
|
onPressed: () {
|
||||||
|
_submit(action);
|
||||||
|
},
|
||||||
|
child: Text(action.label),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user