Browse Source

Fix bottom navigation bar tap perf

Neeraj Gupta 2 years ago
parent
commit
16db332ff5
3 changed files with 15 additions and 55 deletions
  1. 11 21
      lib/ui/home/home_bottom_nav_bar.dart
  2. 3 0
      lib/ui/home_widget.dart
  3. 1 34
      lib/ui/nav_bar.dart

+ 11 - 21
lib/ui/home/home_bottom_nav_bar.dart

@@ -41,12 +41,17 @@ class _HomeBottomNavigationBarState extends State<HomeBottomNavigationBar> {
     _tabChangedEventSubscription =
         Bus.instance.on<TabChangedEvent>().listen((event) {
       if (event.source != TabChangedEventSource.tabBar) {
-        debugPrint('index changed to ${event.selectedIndex}');
+        debugPrint('${(TabChangedEvent).toString()} index changed  from '
+            '$currentTabIndex to ${event.selectedIndex} via ${event.source}');
         if (mounted) {
           setState(() {
             currentTabIndex = event.selectedIndex;
           });
         }
+      } else if (event.source == TabChangedEventSource.tabBar &&
+          currentTabIndex == event.selectedIndex) {
+        // user tapped on the currently selected index on the tapBar
+        Bus.instance.fire(TabDoubleTapEvent(currentTabIndex));
       }
     });
   }
@@ -57,7 +62,8 @@ class _HomeBottomNavigationBarState extends State<HomeBottomNavigationBar> {
     super.dispose();
   }
 
-  void _onTabChange(int index) {
+  void _onTabChange(int index, {String mode = 'tabChanged'}) {
+    debugPrint("_TabChanged called via method $mode");
     Bus.instance.fire(
       TabChangedEvent(
         index,
@@ -66,15 +72,6 @@ class _HomeBottomNavigationBarState extends State<HomeBottomNavigationBar> {
     );
   }
 
-  void _onDoubleTap(int index) {
-    debugPrint("doubleTap on tab $index");
-    Bus.instance.fire(
-      TabDoubleTapEvent(
-        index,
-      ),
-    );
-  }
-
   @override
   Widget build(BuildContext context) {
     final bool filesAreSelected = widget.selectedFiles.files.isNotEmpty;
@@ -139,11 +136,9 @@ class _HomeBottomNavigationBarState extends State<HomeBottomNavigationBar> {
                                 onPressed: () {
                                   _onTabChange(
                                     0,
+                                    mode: "OnPressed",
                                   ); // To take care of occasional missing events
                                 },
-                                onDoubleTap: () {
-                                  _onDoubleTap(0);
-                                },
                               ),
                               GButton(
                                 margin: const EdgeInsets.fromLTRB(10, 6, 10, 6),
@@ -154,12 +149,10 @@ class _HomeBottomNavigationBarState extends State<HomeBottomNavigationBar> {
                                 onPressed: () {
                                   _onTabChange(
                                     1,
+                                    mode: "OnPressed",
                                   ); // To take care of occasional missing
                                   // events
                                 },
-                                onDoubleTap: () {
-                                  _onDoubleTap(1);
-                                },
                               ),
                               GButton(
                                 margin: const EdgeInsets.fromLTRB(10, 6, 8, 6),
@@ -170,17 +163,14 @@ class _HomeBottomNavigationBarState extends State<HomeBottomNavigationBar> {
                                 onPressed: () {
                                   _onTabChange(
                                     2,
+                                    mode: "OnPressed",
                                   ); // To take care
                                   // of occasional missing events
                                 },
-                                onDoubleTap: () {
-                                  _onDoubleTap(2);
-                                },
                               ),
                             ],
                             selectedIndex: currentTabIndex,
                             onTabChange: _onTabChange,
-                            onDoubleTap: _onDoubleTap,
                           ),
                         ),
                       ),

+ 3 - 0
lib/ui/home_widget.dart

@@ -88,7 +88,10 @@ class _HomeWidgetState extends State<HomeWidget> {
     _tabChangedEventSubscription =
         Bus.instance.on<TabChangedEvent>().listen((event) {
       if (event.source != TabChangedEventSource.pageView) {
+        debugPrint(
+            "TabChange going from $_selectedTabIndex to ${event.selectedIndex} souce: ${event.source}");
         _selectedTabIndex = event.selectedIndex;
+        // _pageController.jumpToPage(_selectedTabIndex);
         _pageController.animateToPage(
           event.selectedIndex,
           duration: const Duration(milliseconds: 100),

+ 1 - 34
lib/ui/nav_bar.dart

@@ -2,8 +2,6 @@
 
 library google_nav_bar;
 
-import 'dart:async';
-
 import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 
@@ -13,7 +11,6 @@ class GNav extends StatefulWidget {
     this.tabs,
     this.selectedIndex = 0,
     this.onTabChange,
-    this.onDoubleTap,
     this.gap,
     this.padding,
     this.activeColor,
@@ -40,7 +37,6 @@ class GNav extends StatefulWidget {
   final List<GButton> tabs;
   final int selectedIndex;
   final Function onTabChange;
-  final Function onDoubleTap;
   final double gap;
   final double tabBorderRadius;
   final double iconSize;
@@ -121,23 +117,8 @@ class _GNavState extends State<GNav> {
                     widget.tabBackgroundColor ??
                     Colors.transparent,
                 duration: widget.duration ?? const Duration(milliseconds: 500),
-                onDoubleTap: () {
-                  widget.onDoubleTap(widget.tabs.indexOf(t));
-                },
                 onPressed: () {
-                  if (!clickable) return;
-                  setState(() {
-                    selectedIndex = widget.tabs.indexOf(t);
-                    clickable = false;
-                  });
-                  widget.onTabChange(selectedIndex);
-
-                  Future.delayed(
-                      widget.duration ?? const Duration(milliseconds: 500), () {
-                    setState(() {
-                      clickable = true;
-                    });
-                  });
+                  widget.onTabChange(widget.tabs.indexOf(t));
                 },
               ),
             )
@@ -162,7 +143,6 @@ class GButton extends StatefulWidget {
   final TextStyle textStyle;
   final double iconSize;
   final Function onPressed;
-  final Function onDoubleTap;
   final String text;
   final IconData icon;
   final Color backgroundColor;
@@ -198,7 +178,6 @@ class GButton extends StatefulWidget {
     this.iconSize,
     this.leading,
     this.onPressed,
-    this.onDoubleTap,
     this.backgroundGradient,
     this.borderRadius,
     this.border,
@@ -229,11 +208,6 @@ class _GButtonState extends State<GButton> {
           if (widget.haptic) HapticFeedback.selectionClick();
           widget.onPressed();
         },
-        onDoubleTap: () {
-          if (widget.onDoubleTap != null) {
-            widget.onDoubleTap();
-          }
-        },
         padding: widget.padding,
         margin: widget.margin,
         gap: widget.gap,
@@ -265,7 +239,6 @@ class Button extends StatefulWidget {
     this.rippleColor,
     this.hoverColor,
     this.onPressed,
-    this.onDoubleTap,
     this.duration,
     this.curve,
     this.padding = const EdgeInsets.all(25),
@@ -292,7 +265,6 @@ class Button extends StatefulWidget {
   final bool active;
   final bool debug;
   final VoidCallback onPressed;
-  final VoidCallback onDoubleTap;
   final EdgeInsetsGeometry padding;
   final EdgeInsetsGeometry margin;
   final Duration duration;
@@ -353,11 +325,6 @@ class _ButtonState extends State<Button> with TickerProviderStateMixin {
         onTap: () {
           widget.onPressed();
         },
-        onDoubleTap: () {
-          if (widget.onDoubleTap != null) {
-            widget.onDoubleTap();
-          }
-        },
         child: Container(
           padding: widget.margin,
           child: AnimatedContainer(