相对较新的Flutter .我正在try 实现一个登录表单,但遇到了一个问题,即在 Select Textfield之外的任何内容时,iOS设备在TextField中的输入会消失.
无论是在真实的iOS设备上还是在模拟器上,都可以观察到这种行为.如果不在模拟器上使用iOS键盘(使用我的MacBook键盘),则不会出现此问题.这个问题也不会发生在Android模拟器、Chrome Web应用程序或MacOS应用程序上.
这是我的小工具
class LoginForm extends ConsumerWidget {
const LoginForm({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
var emailController = TextEditingController();
var passwordController = TextEditingController();
ref.listen<AsyncValue>(loginControllerProvider, (previous, next) {
if (next is AsyncError) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(next.error.toString()), backgroundColor: Colors.red));
}
});
return Scaffold(
body: CustomScrollView(slivers: [
SliverAppBar(
pinned: true,
snap: false,
floating: false,
expandedHeight: (MediaQuery.of(context).size.height -
MediaQuery.of(context).padding.bottom) /
2,
flexibleSpace: const FlexibleSpaceBar(
title: Text("foo"), background: FlutterLogo())),
SliverList(
delegate: SliverChildListDelegate([
const SizedBox(height: 12),
const Center(
child: Text(
'Welcome back',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
)),
const SizedBox(height: 12),
const Center(
child: Text(
'Login to manage your account.',
style: TextStyle(
fontSize: 14,
),
)),
const SizedBox(height: 24),
TextField(
controller: emailController,
decoration: const InputDecoration(
border: OutlineInputBorder(), labelText: 'Email'),
),
const SizedBox(height: 12),
TextField(
obscureText: true,
controller: passwordController,
decoration: const InputDecoration(
border: OutlineInputBorder(), labelText: 'Password'),
),
const SizedBox(height: 24),
ElevatedButton(
style: ElevatedButton.styleFrom(
elevation: 0, minimumSize: const Size(1024, 60)),
onPressed: () => ref
.read(loginControllerProvider.notifier)
.login(emailController.text, passwordController.text),
child: const Text("Login", style: TextStyle(fontSize: 16))),
SizedBox(
width: 1024,
height: 60,
child: TextButton(
onPressed: () => ref
.read(loginControllerProvider.notifier)
.authRepo
.registering = true,
child: const Text(
"Don't have an account? Register",
style: TextStyle(fontSize: 14),
)),
)
]))
]));
}
}
以下是一些可能相关的Flutter 细节:
➜ ✗ flutter doctor -v
[✓] Flutter (Channel stable, 3.3.2, on macOS 12.6 21G115 darwin-arm, locale en-GB)
• Flutter version 3.3.2 on channel stable at /opt/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision e3c29ec00c (9 days ago), 2022-09-14 08:46:55 -0500
• Engine revision a4ff2c53d8
• Dart version 2.18.1
• DevTools version 2.15.0
...
[✓] Xcode - develop for iOS and macOS (Xcode 14.0)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14A309
• CocoaPods version 1.11.3
...
• No issues found!