Flexible与Expanded主要区别在于fit参数
Flexible中fit参数表示填满剩余空间的方式,说明如下:
tight: 必须强制填满剩余空间
loose: 尽可能大的填满剩余空间,但是可以不填满
源码如下:
enum FlexFit {
/// The child is forced to fill the available space.
///
/// The [Expanded] widget assigns this kind of [FlexFit] to its child.
tight,
/// The child can be at most as large as the available space (but is
/// allowed to be smaller).
///
/// The [Flexible] widget assigns this kind of [FlexFit] to its child.
loose,
}
Expanded继承Flexible具有Flexible的所有行为(面向对象特性),其默认构造函数强制fit参数为tight(强制填满剩余空间)
源码如下:
重点为: super(key: key, flex: flex, fit: FlexFit.tight, child: child) 这一行代码,调用了super的构造函数,强制fit为tight填充剩余空间
class Expanded extends Flexible {
/// Creates a widget that expands a child of a [Row], [Column], or [Flex]
/// so that the child fills the available space along the flex widget's
/// main axis.
const Expanded({
Key? key,
int flex = 1,
required Widget child,
}) : super(key: key, flex: flex, fit: FlexFit.tight, child: child);
}