Flutter语言 - TextEditingController does not work



我试图从4个TextFields(项目代码,项目名称,项目单位,项目数量)获取数据,通过API将它们传递给我的数据库。但它根本不起作用。我是编程新手,Flutter是我的第二语言。

下面是我的代码:
class _AddItemPageState extends State<AddItemPage> {
final textController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: const Text("Add Item",
style: TextStyle(
color: Colors.white,
fontFamily: "Quicksand",
fontWeight: FontWeight.bold)),
centerTitle: true,
backgroundColor: Colors.green,
),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextField(
controller: textController,
style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
),
const SizedBox(
height: 8,
),
TextField(
controller: textController,
style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
),
const SizedBox(
height: 8,
),
TextField(
controller: textController,
style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
),
const SizedBox(
height: 8,
),
TextField(
controller: textController,
style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
),
const SizedBox(
height: 8,
),
ElevatedButton.icon(
onPressed: () async {
final Map<String, String> data = ({
'code': textController.text,
'name': textController.text,
'unit': textController.text,
'quantity': textController.text,
});
// API 
},
icon: Icon(Icons.add),
style: ButtonStyle(
padding: MaterialStateProperty.all(
EdgeInsets.fromLTRB(0, 16, 0, 16)),
backgroundColor:
MaterialStateProperty.all<Color>(Colors.green)),
label: Text(
"Add Item",
style: TextStyle(fontFamily: "Quicksand", fontSize: 16),
))
],
),
),
));
}
}

我已经找了一天,但仍然没有找到任何东西。任何帮助都将不胜感激。谢谢你! !

TextEditingController只能用于一个TextField。因此,您应该使用4个TextEditingController,而不是为4个文本字段使用textController

我更新了你的代码:

class _AddItemPageState extends State<AddItemPage> {
final firstController = TextEditingController();
final secondController = TextEditingController();
final thirdController = TextEditingController();
final fourthController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: const Text("Add Item",
style: TextStyle(
color: Colors.white,
fontFamily: "Quicksand",
fontWeight: FontWeight.bold)),
centerTitle: true,
backgroundColor: Colors.green,
),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextField(
controller: firstController,
style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
),
const SizedBox(
height: 8,
),
TextField(
controller: secondController,
style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
),
const SizedBox(
height: 8,
),
TextField(
controller: thirdController,
style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
),
const SizedBox(
height: 8,
),
TextField(
controller: fourthController,
style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
),
const SizedBox(
height: 8,
),
ElevatedButton.icon(
onPressed: () async {
final Map<String, String> data = ({
'code': textController.text,
'name': textController.text,
'unit': textController.text,
'quantity': textController.text,
});
// API 
},
icon: Icon(Icons.add),
style: ButtonStyle(
padding: MaterialStateProperty.all(
EdgeInsets.fromLTRB(0, 16, 0, 16)),
backgroundColor:
MaterialStateProperty.all<Color>(Colors.green)),
label: Text(
"Add Item",
style: TextStyle(fontFamily: "Quicksand", fontSize: 16),
))
],
),
),
));
}
}

相关内容

  • 没有找到相关文章

最新更新