将新项目添加到List <Widget>

时间:2020-03-15 14:58:07

标签: flutter dart

我有这个小部件数组, 但我需要能够动态添加更多项目 如何通过函数向此小部件数组添加更多项?

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

这是标签的控制器

  TabBar _tabBarLabel() => TabBar(
    tabs: _tabScroll(),
  );

2 个答案:

答案 0 :(得分:1)

替换您的吸气剂

List<Tab> get tabs => [
  Tab(...),
  Tab(...),
  Tab(...),
];

带有实例变量

List<Tab> tabs = [
  Tab(...),
  Tab(...),
  Tab(...),
];

并使用

tabs.add(Tab(...));

答案 1 :(得分:1)

由于您将要处理代码中的列表,因此这里有一些其他简单的列表操作可能会为您提供帮助。我在这里创建了一个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);