如何阻止gridview.builder在flutter中生成项目

时间:2019-07-19 17:29:07

标签: flutter dart

嗨,我是新手,要在元素完成后停止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'],)
                                ],
                              ),
                                ],
                              ),
                            ),
                          ),
                        );
                    }}),

1 个答案:

答案 0 :(得分:1)

itemCount中有GridView.builder个属性,将其设置为列表的长度。

GridView.builder(gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2,mainAxisSpacing: 2,), 
itemCount:list.length,
itemBuilder: (BuildContext,i){ return YourAwesomeWidget(); });

您也不必在itembuilder函数中使用for循环,gridview构建器可以循环浏览列表,而无需任何其他代码。