如何删除GridView中的空项目计数? [明智的GridView问题] [英] how to remove null item count in GridView? [Group wise GridView Issue]
问题描述
需要创建逐组网格视图。我快完成了。我的json看起来像这样,
Need to create group wise grid view. I am almost done. My json looks like this,
[{ d: Gym, p:0},{ d: 升降机, p:0},{ d:办公室,
p:0},{ d:客房, p:1.1},{ d:房屋前部,
p:1.2},{ d:餐厅, p:1.3},{ d:多功能厅,
p:1.4},{ d:外部, p:1.5},{ d:娱乐, p:
1.6},{ d:洗衣, p:3.1},{ d: A / V设备, p:3.2},{ d:餐饮设备, p:3.3},{ d :其他设备, p:
4.1},{ d:房屋后面, p:4.2},{ d:中央植物, p: 4.3},{ d:标题, p:5},{ d:管道, p:5.1
},{ d:基本服务, p:5.2},{ d:车间
设备, p:5.3},{ d: HSK设备, p:5.4},{ d:
电气设备, p:5.5},{ d:商务中心, p:100
}]
[ { "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;
}
这是我的输出,
我在json <$中发现了为什么会发生问题c $ c> P 在通过3.1,3.2 ..之后又通过了1.1,1.2,1.3。空格,因为缺少数字02
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
推荐答案
在这里,您要去:
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),
)
],
);
}
返回容器
如果没有该组的项目,则为 GridView
。
Returning Container
instead of GridView
if there are no items for that group.
这篇关于如何删除GridView中的空项目计数? [明智的GridView问题]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!