groupGlassNavBarItems function

List<GlassNavBarGroup> groupGlassNavBarItems(
  1. List<GlassBarItem> items, {
  2. Axis axis = Axis.horizontal,
  3. double? buttonSize,
})

Splits a cluster's items into the shells that will actually be drawn.

Runs of GlassBarItemBackground.shared items collapse into one group; every other item stands alone, so it can be given its own shell or none. A GlassBarItem.spacer ends a run, so the items either side of it draw separate shells.

Shared with GlassAppBar's in-route fallback; this library is not exported from the package barrel.

Implementation

List<GlassNavBarGroup> groupGlassNavBarItems(
  List<GlassBarItem> items, {
  Axis axis = Axis.horizontal,
  double? buttonSize,
}) {
  final groups = <GlassNavBarGroup>[];
  var run = <GlassBarActionItem>[];

  void flushRun() {
    if (run.isEmpty) return;
    groups.add(GlassNavBarGroup(
      items: run,
      background: GlassBarItemBackground.shared,
      axis: axis,
      buttonSize: buttonSize,
    ));
    run = <GlassBarActionItem>[];
  }

  for (final item in items) {
    // A spacer: the only item that is not an action.
    if (item is! GlassBarActionItem) {
      flushRun();
      continue;
    }
    assert(
      item.tintColor == null ||
          item.background != GlassBarItemBackground.shared,
      'GlassBarItem.tintColor is only supported for '
      'GlassBarItemBackground.separate items. A shared capsule is a single '
      'glass mesh and cannot tint individual slots. Set background: '
      'GlassBarItemBackground.separate to use tintColor.',
    );
    if (item.background == GlassBarItemBackground.shared) {
      run.add(item);
      continue;
    }
    flushRun();
    groups.add(GlassNavBarGroup(
      items: [item],
      background: item.background,
      axis: axis,
      buttonSize: buttonSize,
    ));
  }
  flushRun();
  return groups;
}