Expanded

一个可以扩展Row、Column或者Flex子组件的组件,以便子组件填充可用空间

使用Expanded组件可以让RowColumnFlex的子组件沿着主轴扩展以填满可用空间。如果有多个子组件被扩展,可用空间将根据flex因子在它们之间分配。

Expanded组件必须是RowColumnFlex的后代,并且从Expanded组件到其包含的RowColumnFlex的路径中只能包含StatelessWidgetsStatefulWidgets不能是其他类型的组件。

示例

示例1

Center(
  child: Column(
    children: <Widget>[
      Container(color: Colors.blue, height: 100, width: 100),
      Expanded(child: Container(color: Colors.amber, width: 100)),
      Container(color: Colors.blue, height: 100, width: 100),
    ],
  ),
)

这个例子显示了如何在Column中使用一个Expanded组件放在中间扩展高度来撑满剩余空间。

示例2

Center(
  child: Row(
    children: <Widget>[
      Expanded(flex: 2, child: Container(color: Colors.amber, height: 100)),
      Container(color: Colors.blue, height: 100, width: 50),
      Expanded(child: Container(color: Colors.amber, height: 100)),
    ],
  ),
)

这个例子展示了在Row中有多个Expanded组件时,根据flex系数来决定填充剩余空间的比例。

构造函数

Expanded.new({
  Key? key, 
  int flex = 1, 
  required Widget child
})

参数

参数名参数类型说明
childWidget子组件
fitFlexFit控制子组件在主轴上如何填充剩余空间
flexint多个Expanded组件对于剩余空间的分配比例

额外说明

fit

  • FlexFit.tight默认值,子组件会被强制拉伸,完全填满分配给Expanded的剩余空间,即使子组件本身有更小或更大的约束。
  • FlexFit.loose,允许子组件的保持自身的约束不被拉伸,如剩余空间宽度有200,子组件设置的宽度为50(Container(width: 50)),它不会强制拉伸,而是保持原尺寸。