Flutter,Wrap,remove spacing for conditional widgets

n7taea2i  于 2023-04-07  发布在  Flutter
关注(0)|答案(2)|浏览(94)

我使用小部件Wrap是这样的:

Wrap(
  crossAxisAlignment: WrapCrossAlignment.center,
  spacing: 5,
  children: [
    condition1 ? Container(width: 50, height: 50, color: Colors.blue) : SizedBox(),
    condition2 ? Container(width: 50, height: 50, color: Colors.red) : SizedBox(),
    condition3 ? Container(width: 50, height: 50, color: Colors.green) : SizedBox()
  ]
)

如何只在Container上而不在SizedBox上应用间距?

brgchamk

brgchamk1#

尝试 Package 容器构件上的填充

bnl4lu3b

bnl4lu3b2#

如果SizedBox只是一个占位符,在其他方面没有任何用处,那么你根本不需要它。查看替代语法:

Wrap(
        crossAxisAlignment: WrapCrossAlignment.center,
        spacing: 5,
        children: [
          if (condition1) Container(width: 50, height: 50, color: Colors.blue),
          if (condition2) Container(width: 50, height: 50, color: Colors.red),
          if (condition3) Container(width: 50, height: 50, color: Colors.green),
        ])

相关问题