首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >颤动- TextEditingController不工作

颤动- TextEditingController不工作
EN

Stack Overflow用户
提问于 2021-10-12 14:31:19
回答 1查看 94关注 0票数 1

我尝试从4个TextFields (商品编码、商品名称、商品单位、商品数量)中获取数据,并将它们传递到我的数据库中。但它根本不起作用。我是编程新手,Flutter是我的第二语言。

下面是我的代码:

代码语言:javascript
运行
复制
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),
                    ))
              ],
            ),
          ),
        ));
  }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-12 14:34:31

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

我更新了你的代码:

代码语言:javascript
运行
复制
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),
                    ))
              ],
            ),
          ),
        ));
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69542372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档