Flutter 将新项目添加到列表<;Widget>;
我有一个小部件数组, 但我需要能够动态地添加更多项 如何使用函数向这个小部件数组添加更多项Flutter 将新项目添加到列表<;Widget>;,flutter,dart,Flutter,Dart,我有一个小部件数组, 但我需要能够动态地添加更多项 如何使用函数向这个小部件数组添加更多项 List<Widget> _tabScroll() => [ Tab( child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(20), border: Border.all(color: Colors.
List<Widget> _tabScroll() => [
Tab(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey[300], width: 1),
color: Colors.grey[100]
),
padding: EdgeInsets.fromLTRB(12.0, 5.0, 12.0, 5.0),
child: Text(_tab1),
),
),
Tab(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey[300], width: 1),
color: Colors.grey[100]
),
padding: EdgeInsets.fromLTRB(12.0, 5.0, 12.0, 5.0),
child: Text(_tab2),
),
),
Tab(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey[300], width: 1),
color: Colors.grey[100]
),
padding: EdgeInsets.fromLTRB(12.0, 5.0, 12.0, 5.0),
child: Text(_tab3),
),
),
];
更换你的吸气器
List<Tab> get tabs => [
Tab(...),
Tab(...),
Tab(...),
];
更换你的吸气器
List<Tab> get tabs => [
Tab(...),
Tab(...),
Tab(...),
];
由于您将要在代码中操作列表,这里有一些其他简单的列表操作可能会对您有所帮助。我在这里创建了一个dartpad文件,以便您可以使用它们,我标记了两个对您的具体问题有帮助的文件:
//创建firstList并将对象添加到firstList的末尾
//这一条将帮助你回答最初的问题
List firstList=[‘芝加哥’、‘圣地亚哥’、‘圣保罗’];
添加(“东京”);
//添加到索引号处的第一个列表。(从零开始索引)
//这一个也会帮助你回答你最初的问题,但是如果你
//想要更精确地插入元素的位置吗
第一份清单。插入(2,“墨西哥城”);
//创建secondList并将整个secondList添加到firstList的末尾
//如果你决定做决定,这个问题将有助于你回答最初的问题
//大量的变化。(首先使用您的更改生成第二个列表,并将其添加到
//您的初始列表一次完成。
List secondList=[“斯德哥尔摩”、“莫斯科”、“巴黎”];
firstList.addAll(第二个列表);
//创建第三个列表并添加到第一个列表中索引“0”处的第一个列表
//如果您需要创建,这将帮助您解决初始问题
//提前列出一个列表,并立即将其插入到特定位置。
排名第三位=[‘纽约’、‘圣何塞’、‘布宜诺斯艾利斯’];
firstList.insertAll(0,第三名);
//删除firstList中的特定对象
第一名单。删除(“圣何塞”);
//删除firstList中特定索引处的对象
firstList.removeAt(0);
//删除firstList中的最后一个对象
firstList.removeLast();
//删除firstList中5到7之间的所有内容(其中7不包括在内)
第一个列表。删除范围(5,7);
//在firstList中查找给定对象的索引
int bestCity=firstList.indexOf(“芝加哥”);
//查找满足某些条件的第一个对象的索引(在本例中,以S开头);
var firstS=firstList.indexWhere((city)=>city.startsWith(“S”);
//与前一个相同,但它不是从零开始,而是从包含的整数的索引开始
var secondS=firstList.indexWhere((city)=>city.startsWith(“S”),firstS+1);
由于您将在代码中操作列表,以下是一些可能对您有所帮助的其他简单列表操作。我在此处创建了一个省道板文件,以便您可以使用它们,我标记了两个对您的特定问题有帮助的操作:
//创建firstList并将对象添加到firstList的末尾
//这一条将帮助你回答最初的问题
List firstList=[‘芝加哥’、‘圣地亚哥’、‘圣保罗’];
添加(“东京”);
//添加到索引号处的第一个列表。(从零开始索引)
//这一个也会帮助你回答你最初的问题,但是如果你
//想要更精确地插入元素的位置吗
第一份清单。插入(2,“墨西哥城”);
//创建secondList并将整个secondList添加到firstList的末尾
//如果你决定做决定,这个问题将有助于你回答最初的问题
//批量更改。(首先使用您的更改构建第二个列表,并将其添加到
//您的初始列表一次完成。
List secondList=[“斯德哥尔摩”、“莫斯科”、“巴黎”];
firstList.addAll(第二个列表);
//创建第三个列表并添加到第一个列表中索引“0”处的第一个列表
//如果您需要创建,这将帮助您解决初始问题
//提前列出一个列表,并立即将其插入到特定位置。
排名第三位=[‘纽约’、‘圣何塞’、‘布宜诺斯艾利斯’];
firstList.insertAll(0,第三名);
//删除firstList中的特定对象
第一名单。删除(“圣何塞”);
//删除firstList中特定索引处的对象
firstList.removeAt(0);
//删除firstList中的最后一个对象
firstList.removeLast();
//删除firstList中5到7之间的所有内容(其中7不包括在内)
第一个列表。删除范围(5,7);
//在firstList中查找给定对象的索引
int bestCity=firstList.indexOf(“芝加哥”);
//查找满足某些条件的第一个对象的索引(在本例中,以S开头);
var firstS=firstList.indexWhere((city)=>city.startsWith(“S”);
//与前一个相同,但它不是从零开始,而是从包含的整数的索引开始
var secondS=firstList.indexWhere((city)=>city.startsWith(“S”),firstS+1);
它不起作用。即使我通过initstateUser CopsOnRoad添加列表,列表长度也不会改变。这是正确的处理方法。我在您的原始代码中注意到,您将其视为函数而不是变量。如果使用CopsOnRoad建议的方法,然后返回结果,则可以保留该函数你可以考虑共享你的代码,看看问题是什么。好了,我用上面的代码“代码>错误改变列表后,得到了这个错误:一个方法声明需要一个明确的参数列表。试着在参数声明中添加一个参数列表。[@Ray,你能用我提供的解决方案更新你的帖子吗,这样我就可以在本地测试它。它不起作用。即使我通过initstateUser CopsOnRoad添加它,列表长度也不会改变。这是正确的处理方法。我在你的原始代码中注意到,你把它当作一个函数而不是一个变量。如果你需要我们使用CopsPoad提出的方法,然后返回结果,您可以保留该函数并仍然操作该列表。您可以考虑共享代码以查看问题所在。好,在将代码更改为代码>错误之后,我得到了这个错误:一个方法声明需要一个明确的参数列表。参数lis
List<Tab> tabs = [
Tab(...),
Tab(...),
Tab(...),
];
tabs.add(Tab(...));
// Create firstList and add object to the end of firstList
// THIS ONE will help you with your initial question
List<String> firstList = ['Chicago', 'Santiago', 'São Paulo'];
firstList.add('Tokyo');
// Adds to firstList at index number.(indexed from zero)
// THIS ONE will also help you with your initial question, but if you
// want to be more precise with where you insert your element
firstList.insert(2, 'Mexico City');
// Create secondList and add entire secondList to end of firstList
// THIS ONE will help with your initial question if you decide to make
// changes in bulk. (build secondList first with your changes, and add it to
// your initial list all at once.
List<String> secondList = ['Stockholm', 'Moscow', 'Paris'];
firstList.addAll(secondList);
// Create thirdList and add to firstList at index "0" in firstList
// THIS ONE will help you with your initial question if you need to create
// a list ahead of time and insert it at a specific place all at once.
List<String> thirdList = ['New York', 'San José', 'Buenos Aires'];
firstList.insertAll(0,thirdList);
// Removes specific object in firstList
firstList.remove("San José");
// Removes object at specific index in firstList
firstList.removeAt(0);
// Removes last object in firstList
firstList.removeLast();
// Removes everything between 5 and 7 (where 7 is not inclusive) in firstList
firstList.removeRange(5,7);
// Find the index of a given object in firstList
int bestCity = firstList.indexOf("Chicago");
// Finds index of first object that meets some criteria (in this case, starts with S);
var firstS = firstList.indexWhere((city)=>city.startsWith("S"));
// Same as the previous, but instead of starting at zero, it starts with the index of the included integer
var secondS = firstList.indexWhere((city)=>city.startsWith("S"), firstS + 1);