Flutter SizeBox.fromSize()创建一个指定Size的SizedBox

17 min read

一般的SizeBox()构造方法:传width、height、child,SizedBox会强制设置它的孩子的宽度或者高度为指定值

SizeBox.expand():可以使SizedBox的大小充满parent的布局,相当于设置了SizedBox的宽度和高度为double.infinity(无穷大)。

SizeBox.fromSize():创建一个指定Size的SizedBox

FractionallySizedBox():可以用百分比来控制sizebox的大小。 widthFactor,heightFactor参数就是相对于父控件的比例。 alignment:可以设置sizebox在父控件里面的相对位置。

SizedOverflowBox():通过设置Size,允许它的child控件,溢出它的父控件,进行绘制,不会报OverFlow的错误。

OverflowBox():通过设置最大最小的宽高,OverflowBox允许它的child控件,溢出它的父控件,进行绘制,不会报OverFlow的错误。

LimtedBox():一个可以限制子控件的最大宽高的控件,child只能在这区域内进行绘制

Size类的部分源码

class Size extends OffsetBase {
  /// Creates a [Size] with the given [width] and [height].
  const Size(double width, double height) : super(width, height);

  /// Creates an instance of [Size] that has the same values as another.
  // Used by the rendering library's _DebugSize hack.
  Size.copy(Size source) : super(source.width, source.height);

  /// Creates a square [Size] whose [width] and [height] are the given dimension.
  ///
  /// See also:
  ///
  ///  * [Size.fromRadius], which is more convenient when the available size
  ///    is the radius of a circle.
  const Size.square(double dimension) : super(dimension, dimension);

  /// Creates a [Size] with the given [width] and an infinite [height].
  const Size.fromWidth(double width) : super(width, double.infinity);

  /// Creates a [Size] with the given [height] and an infinite [width].
  const Size.fromHeight(double height) : super(double.infinity, height);