嗨,我是新手,要在元素完成后停止GridView.builder
滚动。
EX::我有一个由9个项目组成的列表,我希望gridView.builder
仅创建9个项目= > list.length 。
此处的代码 =>列表
List<Map<String, Object>> values = [
{ "id":0,
"category":"موبيلات",
'categorybackimage':'images/backgroundimage',
'categoryimage':'images/iphone'},{ "id":0,
"category":"موبيلات",
'categorybackimage':'images/backgroundimage',
'categoryimage':'images/iphone'},{ "id":0,
"category":"موبيلات",
'categorybackimage':'images/backgroundimage',
'categoryimage':'images/iphone'},{ "id":0,
"category":"موبيلات",
'categorybackimage':'images/backgroundimage',
'categoryimage':'images/iphone'},{ "id":0,
"category":"موبيلات",
'categorybackimage':'images/backgroundimage',
'categoryimage':'images/iphone'},{ "id":0,
"category":"موبيلات",
'categorybackimage':'images/backgroundimage',
'categoryimage':'images/iphone'},{ "id":0,
"category":"موبيلات",
'categorybackimage':'images/backgroundimage',
'categoryimage':'images/iphone'},{ "id":0,
"category":"موبيلات",
'categorybackimage':'images/backgroundimage',
'categoryimage':'images/iphone'},{ "id":0,
"category":"موبيلات",
'categorybackimage':'images/backgroundimage',
'categoryimage':'images/iphone'},
];
,这里是 => GridView
GridView.builder(gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2,mainAxisSpacing: 2,), itemBuilder: (BuildContext,i){
for(i=0;i<values.length;i++){
return
Padding(
padding: const EdgeInsets.all(2.0),
child: SizedBox(
height: 400,
width: 200,
child: InkWell(
onTap: (){},
child: Stack(
children: <Widget>[
ClipRRect(borderRadius: BorderRadius.all(Radius.circular(20)),
child: Image.asset(values[i]['categorybackimage'],fit: BoxFit.fill, )),
Column(
children: <Widget>[
Text("${(values[i]['category'])}"),
Card(
color: Colors.white,
child: Text(
"عروض ",
style: TextStyle(
color: Colors.red,
fontSize: 20,
fontWeight: FontWeight.w900),
),
),
Image.asset(values[i]['categoryimage'],)
],
),
],
),
),
),
);
}}),
答案 0 :(得分:1)
itemCount
中有GridView.builder
个属性,将其设置为列表的长度。
GridView.builder(gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2,mainAxisSpacing: 2,),
itemCount:list.length,
itemBuilder: (BuildContext,i){ return YourAwesomeWidget(); });
您也不必在itembuilder
函数中使用for循环,gridview构建器可以循环浏览列表,而无需任何其他代码。