gpt4 book ai didi

android - 关闭键盘时 TextFormField 数据消失

转载 作者:IT王子 更新时间:2023-10-29 07:12:15 24 4
gpt4 key购买 nike

我正在构建一个 flutter 应用程序,那里有一个 Register 表单。我正在使用 TextFormFields,下面是我的代码。

import 'package:flutter/material.dart';

class RegisterPage extends StatelessWidget {
final borderColorGreen = const Color(0xff339966);

TextEditingController nameTxtController =TextEditingController();
TextEditingController emailTxtController =TextEditingController();
TextEditingController mobileTxtController =TextEditingController();
TextEditingController passwordTxtController =TextEditingController();
TextEditingController confirmPasswordTxtController =TextEditingController();

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/background_login_5.jpg"),
fit: BoxFit.cover,
),
),
child: ListView(
children: <Widget>[
_buildTitleSection(),
_buildInputFields("Name",nameTxtController),
_buildInputFields("Email",emailTxtController),
_buildInputFields("Mobile",mobileTxtController),
_buildInputFields("Password",passwordTxtController),
_buildInputFields("Confirm Password",confirmPasswordTxtController),
_buildRegisterButton(),
],
),
),
);
}

Widget _buildTitleSection() {
return Container(
margin: EdgeInsets.only(top: 40),
padding: EdgeInsets.all(32),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"something.xyz",
style: TextStyle(
color: borderColorGreen,
fontWeight: FontWeight.w900,
fontSize: 25,
),
),
],
));
}

Widget _buildInputFields(String label, TextEditingController textController) {
return Container(
margin: EdgeInsets.only(left: 20, bottom: 20),
child: Column(
children: <Widget>[

Container(
padding: EdgeInsets.only(right: 20),
child: Row(
children: <Widget>[
Flexible(
child: TextFormField(
controller: textController,
style: new TextStyle(color: Colors.white),
decoration: InputDecoration(
labelText: label,
fillColor: Colors.white,
labelStyle: TextStyle(
color: Colors.white, fontWeight: FontWeight.w600),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.white30, width: 2.0),
borderRadius: BorderRadius.circular(25.0),
),
),
),
),
],
),
),
],
));
}





Widget _buildRegisterButton() {
return Container(
margin: EdgeInsets.only(top: 10, right: 20, left: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: SizedBox(
width: double.infinity, // match_parent
child: RaisedButton(
child: Text(
"Create Account",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.normal),
),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
color: borderColorGreen,
onPressed: () {},
)))
],
),
);
}


}

当我点击 TextField 时,键盘打开,我可以输入文本。但是,如果我按手机的后退按钮 关闭键盘,那么我从所有字段输入的文本。我指的是 Android 手机中可用的硬件后退按钮。

表单是可滚动的,在向 TextField 输入文本后,如果我单击另一个 TextField,文本将保留在那里。如果我按下后退按钮关闭键盘,表单中的所有文本都会消失。

我该如何解决这个问题?

最佳答案

请将您的小部件更改为 StatefulWidget,这样不会导致 Controller 重置。

希望对您有所帮助!

关于android - 关闭键盘时 TextFormField 数据消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55195779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com