Skip to content

Commit

Permalink
feat(UX-1096): Added shrink items to navigation bar so the items can …
Browse files Browse the repository at this point in the history
…expand or shrink. To match web (#210)

feat: Added shrink items to navigation bar so the items can expand to fill the space and shrink

fix: added shrink items to widgetbook
  • Loading branch information
DE7924 authored Nov 12, 2024
1 parent bfbfa23 commit 5a785f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Widget navigationBarUseCase(BuildContext context) {
bool showButton = context.knobs.boolean(label: 'Button');
int? dividerIndex = context.knobs.intOrNull.slider(label: 'Divider', min: 0, max: 6, initialValue: null);
bool showSplit = context.knobs.boolean(label: 'Split Items');
bool shrinkItems = context.knobs.boolean(label: 'Shrink Items');
return StatefulBuilder(builder: (context, setState) {
return WidgetbookScaffold(
builder: (context, _) => ZetaNavigationBar(
Expand All @@ -24,6 +25,7 @@ Widget navigationBarUseCase(BuildContext context) {
currentIndex: currIndex,
splitItems: showSplit,
dividerIndex: dividerIndex,
shrinkItems: shrinkItems,
),
);
});
Expand Down
34 changes: 24 additions & 10 deletions lib/src/components/navigation bar/navigation_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ZetaNavigationBar extends ZetaStatelessWidget {
this.dividerIndex,
this.action,
this.semanticLabel,
this.shrinkItems = false,
}) : assert(
items.length >= 2 && items.length <= 6,
'The number of items should be between 2 and 6',
Expand All @@ -56,6 +57,7 @@ class ZetaNavigationBar extends ZetaStatelessWidget {
this.onTap,
this.dividerIndex,
this.semanticLabel,
this.shrinkItems = false,
}) : splitItems = false,
action = null;

Expand All @@ -70,7 +72,8 @@ class ZetaNavigationBar extends ZetaStatelessWidget {
this.dividerIndex,
this.semanticLabel,
}) : splitItems = true,
action = null;
action = null,
shrinkItems = true;

/// Creates a [ZetaNavigationBar] with an action.
const ZetaNavigationBar.action({
Expand All @@ -81,6 +84,7 @@ class ZetaNavigationBar extends ZetaStatelessWidget {
this.currentIndex,
this.onTap,
this.semanticLabel,
this.shrinkItems = false,
}) : dividerIndex = null,
splitItems = false;

Expand All @@ -107,16 +111,25 @@ class ZetaNavigationBar extends ZetaStatelessWidget {
/// {@macro zeta-widget-semantic-label}
final String? semanticLabel;

/// When set to true the items will shrink to take up less space.
/// This is useful when there are many items in the navigation bar.
/// Defaults to false.
/// When set to false the items will take up equal space and will expand to fill the bar.
final bool shrinkItems;

Row _generateNavigationItemRow(List<ZetaNavigationBarItem> items, BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: items.map((navItem) {
final index = items.indexOf(navItem);
return _NavigationItem(
selected: index == currentIndex,
item: navItem,
onTap: () => onTap?.call(index),
context: context,
return Expanded(
flex: !shrinkItems ? 1 : 0,
child: _NavigationItem(
selected: index == currentIndex,
item: navItem,
onTap: () => onTap?.call(index),
context: context,
),
);
}).toList(),
);
Expand All @@ -143,21 +156,21 @@ class ZetaNavigationBar extends ZetaStatelessWidget {
child = Row(
mainAxisAlignment: splitItems ? MainAxisAlignment.spaceBetween : MainAxisAlignment.spaceAround,
children: [
_generateNavigationItemRow(leftItems, context),
Expanded(flex: !shrinkItems ? 1 : 0, child: _generateNavigationItemRow(leftItems, context)),
if (dividerIndex != null)
Container(
color: colors.borderSubtle,
width: _navigationItemBorderWidth,
height: Zeta.of(context).spacing.xl_7,
),
_generateNavigationItemRow(rightItems, context),
Expanded(flex: !shrinkItems ? 1 : 0, child: _generateNavigationItemRow(rightItems, context)),
],
);
} else if (action != null) {
child = Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_generateNavigationItemRow(items, context),
Expanded(flex: !shrinkItems ? 1 : 0, child: _generateNavigationItemRow(items, context)),
action!,
],
);
Expand Down Expand Up @@ -186,7 +199,8 @@ class ZetaNavigationBar extends ZetaStatelessWidget {
..add(ObjectFlagProperty<void Function(int value)?>.has('onTap', onTap))
..add(DiagnosticsProperty<bool>('splitItems', splitItems))
..add(IntProperty('dividerIndex', dividerIndex))
..add(StringProperty('semanticLabel', semanticLabel));
..add(StringProperty('semanticLabel', semanticLabel))
..add(DiagnosticsProperty<bool>('shrinkItems', shrinkItems));
}
}

Expand Down

0 comments on commit 5a785f0

Please sign in to comment.