Bis*_*ret 6 form-fields android-softkeyboard flutter
和我在标题中描述的差不多。应用程序启动时,我有一堆从 Firebase 填充的 TextFormFields。
用户应该能够更新这些,完成后,单击提交按钮来更新数据库。数据库代码一切正常,但是存在一些错误,其工作原理如下:
TextFormField1: "New Text Entered"
TextFormField2: "Some more text"
TextFormField3: "Another update here"
Run Code Online (Sandbox Code Playgroud)
现在我们需要关闭键盘,以便我们可以看到下面的提交按钮。一旦您单击小向下箭头关闭键盘,上述所有更改都会恢复到原始状态。
有人见过这个吗?
我在运行时预填充这些字段中的数据,您可以编辑和更新文本,并且一切正常......除非您最小化键盘。
请告诉我,Flutter 并没有做一些根本上愚蠢的事情,比如每次你要求键盘消失时从头开始重新加载下面的小部件......感觉就像是这样。
小智 3
是的。这种事经常发生在我身上。这是因为当底部插图(由于键盘)发生变化时,屏幕会重建。
TextFormField(s)a括起来Form并给它一个全局密钥。为了方便起见,我将附上一个代码。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginScreen(),
);
}
}
// Login Screen
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
static GlobalKey<FormState> _loginScreenFormKey = GlobalKey<FormState>();
}
class _LoginScreenState extends State<LoginScreen> {
String username;
String password;
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Form(
key: LoginScreen._loginScreenFormKey,
child: Column(
children: [
TextFormField(
decoration: InputDecoration(
hintText: 'Enter username',
),
onChanged: (value) {
setState(() {
username = value;
});
},
),
TextFormField(
decoration: InputDecoration(
hintText: 'Enter username',
),
onChanged: (value) {
setState(() {
password = value;
});
},
obscureText: true,
),
RaisedButton(
onPressed: () {
LoginScreen._loginScreenFormKey.currentState.save();
},
child: Text('submit'),
),
],
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3087 次 |
| 最近记录: |