【问题标题】:How to create Dynamic Tabs, equals to the length of list如何创建动态标签,等于列表的长度
【发布时间】:2020-09-03 09:20:25
【问题描述】:

如何创建等于列表长度的选项卡,并且它们的名称应该等于列表中项目的名称,并且我正在从 firebase 实时数据库中获取列表。

这是我的代码:

class _ItemDetailsDemoState extends State<ItemDetailsDemo>  with SingleTickerProviderStateMixin {


  @override
  void initState() {
    getSubCategories();
  
    super.initState();
  }
  

  List<SubCategoryLoader> subCategoryLoaderList = List();

  Future<void> getSubCategories() async {
    await FirebaseDatabase.instance.reference().child("SubCategoryNames").once()
        .then((DataSnapshot snapshot) {
      var key = snapshot.value.keys;
  ;

      for (var i in key) {
        SubCategoryLoader subCategoryLoader = new SubCategoryLoader(
            snapshot.value[i]['Name']
        );
        subCategoryLoaderList.add(subCategoryLoader);
      }
      for (int j = 0; j < subCategoryLoaderList.length; j++) {
        print(subCategoryLoaderList[j].Name);
      }
      if (mounted) {
        setState(() {
        
          print(subCategoryLoaderList.length);
        });
      }
    });
  }


  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: FirebaseDatabase.instance.reference().child("SubCategoryNames").once(),
      builder: (context,snapshot){
        if(snapshot.hasData){
          if(snapshot.data!=null)
            {
              return DefaultTabController(
                  length: subCategoryLoaderList.length, // I made this dynamic but this is throwing an error "controller's length property does not match with number of tabs, this is because my Tab is static which is 2 how can I make it dynamic. 
                  child: Scaffold(
                    appBar: AppBar(
                      bottom: TabBar(
                        tabs: [
                          Tab(icon: Icon(Icons.looks_one), text: "List1"),  //How can i make this dynamic and text:"List1" must be the name of list items
                          Tab(icon: Icon(Icons.looks_two), text: "List2"),   
                        ],
                      ),

                    ),
                    body: TabBarView(
                      children: [
                        _buildList(key: "key1", string: "a"),
                        _buildList(key: "key2", string: "List2: "),
                      ],
                    ),
                  ));
            }else{
           return Loader();
          }
        }else{
          return Loader();
        }
      },

    );

  }

  Widget _buildList({String key, String string}) {
    return ListView.builder(
      shrinkWrap: true,
      scrollDirection: Axis.vertical,
      itemCount: subCategoryLoaderList.length,

      itemBuilder: (context, index1){
        return Container(
          child: Text(subCategoryLoaderList[index1].Name+string),
        );
      },
    );
  }
}

我还希望 TabBarView 也是动态的,以便相应地呈现项目。我需要获取属于该子类别的所有数据。

【问题讨论】:

    标签: flutter firebase-realtime-database tabs tabbarcontroller


    【解决方案1】:

    tabs 和 TabBarView 的 children 的数量必须与 DefaultTabController 的 length 相同。一种方法是使用map 函数将SubCategoryLoader 转换为Tabs 或页面:

      @override
      Widget build(BuildContext context) {
        return FutureBuilder(
          future: FirebaseDatabase.instance
              .reference()
              .child("SubCategoryNames")
              .once(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              if (snapshot.data != null) {
                return DefaultTabController(
                    length: subCategoryLoaderList.length,
                    child: Scaffold(
                      appBar: AppBar(
                        bottom: TabBar(
                          tabs: subCategoryLoaderList
                              .map((subCatagory) => Tab(text: subCatagory.Name))
                              .toList(),
                        ),
                      ),
                      body: TabBarView(
                        children: subCategoryLoaderList.map((sub){
                          return _buildList(key: "key${sub.id}", string: "some string");
                        }).toList(),
                      ),
                    ));
              } else {
                return Loader();
              }
            } else {
              return Loader();
            }
          },
        );
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 2023-03-07
      相关资源
      最近更新 更多