Flutter TextFormField:键盘出现,瞬间消失
Flutter TextFormField: The keyboard appeared and instantly disappeared
当我单击TextFormField 时,键盘出现,然后立即消失,就像闪烁一样。不知是不是因为少了一些参数?还是其他一些问题?谢谢。
这是我的相关代码。
final _formKey = GlobalKey<FormState>();
String? _account;
Widget _showAccountInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(15.0, 10.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
obscureText: true,
autofocus: false,
style: TextStyle(fontSize: 15),
decoration: new InputDecoration(
border: InputBorder.none,
hintText: 'input',
icon: new Icon(
Icons.lock,
color: Colors.grey,
)),
onSaved: (value) => _account = value?.trim(),
),
);
}
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: new AppBar(
title: new Text("Second Screen"),
),
body: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_showAccountInput()
],
),
),
);
}
}
如果您的构建方法在 StatelessWidget 中,请尝试将其更改为 StatefulWidget
我修改了你的一些代码,键盘工作正常。也许您可以尝试一下或与您的代码进行比较,以便找到原因。这是完整的代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _formKey = GlobalKey<FormState>();
String? _account;
Widget _showAccountInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(15.0, 10.0, 0.0, 0.0),
child: TextFormField(
maxLines: 1,
obscureText: true,
autofocus: false,
style: const TextStyle(fontSize: 15),
decoration: const InputDecoration(
border: InputBorder.none,
hintText: 'input',
icon: Icon(
Icons.lock,
color: Colors.grey,
)),
onSaved: (value) => _account = value?.trim(),
),
);
}
// @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Second Screen"),
),
body: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[_showAccountInput()],
),
),
);
}
}
- 不需要使用有状态的小部件,因为小部件的状态不会改变。
- 完成该示例的最简单方法是先创建文本字段小部件,然后提取方法并使其成为可重用小部件。
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [ShowAccountInput()],
),
);
}
}
class ShowAccountInput extends StatelessWidget {
String? account;
ShowAccountInput({
this.account,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(15.0, 10.0, 0.0, 0.0),
child: TextFormField(
maxLines: 1,
obscureText: true,
decoration: const InputDecoration(
hintText: 'input',
border: InputBorder.none,
icon: Icon(
Icons.lock,
color: Colors.grey,
)),
onSaved: (value) => account = value?.trim(),
),
);
}
}
当我单击TextFormField 时,键盘出现,然后立即消失,就像闪烁一样。不知是不是因为少了一些参数?还是其他一些问题?谢谢。 这是我的相关代码。
final _formKey = GlobalKey<FormState>();
String? _account;
Widget _showAccountInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(15.0, 10.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
obscureText: true,
autofocus: false,
style: TextStyle(fontSize: 15),
decoration: new InputDecoration(
border: InputBorder.none,
hintText: 'input',
icon: new Icon(
Icons.lock,
color: Colors.grey,
)),
onSaved: (value) => _account = value?.trim(),
),
);
}
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: new AppBar(
title: new Text("Second Screen"),
),
body: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_showAccountInput()
],
),
),
);
}
}
如果您的构建方法在 StatelessWidget 中,请尝试将其更改为 StatefulWidget
我修改了你的一些代码,键盘工作正常。也许您可以尝试一下或与您的代码进行比较,以便找到原因。这是完整的代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _formKey = GlobalKey<FormState>();
String? _account;
Widget _showAccountInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(15.0, 10.0, 0.0, 0.0),
child: TextFormField(
maxLines: 1,
obscureText: true,
autofocus: false,
style: const TextStyle(fontSize: 15),
decoration: const InputDecoration(
border: InputBorder.none,
hintText: 'input',
icon: Icon(
Icons.lock,
color: Colors.grey,
)),
onSaved: (value) => _account = value?.trim(),
),
);
}
// @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Second Screen"),
),
body: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[_showAccountInput()],
),
),
);
}
}
- 不需要使用有状态的小部件,因为小部件的状态不会改变。
- 完成该示例的最简单方法是先创建文本字段小部件,然后提取方法并使其成为可重用小部件。
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [ShowAccountInput()],
),
);
}
}
class ShowAccountInput extends StatelessWidget {
String? account;
ShowAccountInput({
this.account,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(15.0, 10.0, 0.0, 0.0),
child: TextFormField(
maxLines: 1,
obscureText: true,
decoration: const InputDecoration(
hintText: 'input',
border: InputBorder.none,
icon: Icon(
Icons.lock,
color: Colors.grey,
)),
onSaved: (value) => account = value?.trim(),
),
);
}
}