Skip to content

Commit

Permalink
adding builder for single route item
Browse files Browse the repository at this point in the history
  • Loading branch information
g-apparence committed Oct 11, 2023
1 parent 6d69ee2 commit 8e2f95c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 24 deletions.
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
41 changes: 40 additions & 1 deletion lib/bart/bart_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ typedef BartPageBuilder = Widget Function(
RouteSettings? settings,
);

typedef IconBuilder = Widget Function(
BuildContext context,
);

class BartMenuRoute {
String? label;
IconData? icon;
IconBuilder? iconBuilder;

/// The optional [IconData] that's displayed when this
/// [NavigationDestination] is selected.
Expand All @@ -32,6 +37,7 @@ class BartMenuRoute {
BartMenuRoute._({
this.label,
this.icon,
this.iconBuilder,
this.selectedIcon,
required this.path,
required this.pageBuilder,
Expand All @@ -43,7 +49,16 @@ class BartMenuRoute {
this.maintainState,
this.transitionsBuilder,
this.transitionDuration,
});
}) {
assert(
icon != null || iconBuilder != null,
"You must provide an icon or an iconWidget",
);
assert(
icon == null || iconBuilder == null,
"You can't provide both an icon and an iconWidget",
);
}

factory BartMenuRoute.bottomBar({
required String label,
Expand All @@ -69,6 +84,30 @@ class BartMenuRoute {
showBottomBar: true,
);

factory BartMenuRoute.bottomBarBuilder({
required String? label,
required IconBuilder builder,
required String path,
required BartPageBuilder pageBuilder,
RouteTransitionsBuilder? transitionsBuilder,
Duration? transitionDuration,
bool cache = true,
IconData? selectedIcon,
}) =>
BartMenuRoute._(
label: label,
iconBuilder: builder,
path: path,
cache: cache,
type: BartMenuRouteType.bottomNavigation,
pageBuilder: pageBuilder,
settings: RouteSettings(name: path),
transitionsBuilder: transitionsBuilder,
transitionDuration: transitionDuration,
selectedIcon: selectedIcon,
showBottomBar: true,
);

factory BartMenuRoute.innerRoute({
required String path,
required BartPageBuilder pageBuilder,
Expand Down
27 changes: 20 additions & 7 deletions lib/bart/widgets/bottom_bar/styles/bottom_bar_cupertino.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BartCupertinoBottomBar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoTabBar(
items: routeWidgetList,
items: getRouteWidgetList(context),
currentIndex: currentIndex,
iconSize: theme.iconSize,
border: theme.border,
Expand All @@ -32,10 +32,23 @@ class BartCupertinoBottomBar extends StatelessWidget {
);
}

List<BottomNavigationBarItem> get routeWidgetList => routes
.map((route) => BottomNavigationBarItem(
icon: Icon(route.icon),
label: route.label,
))
.toList();
List<BottomNavigationBarItem> getRouteWidgetList(BuildContext context) =>
routes.map(
(route) {
if (route.icon != null) {
return BottomNavigationBarItem(
icon: Icon(route.icon),
label: route.label,
);
} else if (route.iconBuilder != null) {
return BottomNavigationBarItem(
icon: route.iconBuilder!(context),
label: route.label,
);
}
throw Exception(
"You must provide an icon or an iconBuilder for each route",
);
},
).toList();
}
37 changes: 27 additions & 10 deletions lib/bart/widgets/bottom_bar/styles/bottom_bar_material.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BartMaterial3BottomBar extends StatelessWidget {
Widget build(BuildContext context) {
return NavigationBar(
selectedIndex: currentIndex,
destinations: routeWidgetList,
destinations: getRouteWidgetList(context),
elevation: theme.elevation,
backgroundColor: theme.bgColor,
height: theme.height,
Expand All @@ -73,13 +73,30 @@ class BartMaterial3BottomBar extends StatelessWidget {
);
}

List<NavigationDestination> get routeWidgetList => routes
.where((element) => element.type == BartMenuRouteType.bottomNavigation)
.map((route) => NavigationDestination(
icon: Icon(route.icon),
label: route.label ?? '',
selectedIcon:
route.selectedIcon != null ? Icon(route.selectedIcon) : null,
))
.toList();
List<NavigationDestination> getRouteWidgetList(BuildContext context) => routes
.where(
(element) => element.type == BartMenuRouteType.bottomNavigation)
.map(
(route) {
if (route.icon != null) {
return NavigationDestination(
icon: Icon(route.icon),
label: route.label ?? '',
selectedIcon:
route.selectedIcon != null ? Icon(route.selectedIcon) : null,
);
} else if (route.iconBuilder != null) {
return NavigationDestination(
icon: route.iconBuilder!(context),
label: route.label ?? '',
// selectedIcon: route.selectedIconBuilder != null
// ? route.selectedIconBuilder!(context)
// : null,
);
}
throw Exception(
"You must provide an icon or an iconBuilder for each route",
);
},
).toList();
}
10 changes: 5 additions & 5 deletions lib/bart/widgets/nested_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _NestedNavigatorState extends State<NestedNavigator>
child: Navigator(
key: widget.navigationKey,
initialRoute: widget.initialRoute,
observers: [routeObserver],
// observers: [routeObserver],
onGenerateRoute: (RouteSettings routeSettings) {
Actions.invoke(context, AppBarBuildIntent.empty());
hideAppBar(context);
Expand Down Expand Up @@ -101,15 +101,15 @@ class _NestedNavigatorState extends State<NestedNavigator>
},
),
onWillPop: () {
// showBottomBar(context);
showBottomBar(context);
if (widget.onWillPop != null) {
widget.onWillPop!();
}
if (widget.navigationKey.currentState!.canPop()) {
if (widget.navigationKey.currentState != null &&
widget.navigationKey.currentState!.canPop()) {
widget.navigationKey.currentState!.pop();
return Future<bool>.value(false);
}
return Future<bool>.value(true);
return Future<bool>.value(false);
},
);
}
Expand Down

0 comments on commit 8e2f95c

Please sign in to comment.