在使用texttedingcontroller设置初始值后,对TextField中的值进行编辑 &g



我正在将一些用户信息从第一个屏幕传递到第二个屏幕,在第二个屏幕上,我试图在TextField内编辑该信息。第二个屏幕的代码如下:

class EditProfile extends StatefulWidget {
String profName;
String profOcc;
EditProfile({Key? key, required this.profName, required this.profOcc,}): super(key: key);
@override
_EditProfileState createState() => _EditProfileState();
}
class _EditProfileState extends State<EditProfile> {
@override
Widget build(BuildContext context) {
var nameController = TextEditingController()..text = widget.profName;
var occController = TextEditingController()..text = widget.profOcc;
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
elevation: 1,
leading: IconButton(
icon: Icon(
Icons.arrow_back,
color: Color(0xFF6f6bdb),
),
onPressed: (){
Navigator.pop(context);
},
),
),
body: Container(
padding: EdgeInsets.fromLTRB(30.0, 30.0, 30.0, 0.0),
child: GestureDetector(
onTap: () {FocusScope.of(context).unfocus();},
child: ListView(
children: [
Text(
"Edit Profile",
),
SizedBox(height: 20.0),
createInputField("NAME", nameController),
createInputField("OCCUPATION", occController),
SizedBox(height: 25.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () async {
//// Update the values when pressed
},
style: ElevatedButton.styleFrom(
primary: Color(0xFF6f6bdb), // background
padding: EdgeInsets.symmetric(horizontal: 50),
),
child: Text(
"UPDATE",
)
),
],
)
],
),
),
),
);
}
TextField createInputField(String labelText, TextEditingController controller) {
return TextField(
controller: controller,
decoration: InputDecoration(
isDense: true,
labelText: "$labelText:",
floatingLabelBehavior: FloatingLabelBehavior.always,
hintStyle: TextStyle(
fontSize: 16, fontWeight: FontWeight.w700,
color: Colors.black,
)
),
);
}

我的问题是,因为我在小部件内初始化文本编辑控制器,当我编辑TextField中的值,然后对字段进行散焦时,TextField中的值会恢复到初始值。我的问题是,是否有一种方法来初始化窗口外的文本编辑控制器,并且仍然使用我从第一个屏幕获得的值来设置初始值?widget.profName不能在Widget之外工作。如有任何帮助,我将不胜感激。

您可以在构建方法外部创建TextEditingController,通过在init字段中设置它的值。一样,

class SecondPage extends StatefulWidget {
final String myString;
const SecondPage({Key? key, required this.myString}) : super(key: key);
@override
State<SecondPage> createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {

late final TextEditingController controller;
@override
void initState() {
super.initState();
controller = TextEditingController(text: widget.myString);
}

@override
void dispose() {
controller.dispose();
super.dispose();
}

另一种方法是在init字段外赋值如果你将late赋给变量

class SecondPage extends StatefulWidget {
final String myString;
const SecondPage({Key? key, required this.myString}) : super(key: key);
@override
State<SecondPage> createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {

late final TextEditingController controller = TextEditingController(text: widget.myString);
@override
void dispose() {
controller.dispose();
super.dispose();
}

记住,一旦你停止使用它处置textController。链接:https://api.flutter.dev/flutter/widgets/TextEditingController-class.html

相关内容

  • 没有找到相关文章

最新更新