split method

  1. @override
List<Rect> split(
  1. Rect area
)
override

Splits the given area into multiple sub-areas.

Implementation

@override
List<Rect> split(Rect area) {
  var availableSpace = axis == Axis.horizontal ? area.height : area.width;
  var totalWeight = 0;

  final computedSize = List.filled(constraints.length, 0);
  for (final constraint in constraints) {
    switch (constraint) {
      case Fixed(:final size):
        availableSpace -= size;
      case Flexible(:final weight):
        totalWeight += weight;
    }
  }

  final perRatioSize = availableSpace / totalWeight;
  for (var i = 0; i < constraints.length; i++) {
    final constraint = constraints[i];
    switch (constraint) {
      case Fixed(:final size):
        computedSize[i] = size;
      case Flexible(:final weight):
        computedSize[i] = (perRatioSize * weight).floor();
    }
  }

  final splits = List.filled(constraints.length, Rect.zero);
  var offset = 0;
  for (var i = 0; i < computedSize.length; i++) {
    final size = computedSize[i];
    final Rect rect;
    if (axis == Axis.horizontal) {
      rect = Rect.fromLTWH(area.left, area.top + offset, area.width, size);
    } else {
      rect = Rect.fromLTWH(area.left + offset, area.top, size, area.height);
    }
    splits[i] = rect;
    offset += size;
  }

  return splits;
}