如何在Flutter中的StaggeredGridView中使用堆栈小部件?

时间:2020-11-09 13:25:06

标签: flutter layout flutter-layout staggered-gridview flutter-layoutbuilder

Before Adding stack widget

After Adding stack widget om / cFMse.png

当我在StaggeredGridView中使用堆栈小部件时,为什么图像会变得怪异的大小。当我从网格视图中删除堆栈时,布局看起来就很好了。我是否以错误的方式使用布局?

new StaggeredGridView.countBuilder(
                crossAxisCount: 2,
                crossAxisSpacing: 10,
                mainAxisSpacing: 10,
                itemCount:model.hit.length,
                itemBuilder: (context, index) {
                  return
                    Stack(
                     children: [
                       Container(
                         decoration: BoxDecoration(
                             color: Colors.transparent,
                             borderRadius: BorderRadius.all(Radius.circular(35))),
                         child:
                         ClipRRect(
                             borderRadius: BorderRadius.all(Radius.circular(35)),
                             child:FadeInImage.memoryNetwork(
                               placeholder: kTransparentImage, image: model.hit[index]['largeImageURL'],fit: BoxFit.cover,)

                         ),


                       )
                     ],
                    );

                },
                staggeredTileBuilder: (index) {
                  return new StaggeredTile.count(1, index.isEven ? 1.2 : 1.8);
                })

1 个答案:

答案 0 :(得分:1)

尝试将StackFit.expand参数传递给堆栈

return
    Stack(
       fit: StackFit.expand, /// <-- expand to the size of the parent constraint
       children: [
          Container(
            decoration: BoxDecoration(
              color: Colors.transparent,
              borderRadius: BorderRadius.all(Radius.circular(35))),
              child: ClipRRect(
                 borderRadius: BorderRadius.all(Radius.circular(35)),
                 child:FadeInImage.memoryNetwork(
                   placeholder: kTransparentImage, image: model.hit[index]['largeImageURL'],fit: BoxFit.cover,)
                 ),
             )
        ],
);