Flexible與Expanded主要區(qū)別在于fit參數(shù)
Flexible中fit參數(shù)表示填滿剩余空間的方式,說明如下:
tight: 必須強(qiáng)制填滿剩余空間
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的所有行為(面向?qū)ο筇匦裕┧觯淠J(rèn)構(gòu)造函數(shù)強(qiáng)制fit參數(shù)為tight(強(qiáng)制填滿剩余空間)
源碼如下:
重點(diǎn)為: super(key: key, flex: flex, fit: FlexFit.tight, child: child) 這一行代碼,調(diào)用了super的構(gòu)造函數(shù)膘壶,強(qiáng)制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);
}