在Flutter GridView构建器中添加自定义项

4
我正在尝试在gridview.builder中添加一个新的自定义按钮。在我的gridview中,我有5个项目,请参见下图 enter image description here 请查看我的简单代码。
 GridView.builder(
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 3),
                        itemCount: _categoryList.length,
                        itemBuilder: (context, index) {
                          if (_categoryList.length != 0) {
                            return Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: CircleAvatar(
                                backgroundColor: Colors.transparent,
                                backgroundImage: NetworkImage(_categoryList[index].icon,),
                                  ),
                            );
                          } else
                            return Container();
                        },
                      ),
1个回答

3
在子元素中,您可以请求索引为5并显示按钮组件,而不是照片列表项:
GridView.builder(
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 3),
                    itemCount: _categoryList.length,
                    itemBuilder: (context, index) {
                      if (_categoryList.length != 0) {
                        return Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: index == 5 ? myButton() : CircleAvatar(
                            backgroundColor: Colors.transparent,
                            backgroundImage: NetworkImage(_categoryList[index].icon,),
                              ),
                        );
                      } else
                        return Container();
                    },
                  ),

尝试此操作时出现错误。 - Ferer Atlus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接