render method

  1. @override
void render(
  1. Buffer buffer,
  2. Rect area
)
override

Renders the underlying widget to the provided buffer within the area.

Implementation

@override
void render(Buffer buffer, Rect area) {
  final align = widget as Align;
  int childWidth = area.width;
  int childHeight = area.height;

  final childWidget = align.child;
  if (childWidget is SizedBox) {
    childWidth = childWidget.width ?? area.width;
    childHeight = childWidget.height ?? area.height;
  } else if (childWidget is ConstrainedBox) {
    childWidth = area.width.clamp(
      childWidget.constraints.minWidth,
      childWidget.constraints.maxWidth,
    );
    childHeight = area.height.clamp(
      childWidget.constraints.minHeight,
      childWidget.constraints.maxHeight,
    );
  }

  if (align.widthFactor != null) {
    childWidth = (childWidth * align.widthFactor!).round();
  }
  if (align.heightFactor != null) {
    childHeight = (childHeight * align.heightFactor!).round();
  }

  childWidth = childWidth.clamp(0, area.width);
  childHeight = childHeight.clamp(0, area.height);

  final double remainingWidth = (area.width - childWidth).toDouble();
  final int offsetX = (remainingWidth * (align.alignment.x + 1.0) / 2.0)
      .round();

  final double remainingHeight = (area.height - childHeight).toDouble();
  final int offsetY = (remainingHeight * (align.alignment.y + 1.0) / 2.0)
      .round();

  if (childElement != null &&
      childElement!.widget.runtimeType == childWidget.runtimeType) {
    childElement!.update(childWidget);
  } else {
    childElement = childWidget.createElement();
    childElement!.mount(this);
  }

  final childArea = Rect(
    area.x + offsetX,
    area.y + offsetY,
    childWidth,
    childHeight,
  );

  final childViewport = Viewport(buffer, childArea);
  childElement!.render(childViewport, Rect(0, 0, childWidth, childHeight));
}