使用ListView和Listile的TabView的动态子级

时间:2020-01-21 02:08:25

标签: flutter dart

Dynamic children for TabView in flutter

它确实运行良好,但是在基于类别创建了tabbarview之后,我正在创建一个包含内容列表项的listview,它确实按预期进行了,唯一的问题是每个列表项都作为我需要的ontap创建,我只能从创建的最后一个中获取水龙头。.我如何解决这个问题?

 Widget build(BuildContext context) {
_poiCatList = catPoi.currentCatPoiList.where((poi) => poi.IsActive == true && poi.IsFeatured == false).toList(); // filtra cat de pois
return DefaultTabController(
    length: _poiCatList.length,
    child: Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        bottom: TabBar(
          isScrollable: true,
          tabs:
          List<Widget>.generate(_poiCatList.length, (int index){
              var _selectedIcon = poiIconSet.currentPoiIconSetList.firstWhere((icon) => icon.Id == _poiCatList[index].CGBO_POIICONSET_ID);  // escolhe icon correto
              return new Tab(icon: Icon(poiIconSet.iconSet[_selectedIcon.Name.toLowerCase()]), text: _poiCatList[index].Name);
          }),
        ),
        title: Text("City Guide - Point's of Interest"),
      ),
      body: TabBarView(
          children:
            List<Widget>.generate(_poiCatList.length, (int tabIndex){
              _poiList = poi.currentPoiList.where((poi) => poi.IsActive == true && poi.IsFeatured == false && poi.CG_CATPOI_ID == _poiCatList[tabIndex].Id).toList(); // filtra pois pela cat
              return new ListView(
                  children:
                    List<Widget>.generate(_poiList.length, (int tileIndex){
                      return new ListTile(
                          title: new Text(_poiList[tileIndex].Title, style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
                          subtitle: new Text(_poiList[tileIndex].Address),
                          leading: new Icon(
                            Icons.place,
                            color: Colors.blue[500],
                          ),
                          trailing: Icon(Icons.keyboard_arrow_right),
                          onTap: () {
                            setState(() {
                              _currentSelectPoi = tileIndex;
                            });
                            _scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text("You clicked item number $_currentSelectPoi ")));

                          },
                      );
                    }
                   )
              );
            }),
      ),
    ),
  );

} }

0 个答案:

没有答案