如何在布局中随机放置小部件

How to randomly position widgets in a layout

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

我正在考虑使用换行小部件,但这并没有停止工作,因为它没有随机化一行中的 children。到目前为止我的代码

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),
      )
    ],
  );
}

您可以创建 class 人,并存储个人资料名称和图像,

class Person {
  String name;
  String imageUrl;
}

并且在您的代码中可以将所有人员存储在数组中

List<Person> persons = [Person(), Person(),....]


Wrap(
   spacing: 30,
   children: _children
);

List<Widget> _children {
  List<Widget> _widgets = List<Widget>();
  List<Persons> _randomList = persons.shuffle();
   
  _randomList.forEach((person) {
      _widgets.add(_buildProfile(person))
   });

  return _widgets;
}


您可以使用 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
  );