Flutter:如何将文本字段输入到列表中以构建 ListView.builder

Flutter: How can i put Textfield input into a list to build a ListView.builder

我几天前就开始尝试构建一个 listviewbuilder。为此,我需要来自另一个屏幕的文本字段输入。我看了很多教程和问题,但它没有 work.Im 试图将来自多个文本字段的输入放入多个列表以构建 Listview 生成器。如果我可以在按下 flatbutton 时保存所有文本字段输入,那将是最好的。我希望有人能帮助我。

首页

List<String> time = ["8:00"];List<String> 
List<String> where = ["Am See"];
List<String> who = ["Eric"];
List<String> when = ["Donnerstag 21.4.21"];






body: SingleChildScrollView(
            physics: ScrollPhysics(),
            child: Column(children: [
              Upperscreen(size: size),
              ListView.builder(
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: where.length,
                  itemBuilder: (BuildContext context, int Index) {
                    return Column(children: [
                      SizedBox(
                        height: 40,
                      ),
                      Container(
                          child: GestureDetector(
                              onTap: () {
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => Meet1()));
                              },
                              child: Container(
                                  width: size.width * 0.9,
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(
                                        Radius.circular(70)),
                                    gradient: LinearGradient(
                                      begin: Alignment.topRight,
                                      end: Alignment.bottomRight,
                                      colors: [
                                        Colors.green,
                                        Colors.orange,
                                      ],
                                    ),
                                  ),
                                  child: Column(children: <Widget>[
                                    SizedBox(
                                      height: 10,
                                    ),
                                    Padding(
                                      padding: EdgeInsets.all(20),
                                      child: Column(
                                        children: <Widget>[
                                          Text(
                                            time[Index],
                                            style: TextStyle(
                                                color: Colors.white,
                                                fontSize: 40,
                                                fontWeight:
                                                    FontWeight.bold),
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Text(
                                            who[Index],
                                            style: TextStyle(
                                                color: Colors.white,
                                                fontSize: 20,
                                                fontWeight:
                                                    FontWeight.bold),
                                          ),
                                          Text(
                                            when[Index],
                                            style: TextStyle(
                                                color: Colors.white,
                                                fontSize: 20,
                                                fontWeight:
                                                    FontWeight.bold),
                                          ),
                                          Text(
                                            where[Index],
                                            style: TextStyle(
                                                color: Colors.white,
                                                fontSize: 20,
                                                fontWeight:
                                                    FontWeight.bold),

第二页

child: Column(children: <Widget>[
      SizedBox(
        height: 10,
      ),
      Padding(
        padding: EdgeInsets.all(20),
        child: Column(
          children: <Widget>[
            TextField(decoration: InputDecoration(hintText: " Time ")),
            SizedBox(
              height: 10,
            ),
            TextField(
              decoration: InputDecoration(hintText: " Who "),
            ),
            SizedBox(
              height: 10,
            ),
            TextField(
              decoration: InputDecoration(hintText: " Date "),
            ),
            SizedBox(
              height: 10,
            ),
            TextField(
              decoration: InputDecoration(hintText: " Where "),
            ),
            SizedBox(height: 10)
          ],
        ),
      ),
    ]));

这里Flatbutton要全部添加。

return FlatButton(
  child: Icon(
    Icons.check_circle_outline_rounded,
    color: Colors.green,
    size: 120,
  ),
  onPressed: () {
    Navigator.of(context).popUntil((route) => route.isFirst);
  },

使用一个TextEditingController(),就像这样-

TextEditingController() myController = TextEditingController();

然后将此控制器分配给TextField()中的控制器属性 -

TextField(
  controller: myController,
),

然后使用myController.textTextField()中检索文本,并将其作为String参数传递给其他页面-

例子-

class Screen1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //....
      body: FlatButton(
        child: Icon(
        Icons.check_circle_outline_rounded,
        color: Colors.green,
        size: 120,
        ),
        onPressed: () {
            Navigator.push(context, MaterialPageRoute(builder: (builder) {
                 return Screen2(text: myController.text);
            }));
         },
      //....
     ),
    );
  }
}

第二页 -

class Screen2 extends StatelessWidget {
  
  String text;
  Screen2({this.text});
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //....
      body: Text(text),
    );
  }
}

Go to this link to see another example

现在,这里我只使用了 1 个参数“文本”。您可以根据需要使用多个参数,例如 - "text1"、"text2"、"text3" 等,并为此使用尽可能多的 TextEditingController()

*****另请注意,使用 FlatButton() 已贬值,您可以使用 TextButton() 代替