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/material.dart';
|
||||||
|
|
||||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
|
|
@ -24,79 +25,115 @@ Future<String?> showTextInputDialog({
|
||||||
TextInputType? keyboardType,
|
TextInputType? keyboardType,
|
||||||
int? maxLength,
|
int? maxLength,
|
||||||
bool autocorrect = true,
|
bool autocorrect = true,
|
||||||
}) =>
|
}) {
|
||||||
showAdaptiveDialog<String>(
|
final theme = Theme.of(context);
|
||||||
context: context,
|
return showAdaptiveDialog<String>(
|
||||||
useRootNavigator: useRootNavigator,
|
context: context,
|
||||||
builder: (context) {
|
useRootNavigator: useRootNavigator,
|
||||||
final controller = TextEditingController(text: initialText);
|
builder: (context) {
|
||||||
final error = ValueNotifier<String?>(null);
|
final controller = TextEditingController(text: initialText);
|
||||||
return ConstrainedBox(
|
final error = ValueNotifier<String?>(null);
|
||||||
constraints: const BoxConstraints(maxWidth: 512),
|
return ConstrainedBox(
|
||||||
child: AlertDialog.adaptive(
|
constraints: const BoxConstraints(maxWidth: 512),
|
||||||
title: ConstrainedBox(
|
child: AlertDialog.adaptive(
|
||||||
constraints: const BoxConstraints(maxWidth: 256),
|
title: ConstrainedBox(
|
||||||
child: Text(title),
|
constraints: const BoxConstraints(maxWidth: 256),
|
||||||
),
|
child: Text(title),
|
||||||
content: Column(
|
),
|
||||||
mainAxisSize: MainAxisSize.min,
|
content: Column(
|
||||||
children: [
|
mainAxisSize: MainAxisSize.min,
|
||||||
if (message != null)
|
children: [
|
||||||
Padding(
|
if (message != null)
|
||||||
padding: const EdgeInsets.only(bottom: 16.0),
|
ConstrainedBox(
|
||||||
child: ConstrainedBox(
|
constraints: const BoxConstraints(maxWidth: 256),
|
||||||
constraints: const BoxConstraints(maxWidth: 256),
|
child: Text(message),
|
||||||
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,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
],
|
const SizedBox(height: 16),
|
||||||
),
|
ValueListenableBuilder<String?>(
|
||||||
actions: [
|
valueListenable: error,
|
||||||
AdaptiveDialogAction(
|
builder: (context, error, _) {
|
||||||
onPressed: () => Navigator.of(context).pop(null),
|
switch (theme.platform) {
|
||||||
child: Text(cancelLabel ?? L10n.of(context).cancel),
|
case TargetPlatform.android:
|
||||||
),
|
case TargetPlatform.fuchsia:
|
||||||
AdaptiveDialogAction(
|
case TargetPlatform.linux:
|
||||||
onPressed: () {
|
case TargetPlatform.windows:
|
||||||
final input = controller.text;
|
return TextField(
|
||||||
final errorText = validator?.call(input);
|
controller: controller,
|
||||||
if (errorText != null) {
|
obscureText: obscureText,
|
||||||
error.value = errorText;
|
minLines: minLines,
|
||||||
return;
|
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
Add a link
Reference in a new issue