Skip to content

Commit

Permalink
feat: add a switchScreen property to disable view switching and allow…
Browse files Browse the repository at this point in the history
… the user to manually handle it with onTap callback
  • Loading branch information
vinothvino42 committed Dec 7, 2023
1 parent 32b5543 commit 2b394ad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
4 changes: 4 additions & 0 deletions assets/schema/ensemble_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5080,6 +5080,10 @@
"type": "boolean",
"description": "Mark this item as a floating icon"
},
"switchScreen": {
"type": "boolean",
"description": "Disable the screen switching when clicking the bottom nav bar item. Default (True)"
},
"floatingMargin": {
"$ref": "#/$defs/Margin-payload",
"description": "The margin around the floating."
Expand Down
3 changes: 3 additions & 0 deletions lib/framework/menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ abstract class Menu {
floatingAlignment:
Utils.optionalString(item['floatingAlignment']) ?? 'center',
floatingMargin: Utils.optionalInt(item['floatingMargin']),
switchScreen: Utils.getBool(item['switchScreen'], fallback: true),
onTap: item['onTap'],
onTapHaptic: Utils.optionalString(item['onTapHaptic']),
isExternal: Utils.getBool(item['isExternal'], fallback: false),
Expand Down Expand Up @@ -207,6 +208,7 @@ class MenuItem {
this.iconLibrary,
this.selected,
this.floating = false,
this.switchScreen = true,
this.floatingAlignment = 'center',
this.floatingMargin,
this.onTap,
Expand All @@ -223,6 +225,7 @@ class MenuItem {
final String? iconLibrary;
final dynamic selected;
final bool floating;
final bool switchScreen;
final String floatingAlignment;
final int? floatingMargin;
final dynamic onTap;
Expand Down
41 changes: 28 additions & 13 deletions lib/framework/view/bottom_nav_page_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BottomNavBarItem {
this.activeIcon,
this.isFloating = false,
this.floatingMargin,
this.switchScreen = true,
this.onTap,
this.onTapHaptic,
});
Expand All @@ -31,6 +32,7 @@ class BottomNavBarItem {
bool isFloating;
bool isCustom;
double? floatingMargin;
bool? switchScreen;
EnsembleAction? onTap;
String? onTapHaptic;
}
Expand Down Expand Up @@ -281,6 +283,7 @@ class _BottomNavPageGroupState extends State<BottomNavPageGroup>
activeIcon: activeIcon,
isCustom: isCustom,
text: label,
switchScreen: item.switchScreen,
onTap: EnsembleAction.fromYaml(item.onTap),
onTapHaptic: item.onTapHaptic,
),
Expand Down Expand Up @@ -315,17 +318,20 @@ class _BottomNavPageGroupState extends State<BottomNavPageGroup>
Utils.getShadowBlurStyle(widget.menu.styles?['shadowStyle']),
notchedShape: const CircularNotchedRectangle(),
onTabSelected: (index) {
if (widget.menu.reloadView == true) {
viewGroupNotifier.updatePage(index);
final isSwitchScreen =
Utils.getBool(navItems[index].switchScreen, fallback: true);
if (isSwitchScreen) {
if (widget.menu.reloadView == true) {
viewGroupNotifier.updatePage(index);
} else {
PageGroupWidget.getPageController(context)!.jumpToPage(index);
viewGroupNotifier.updatePage(index);
}

_onTap(navItems[index]);
} else {
PageGroupWidget.getPageController(context)!.jumpToPage(index);
viewGroupNotifier.updatePage(index);
}

// Executing onTap action
if (navItems[index].onTap != null) {
ScreenController().executeActionWithScope(
context, widget.scopeManager, navItems[index].onTap!);
// Execute only onTap action. Page switching is handled by the developer with onTap
_onTap(navItems[index]);
}

// Executing haptic feedback action
Expand All @@ -346,6 +352,13 @@ class _BottomNavPageGroupState extends State<BottomNavPageGroup>
);
}

void _onTap(BottomNavBarItem menuItem) {
if (menuItem.onTap != null) {
ScreenController().executeActionWithScope(
context, widget.scopeManager, menuItem.onTap!);
}
}

Widget? _buildCustomIcon(MenuItem item, {bool isActive = false}) {
dynamic customWidgetModel =
isActive ? item.customActiveWidget : item.customWidget;
Expand Down Expand Up @@ -412,9 +425,11 @@ class EnsembleBottomAppBarState extends State<EnsembleBottomAppBar> {

void _updateIndex(int index) {
widget.onTabSelected(index);
setState(() {
_selectedIndex = index;
});
if (widget.items[index].switchScreen == true) {
setState(() {
_selectedIndex = index;
});
}
}

int? getFabIndex() {
Expand Down

0 comments on commit 2b394ad

Please sign in to comment.