首页 > 解决方案 > 如何在布局中随机放置小部件

问题描述

假设我想将小部件随机放置在特定布局中,如下图所示,我该如何实现?

图片

我正在考虑使用 wrap 小部件,但这并没有停止工作,因为它不会随机排列一行中的孩子。我的代码到现在

return Wrap(
   spacing: 30,
   children: [
        buildprofile(),
        buildprofile(),
        buildprofile(),
        buildprofile(),
      ],
    );

buildprofile() {
  return Column(
    children: [
      CircleAvatar(
        radius: 64,
        backgroundColor: Colors.pink,
        child: (CircleAvatar(
          radius: 62,
          backgroundImage: NetworkImage(profilepic),
        )),
      ),
      SizedBox(
        height: 10,
      ),
      Text(
        "Sivaram",
        style: mystyle(16, Colors.black, FontWeight.w700),
      )
    ],
  );
}

标签: flutterdartwidgetflutter-layout

解决方案


你可以使用flutter_staggered_grid_view

  StaggeredGridView.count(
    crossAxisCount: 4,
    children: List.generate(
        3,
        (index) => Center(
              child: CircleAvatar(
                radius: 64,
                backgroundColor: Colors.pink,
              ),
            )),
    staggeredTiles: [
      StaggeredTile.count(2, 2), // takes up 2 rows and 2 columns space
      StaggeredTile.count(2, 1), // takes up 2 rows and 1 column
      StaggeredTile.count(1, 2), // takes up 1 row and 2 column space
    ], // scatter them randomly
  );

在此处输入图像描述


推荐阅读