widgetsInUse static method

List<Widget> widgetsInUse(
  1. List<PriorityWidget> widgets,
  2. double availableWidth,
  3. bool isRow
)

Implementation

static List<Widget> widgetsInUse(
    List<PriorityWidget> widgets, double availableWidth, bool isRow) {
  final priorities = widgets.map((e) => e.priority).toSet().toList()
    ..sort((a, b) => a.compareTo(b));

  final widgetsInUse = <int, Widget>{};

  double usedWidth = 0;

  for (int priority in priorities) {
    final widgetsWithPriority = widgets.where((e) => e.priority == priority);

    for (PriorityWidget candidate in widgetsWithPriority) {
      if (availableWidth - usedWidth >= candidate.sizeInMainAxis) {
        final originalIndex = widgets.indexOf(candidate);

        widgetsInUse[originalIndex] = SizedBox(
          width: isRow ? candidate.sizeInMainAxis : null,
          height: isRow ? null : candidate.sizeInMainAxis,
          child: candidate.child,
        );

        usedWidth += candidate.sizeInMainAxis;
      }
    }
  }
  return mapToListSortedByKey(widgetsInUse);
}