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, TextEditingController> _controllers = {};
|
||||
|
||||
BuildContext? _dialogContext;
|
||||
|
||||
/// Zeigt den vorher zusammengebauten Dialog an
|
||||
@@ -90,38 +92,38 @@ class DynamicDialog {
|
||||
);
|
||||
}
|
||||
|
||||
return AlertDialog(
|
||||
backgroundColor: backgroundColor,
|
||||
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,
|
||||
return SingleChildScrollView(
|
||||
child: AlertDialog(
|
||||
backgroundColor: backgroundColor,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(borderRadius),
|
||||
),
|
||||
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(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (content != null) content!,
|
||||
...inputFields.map(
|
||||
(final DialogInputField field) => Padding(
|
||||
(final DialogInputField field) => Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 6),
|
||||
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) {
|
||||
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(
|
||||
final DialogInputField inputField,
|
||||
final DialogAction primaryAction,
|
||||
) {
|
||||
_values[inputField.id] = inputField.initialValue;
|
||||
if (_values[inputField.id] == null) {
|
||||
_values[inputField.id] = inputField.initialValue;
|
||||
}
|
||||
|
||||
if (inputField.inputType == DialogInputFieldTypeEnum.date) {
|
||||
return DynamicDateTimeField(
|
||||
initialValue: inputField.initialValue,
|
||||
initialValue: _values[inputField.id],
|
||||
autofocus: inputField.autoFocus,
|
||||
onChanged: (final value) {
|
||||
inputField.onChanged?.call(value);
|
||||
@@ -175,24 +187,20 @@ class DynamicDialog {
|
||||
),
|
||||
);
|
||||
} else if (inputField.inputType == DialogInputFieldTypeEnum.select) {
|
||||
DialogInputFieldSelectItem? initialValue;
|
||||
|
||||
if (inputField.initialValue is Enum) {
|
||||
final Enum inputFieldInitialValue = inputField.initialValue;
|
||||
if (_values[inputField.id] is Enum) {
|
||||
final Enum inputFieldInitialValue = _values[inputField.id];
|
||||
|
||||
for (final DialogInputFieldSelectItem value in inputField.selectItems) {
|
||||
if (value.id == inputFieldInitialValue.index) {
|
||||
initialValue = value;
|
||||
_values[inputField.id] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_values[inputField.id] = initialValue;
|
||||
|
||||
return DropdownSearch<DialogInputFieldSelectItem>(
|
||||
items: (final f, final cs) => inputField.selectItems,
|
||||
itemAsString: (final DialogInputFieldSelectItem value) => value.value,
|
||||
selectedItem: initialValue,
|
||||
selectedItem: _values[inputField.id],
|
||||
onChanged: (final DialogInputFieldSelectItem? value) {
|
||||
inputField.onChanged?.call(value);
|
||||
_values[inputField.id] = value;
|
||||
@@ -211,12 +219,14 @@ class DynamicDialog {
|
||||
) => v1.id == v2.id,
|
||||
);
|
||||
} else {
|
||||
if (_controllers[inputField.id] == null) {
|
||||
_controllers[inputField.id] = TextEditingController(
|
||||
text: (_values[inputField.id] ?? '').toString()
|
||||
);
|
||||
}
|
||||
|
||||
return TextField(
|
||||
controller: TextEditingController(
|
||||
text: inputField.initialValue is String
|
||||
? inputField.initialValue
|
||||
: '',
|
||||
),
|
||||
controller: _controllers[inputField.id],
|
||||
autofocus: inputField.autoFocus,
|
||||
keyboardType: inputField.keyboardType,
|
||||
obscureText: inputField.obscureText,
|
||||
@@ -230,10 +240,27 @@ class DynamicDialog {
|
||||
isDense: true,
|
||||
),
|
||||
onSubmitted: (_) {
|
||||
close();
|
||||
primaryAction.onPressed?.call(_values);
|
||||
_submit(primaryAction);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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