From 32b554335bb8e8aa176fc1427e33dec024f9b7c1 Mon Sep 17 00:00:00 2001 From: vinothvino42 Date: Thu, 7 Dec 2023 10:50:53 +0530 Subject: [PATCH 1/2] feat: add onTap and onTapHaptic actions for the bottom nav bar item --- lib/framework/view/bottom_nav_page_group.dart | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/framework/view/bottom_nav_page_group.dart b/lib/framework/view/bottom_nav_page_group.dart index 91ff12059..2c8ac0a74 100644 --- a/lib/framework/view/bottom_nav_page_group.dart +++ b/lib/framework/view/bottom_nav_page_group.dart @@ -13,14 +13,16 @@ import 'package:ensemble/util/utils.dart'; import 'package:ensemble/framework/widget/icon.dart' as ensemble; import 'package:flutter/material.dart'; -class FABBottomAppBarItem { - FABBottomAppBarItem({ +class BottomNavBarItem { + BottomNavBarItem({ required this.icon, required this.text, required this.isCustom, this.activeIcon, this.isFloating = false, this.floatingMargin, + this.onTap, + this.onTapHaptic, }); Widget icon; @@ -29,6 +31,8 @@ class FABBottomAppBarItem { bool isFloating; bool isCustom; double? floatingMargin; + EnsembleAction? onTap; + String? onTapHaptic; } enum FloatingAlignment { @@ -240,7 +244,7 @@ class _BottomNavPageGroupState extends State } Widget? _buildBottomNavBar() { - List navItems = []; + List navItems = []; final unselectedColor = Utils.getColor(widget.menu.styles?['color']) ?? Theme.of(context).unselectedWidgetColor; @@ -272,11 +276,13 @@ class _BottomNavPageGroupState extends State : null); navItems.add( - FABBottomAppBarItem( + BottomNavBarItem( icon: icon, activeIcon: activeIcon, isCustom: isCustom, text: label, + onTap: EnsembleAction.fromYaml(item.onTap), + onTapHaptic: item.onTapHaptic, ), ); } @@ -315,6 +321,21 @@ class _BottomNavPageGroupState extends State PageGroupWidget.getPageController(context)!.jumpToPage(index); viewGroupNotifier.updatePage(index); } + + // Executing onTap action + if (navItems[index].onTap != null) { + ScreenController().executeActionWithScope( + context, widget.scopeManager, navItems[index].onTap!); + } + + // Executing haptic feedback action + if (navItems[index].onTapHaptic != null) { + ScreenController().executeAction( + context, + HapticAction( + type: navItems[index].onTapHaptic!, onComplete: null), + ); + } }, items: navItems, isFloating: fabMenuItem != null, @@ -360,7 +381,7 @@ class EnsembleBottomAppBar extends StatefulWidget { }) { // assert(items.length == 2 || items.length == 4); } - final List items; + final List items; final int selectedIndex; final double? height; final dynamic margin; @@ -484,7 +505,7 @@ class EnsembleBottomAppBarState extends State { } Widget _buildTabItem({ - required FABBottomAppBarItem item, + required BottomNavBarItem item, required int index, required ValueChanged onPressed, }) { From 2b394ad2b1b9183832abe9d53e5fbcf377caadfd Mon Sep 17 00:00:00 2001 From: vinothvino42 Date: Thu, 7 Dec 2023 18:38:38 +0530 Subject: [PATCH 2/2] feat: add a switchScreen property to disable view switching and allow the user to manually handle it with onTap callback --- assets/schema/ensemble_schema.json | 4 ++ lib/framework/menu.dart | 3 ++ lib/framework/view/bottom_nav_page_group.dart | 41 +++++++++++++------ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/assets/schema/ensemble_schema.json b/assets/schema/ensemble_schema.json index 3209c8b69..7051c0051 100644 --- a/assets/schema/ensemble_schema.json +++ b/assets/schema/ensemble_schema.json @@ -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." diff --git a/lib/framework/menu.dart b/lib/framework/menu.dart index 0ac749304..b423537f9 100644 --- a/lib/framework/menu.dart +++ b/lib/framework/menu.dart @@ -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), @@ -207,6 +208,7 @@ class MenuItem { this.iconLibrary, this.selected, this.floating = false, + this.switchScreen = true, this.floatingAlignment = 'center', this.floatingMargin, this.onTap, @@ -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; diff --git a/lib/framework/view/bottom_nav_page_group.dart b/lib/framework/view/bottom_nav_page_group.dart index 2c8ac0a74..5f82ff030 100644 --- a/lib/framework/view/bottom_nav_page_group.dart +++ b/lib/framework/view/bottom_nav_page_group.dart @@ -21,6 +21,7 @@ class BottomNavBarItem { this.activeIcon, this.isFloating = false, this.floatingMargin, + this.switchScreen = true, this.onTap, this.onTapHaptic, }); @@ -31,6 +32,7 @@ class BottomNavBarItem { bool isFloating; bool isCustom; double? floatingMargin; + bool? switchScreen; EnsembleAction? onTap; String? onTapHaptic; } @@ -281,6 +283,7 @@ class _BottomNavPageGroupState extends State activeIcon: activeIcon, isCustom: isCustom, text: label, + switchScreen: item.switchScreen, onTap: EnsembleAction.fromYaml(item.onTap), onTapHaptic: item.onTapHaptic, ), @@ -315,17 +318,20 @@ class _BottomNavPageGroupState extends State 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 @@ -346,6 +352,13 @@ class _BottomNavPageGroupState extends State ); } + 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; @@ -412,9 +425,11 @@ class EnsembleBottomAppBarState extends State { void _updateIndex(int index) { widget.onTabSelected(index); - setState(() { - _selectedIndex = index; - }); + if (widget.items[index].switchScreen == true) { + setState(() { + _selectedIndex = index; + }); + } } int? getFabIndex() {