【问题标题】:Flutter: Add a String Text to an ListFlutter:将字符串文本添加到列表中
【发布时间】:2021-06-09 10:10:54
【问题描述】:

在我的项目中,我想将文本字段中的文本输入添加到列表中以构建 Listview.builder。 问题是我不知道如何将字符串添加到列表中(我想像在待办事项应用程序中一样使用字符串来制作服务日期)。 (例如) time.add(time1) 不起作用,我希望有人可以帮助我。 有没有其他方法可以将 Inputtext 传输到列表中,我对所有内容都开放

首页

class Homescreen extends StatefulWidget {
String time1;
String who1;
String where1;
String when1;
Homescreen({this.time1, this.who1, this.where1, this.when1});

@override
_HomescreenState createState() => _HomescreenState();
}




TextEditingController myControllertime = TextEditingController();
TextEditingController myControllerwho = TextEditingController();
TextEditingController myControllerwhen = TextEditingController();
TextEditingController myControllerwhere = TextEditingController();

class _HomescreenState extends State<Homescreen> {
List<String> time = ["8:00",];
List<String> who = ["Eric", ];
List<String> when = ["Friday 21.4.21",];
List<String> where = ["At McDonalds", ];

 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.orange,
                                         Colors.purple,
                                       ],
                                     ),
                                   ),
                                   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),
                                           ),

第二页

TextButton(
          child: Icon(
            Icons.check_circle_outline_rounded,
            color: Colors.green,
            size: 120,
          ),
          onPressed: () {
            Navigator.pop(context, MaterialPageRoute(builder: (builder) {
              return Homescreen(
              time1: myControllertime.text,
              who1: myControllerwho.text,
              when1: myControllerwhen.text,
              where1: myControllerwhere.text
              ,
              );
            }));
          })
      ],
    ));




child: Column(children: <Widget>[
      SizedBox(
        height: 10,
      ),
      Padding(
        padding: EdgeInsets.all(25),
        child: Column(
          children: <Widget>[
            TextField(
                controller: myControllertime,
                decoration: InputDecoration(hintText: " Time ")),
            SizedBox(
              height: 10,
            ),
            TextField(
              controller: myControllerwho,
              decoration: InputDecoration(hintText: " Who "),
            ),
            SizedBox(
              height: 10,
            ),
            TextField(
              controller: myControllerwhen,
              decoration: InputDecoration(hintText: " Date "),
            ),
            SizedBox(
              height: 10,
            ),
            TextField(
              controller: myControllerwhere,
              decoration: InputDecoration(hintText: " Where "),
            ),
            

【问题讨论】:

  • //void main() { String m = "map";列表 mx = ["map1","2","3"]; mx.add(m);打印(MX); } // 这是一个将字符串添加到列表的坏例子,但为什么我要评论这个呢?这样可以完成工作,您也可以将其用于您的工作,但这不是正确的方法。

标签: list flutter textfield


【解决方案1】:

代码中有太多错误。让我们帮助您。

首先,time.add(time1) 不起作用,因为您每次都在添加后创建新的主屏幕,并且列表时间会一次又一次地重新初始化。因此在这里添加一个值是行不通的。

要保存数据,您必须将数据实际放在另一个具有静态引用的类中,或者可以使用 sharedprefs/anything 将它们持久化,但情况不同。

例如,您可以创建这样的类

class TODOData{
 static List<String> time = ["8:00",];
 static List<String> who = ["Eric", ];
 static List<String> when = ["Friday 21.4.21",];
 static List<String> where = ["At McDonalds", ];
}

现在,每当您想保存新字段(例如 time1)时,就您而言,只需使用它TODOData.time.add(time1); 您无需将其传递给您的主屏幕。 您可以使用TODOData.time / TODOData.who etc 访问该数据。

您现在甚至可以从 HomeScreen Widget 中删除所有这些字段 time1 等。您可以在上面提到的列表中的 SecondScreen 的 onPressed 方法中添加所有这些值。并导航到 HomeScreen,它将包含新数据。

这将暂时解决您的数据添加问题。

您可以从_HomescreenState 中删除所有这些列表并按上述方式使用。

现在是理想的方式。

您应该始终创建一个模型类来简化该数据。它使大型项目的内容更具可读性、可访问性和可扩展性。

例如,您实际上可以创建一个像这样的模型类,而不是创建 4 个不同的数据列表。

class TODOModel{(
 String time,
 String who,
 String when,
 String where
)}

然后在同一个 TODOData 类中创建它的列表。

【讨论】:

  • 感谢您的帮助。我将 ToDoData 和 Todolist 放在 main.dart 文件中,并在我提示的第二个屏幕上:按下 ... ToDoData.time.add(myControllertime.text)。但在我的主屏幕上,我不知道如何使用列表。例如Text(time[index]),程序找不到列表时间等。你知道我做错了什么
  • class TimescheduleData { static List&lt;String&gt; time = [ ]; static List&lt;String&gt; who = [ "Eric", ]; static List&lt;String&gt; when = [ "Friday 21.4.21", ]; static List&lt;String&gt; where = [ "At McDonalds", ]; } class Timeschedulemodel { String time; String who; String when; String where; }
  • onPressed: () { TimescheduleData.time.add(myControllertime.text); TimescheduleData.who.add(myControllerwho.text); TimescheduleData.when.add(myControllerwhen.text); TimescheduleData.where.add(myControllerwhere.text); Navigator.pop(
  • 文本(时间[索引],)#时间不起作用
  • @EricPhilippi 您必须改用 Text(TimescheduleData.time[Index],)
猜你喜欢
  • 2013-09-24
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 2011-09-10
相关资源
最近更新 更多