gpt4 book ai didi

flutter - 将新项目添加到List

转载 作者:行者123 更新时间:2023-12-03 04:24:20 25 4
gpt4 key购买 nike

我有这一系列的小部件,
但我需要能够动态添加更多项目
如何使用函数向此小部件数组添加更多项?

  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),
),
),
];

这是选项卡的 Controller
  TabBar _tabBarLabel() => TabBar(
tabs: _tabScroll(),
);

最佳答案

由于您将要在代码中处理列表,因此这里有一些其他简单的列表操作可能会为您提供帮助。我在这里创建了一个dartpad文件,以便您可以使用它们,并且我标记了两个将对您的特定问题有帮助:
https://dartpad.dev/a6f78b9ad58a589e7415d9b1625950b6

// 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);

关于flutter - 将新项目添加到List <Widget>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60694219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com