how to remove null item count in GridView? [Group

2019-08-27 01:52发布

问题:

Need to create group wise grid view. I am almost done. My json looks like this,

[ { "d": "Gym", "p": 0 }, { "d": "Lifts", "p": 0 }, { "d": "Office", "p": 0 }, { "d": "Guest Rooms", "p": 1.1 }, { "d": "Front of House", "p": 1.2 }, { "d": "Restaurants", "p": 1.3 }, { "d": "Function Rooms", "p": 1.4 }, { "d": "Exterior", "p": 1.5 }, { "d": "Recreation", "p": 1.6 }, { "d": "Laundry", "p": 3.1 }, { "d": "A/V Equipment", "p": 3.2 }, { "d": "F&B Equipment", "p": 3.3 }, { "d": "Other Equipment", "p": 4.1 }, { "d": "Back of House", "p": 4.2 }, { "d": "Central Plant", "p": 4.3 }, { "d": "Headings", "p": 5 }, { "d": "Plumbing", "p": 5.1 }, { "d": "Essential Services", "p": 5.2 }, { "d": "Workshop Equipment", "p": 5.3 }, { "d": "HSK Equipment", "p": 5.4 }, { "d": "Electrical Equipment", "p": 5.5 }, { "d": "Business Centre", "p": 100 } ]

 Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
    List<Data> values = snapshot.data;
    final listCount = getItemCounter(values);
//    final listCount =
//        values.map<double>((m) => m.p).reduce((a, b) => max(a, b)).floor();

    return CustomScrollView(
      slivers: <Widget>[
        SliverList(
          delegate: SliverChildBuilderDelegate((context, i) {
            final items = values
                .where((m) => i + 1 <= m.p && m.p < i + 2)
                .toList(growable: false)
                  ..sort((a, b) => a.p.compareTo(b.p));
            return GridView.builder(
              itemCount: items.length,
              shrinkWrap: true,
              primary: false,
             gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4),
              itemBuilder: (context, i) {
                final item = items[i];
                return _builderItem(item);
              },
            );
          }, childCount: listCount),
        )
      ],
    );
  }
 getItemCounter(List<Data> values){
    List iPoint = [];
    List distictI = [];
    for(int i = 0; i < values.length; i++){
      iPoint.add(values[i].p.toInt());
    }
    //remove duplicate
    distictI = iPoint.toSet().toList();
    return distictI.length;
  }

this is my output look like,

I found a problem why it is happening, in my json P passing 1.1,1.2,1.3 after that passing 3.1,3.2..like wise. space because of missing number 02

回答1:

Here you go:

 Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
    List<Data> values = snapshot.data;
    final pIntValues = values.map<int>((m) => m.p.toInt()).toSet().toList(growable: false)..sort();

    return CustomScrollView(
      slivers: <Widget>[
        SliverList(
          delegate: SliverChildBuilderDelegate((context, i) {
            final p = pIntValues[i];
            final items = values
                .where((m) => p <= m.p && m.p < p + 1)
                .toList(growable: false)
                  ..sort((a, b) => a.p.compareTo(b.p));
//check if not items, return Container if true
            if(items.length == 0)
              return Container(); 
            return GridView.builder(
              itemCount: items.length,
              shrinkWrap: true,
              primary: false,
             gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4),
              itemBuilder: (context, i) {
                final item = items[i];
                return _builderItem(item);
              },
            );
          }, childCount: pIntValues.length),
        )
      ],
    );
  }

Returning Container instead of GridView if there are no items for that group.



标签: dart flutter