fix: Textfields in dialogs on iOS
This commit is contained in:
parent
359399df21
commit
b841d8c09e
1 changed files with 107 additions and 70 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
|
|
@ -24,79 +25,115 @@ Future<String?> showTextInputDialog({
|
|||
TextInputType? keyboardType,
|
||||
int? maxLength,
|
||||
bool autocorrect = true,
|
||||
}) =>
|
||||
showAdaptiveDialog<String>(
|
||||
context: context,
|
||||
useRootNavigator: useRootNavigator,
|
||||
builder: (context) {
|
||||
final controller = TextEditingController(text: initialText);
|
||||
final error = ValueNotifier<String?>(null);
|
||||
return ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 512),
|
||||
child: AlertDialog.adaptive(
|
||||
title: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 256),
|
||||
child: Text(title),
|
||||
),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (message != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 16.0),
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 256),
|
||||
child: Text(message),
|
||||
),
|
||||
),
|
||||
ValueListenableBuilder<String?>(
|
||||
valueListenable: error,
|
||||
builder: (context, error, _) {
|
||||
return TextField(
|
||||
controller: controller,
|
||||
obscureText: obscureText,
|
||||
minLines: minLines,
|
||||
maxLines: maxLines,
|
||||
maxLength: maxLength,
|
||||
keyboardType: keyboardType,
|
||||
autocorrect: autocorrect,
|
||||
decoration: InputDecoration(
|
||||
errorText: error,
|
||||
hintText: hintText,
|
||||
labelText: labelText,
|
||||
prefixText: prefixText,
|
||||
suffixText: suffixText,
|
||||
),
|
||||
);
|
||||
},
|
||||
}) {
|
||||
final theme = Theme.of(context);
|
||||
return showAdaptiveDialog<String>(
|
||||
context: context,
|
||||
useRootNavigator: useRootNavigator,
|
||||
builder: (context) {
|
||||
final controller = TextEditingController(text: initialText);
|
||||
final error = ValueNotifier<String?>(null);
|
||||
return ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 512),
|
||||
child: AlertDialog.adaptive(
|
||||
title: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 256),
|
||||
child: Text(title),
|
||||
),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (message != null)
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 256),
|
||||
child: Text(message),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
AdaptiveDialogAction(
|
||||
onPressed: () => Navigator.of(context).pop(null),
|
||||
child: Text(cancelLabel ?? L10n.of(context).cancel),
|
||||
),
|
||||
AdaptiveDialogAction(
|
||||
onPressed: () {
|
||||
final input = controller.text;
|
||||
final errorText = validator?.call(input);
|
||||
if (errorText != null) {
|
||||
error.value = errorText;
|
||||
return;
|
||||
const SizedBox(height: 16),
|
||||
ValueListenableBuilder<String?>(
|
||||
valueListenable: error,
|
||||
builder: (context, error, _) {
|
||||
switch (theme.platform) {
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.fuchsia:
|
||||
case TargetPlatform.linux:
|
||||
case TargetPlatform.windows:
|
||||
return TextField(
|
||||
controller: controller,
|
||||
obscureText: obscureText,
|
||||
minLines: minLines,
|
||||
maxLines: maxLines,
|
||||
maxLength: maxLength,
|
||||
keyboardType: keyboardType,
|
||||
autocorrect: autocorrect,
|
||||
decoration: InputDecoration(
|
||||
errorText: error,
|
||||
hintText: hintText,
|
||||
labelText: labelText,
|
||||
prefixText: prefixText,
|
||||
suffixText: suffixText,
|
||||
),
|
||||
);
|
||||
case TargetPlatform.iOS:
|
||||
case TargetPlatform.macOS:
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
CupertinoTextField(
|
||||
controller: controller,
|
||||
obscureText: obscureText,
|
||||
minLines: minLines,
|
||||
maxLines: maxLines,
|
||||
maxLength: maxLength,
|
||||
keyboardType: keyboardType,
|
||||
autocorrect: autocorrect,
|
||||
prefix:
|
||||
prefixText != null ? Text(prefixText) : null,
|
||||
suffix:
|
||||
suffixText != null ? Text(suffixText) : null,
|
||||
placeholder: labelText ?? hintText,
|
||||
),
|
||||
if (error != null)
|
||||
Text(
|
||||
error,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: theme.colorScheme.error,
|
||||
),
|
||||
textAlign: TextAlign.left,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
Navigator.of(context).pop<String>(input);
|
||||
},
|
||||
autofocus: true,
|
||||
child: Text(
|
||||
okLabel ?? L10n.of(context).ok,
|
||||
style: isDestructive
|
||||
? TextStyle(color: Theme.of(context).colorScheme.error)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
actions: [
|
||||
AdaptiveDialogAction(
|
||||
onPressed: () => Navigator.of(context).pop(null),
|
||||
child: Text(cancelLabel ?? L10n.of(context).cancel),
|
||||
),
|
||||
AdaptiveDialogAction(
|
||||
onPressed: () {
|
||||
final input = controller.text;
|
||||
final errorText = validator?.call(input);
|
||||
if (errorText != null) {
|
||||
error.value = errorText;
|
||||
return;
|
||||
}
|
||||
Navigator.of(context).pop<String>(input);
|
||||
},
|
||||
autofocus: true,
|
||||
child: Text(
|
||||
okLabel ?? L10n.of(context).ok,
|
||||
style: isDestructive
|
||||
? TextStyle(color: Theme.of(context).colorScheme.error)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue