Flutter:将字符串文本添加到列表 [英] Flutter: Add a String Text to an List

查看:68
本文介绍了Flutter:将字符串文本添加到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我想将Textfield中的textinput添加到List中以构建Listview.builder.问题是我不知道如何将字符串添加到列表中(我想像在todo应用程序中那样使用字符串来计算服务日期).(例如)time.add(time1)无法正常工作,希望有人可以帮助我.有没有其他方法可以将Inputtext传输到列表,我对所有内容都开放

in my project I want to add the textinput from a Textfield into a List to build a Listview.builder. The Problem is that I dont know how to add the String to the List (I want to use the string like in a todo app to make serval dates). (for example) time.add(time1) isnt working and I hope someone can help me. Is there a completly other way to transport the Inputtext to the list, Im open for everything

首页

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 "),
            ),
            

推荐答案

代码中有太多错误的tbh.让我们来帮助您.

There is too much wrong tbh in the code. Let us help you.

首先,time.add(time1)无法正常工作,因为您每次添加后都会创建新的HomeScreen,并且一次又一次地重新初始化列表时间.因此,在这里增加价值是行不通的.

Firstly, time.add(time1) is not working because you are everytime creating New HomeScreen after addition and List time is reinitialised again and again. thus adding a value wont work here.

要保存数据,您必须将这些数据实际放置在具有静态引用的另一个类中的某个位置,或者可以使用sharedprefs/任何方法(除了那种不同情况)将其持久化.

To save data you have to actually put that data somewhere in another class with static reference or may be persist them using sharedprefs/anything but that different case.

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

for example you can create a class like this

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等访问该数据.

Now whenever you want to save new field for example time1, in your case, just use it TODOData.time.add(time1); You don't need to pass it your home screen. And you can access that data using TODOData.time / TODOData.who etc.

您现在甚至可以从HomeScreen窗口小部件中删除所有这些字段time1等.您可以如上所述在列表的SecondScreen中的onPressed方法中添加所有这些值.并导航到HomeScreen,它将拥有该新数据.

You can now even remove all those fields time1, etc from HomeScreen Widget. You can add all those values in onPressed method in SecondScreen in the list as mentioned above. and navigate to HomeScreen, it will have that new data.

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

This will solve your problem temporarily for data addition.

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

You can remove all those lists from _HomescreenState and use as mentioned above.

现在是理想的方式.

您应该始终创建一个模型类来简单地存储该数据.在大型项目中,它使事情更具可读性,可访问性和可扩展性.

You should always create a model class to simplyfy that data. It makes things more readable, accesable and scalable on large projects.

例如,除了创建4个不同的数据列表之外,您实际上可以创建一个这样的模型类.

For example instead of creating 4 different list of data you can actually create a model class like this.

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

然后在相同的TODOData类中创建它的列表.

And then create a list of it in the same TODOData class.

这篇关于Flutter:将字符串文本添加到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆