From a19c99ba053fb72f23837117f9c23251f359fc4e Mon Sep 17 00:00:00 2001 From: mahesh jamdade Date: Fri, 12 Jul 2024 09:51:33 -0400 Subject: [PATCH] Fix: Issue #48 #51 --- CHANGELOG.md | 2 + README.md | 2 + example/lib/main.dart | 143 ++++++++++++++++++----------------- lib/src/navbar_notifier.dart | 7 +- lib/src/navbar_router.dart | 13 ++-- 5 files changed, 88 insertions(+), 79 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8cfbd8..3b666b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ ## [0.7.5] April 30, 2024 +- Migrate WillPopScope to PopScope [Issue 48](https://github.com/maheshmnj/navbar_router/pull/51/) - Fix Deprecation [Issue 49](https://github.com/maheshmnj/navbar_router/pull/49/) - Migrate example app to Flutter's Gradle plugins +- Fix: Navbar Destination not updating on backbutton press [Issue 51](https://github.com/maheshmnj/navbar_router/pull/51/) ## [0.7.4] April 30, 2024 diff --git a/README.md b/README.md index 57aef51..ce6d909 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,8 @@ You can show a snackbar on top of the navbar by using the `NavbarNotifier.showSn ![snackbar](https://github.com/flutter/flutter/assets/31410839/b2c95c3b-45fa-474c-acee-6f48a051f8ef) +Note: You will need to wrap your NavbarRouter within a builder for this to work see the [example](example/lib/main.dart) for more details. + ```dart NavbarNotifier.showSnackBar( context, diff --git a/example/lib/main.dart b/example/lib/main.dart index 5ca41eb..896bc80 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -239,74 +239,76 @@ class _HomePageState extends ConsumerState { } return const SizedBox.shrink(); }), - body: NavbarRouter( - errorBuilder: (context) { - return const Center(child: Text('Error 404')); - }, - isDesktop: size.width > 600 ? true : false, - onBackButtonPressed: (isExitingApp) { - if (isExitingApp) { - newTime = DateTime.now(); - int difference = newTime.difference(oldTime).inMilliseconds; - oldTime = newTime; - if (difference < 1000) { - NavbarNotifier.hideSnackBar(context); - return isExitingApp; + body: Builder(builder: (context) { + return NavbarRouter( + errorBuilder: (context) { + return const Center(child: Text('Error 404')); + }, + isDesktop: size.width > 600 ? true : false, + onBackButtonPressed: (isExitingApp) { + if (isExitingApp) { + newTime = DateTime.now(); + int difference = newTime.difference(oldTime).inMilliseconds; + oldTime = newTime; + if (difference < 1000) { + NavbarNotifier.hideSnackBar(context); + return isExitingApp; + } else { + final state = Scaffold.of(context); + NavbarNotifier.showSnackBar( + context, + "Tap back button again to exit", + bottom: state.hasFloatingActionButton ? 0 : kNavbarHeight, + ); + return false; + } } else { - final state = Scaffold.of(context); - NavbarNotifier.showSnackBar( - context, - "Tap back button again to exit", - bottom: state.hasFloatingActionButton ? 0 : kNavbarHeight, - ); - return false; + return isExitingApp; } - } else { - return isExitingApp; - } - }, - initialIndex: 0, - type: NavbarType.floating, - destinationAnimationCurve: Curves.fastOutSlowIn, - destinationAnimationDuration: 200, - decoration: FloatingNavbarDecoration( - height: 80, - // minExtendedWidth: 226, - // minWidth: 92, - borderRadius: BorderRadius.circular(20), - isExtended: size.width > 800 ? true : false, - // labelTextStyle: const TextStyle( - // color: Color.fromARGB(255, 176, 207, 233), fontSize: 14), - // elevation: 3.0, - // indicatorShape: const RoundedRectangleBorder( - // borderRadius: BorderRadius.all(Radius.circular(20)), - // ), - backgroundColor: - Theme.of(context).colorScheme.surfaceContainerHighest, - // indicatorColor: const Color.fromARGB(255, 176, 207, 233), - // // iconTheme: const IconThemeData(color: Colors.indigo), - // /// labelTextStyle: const TextStyle(color: Colors.white, fontSize: 14), - // labelBehavior: NavigationDestinationLabelBehavior.alwaysShow - ), - onChanged: (x) { - ref.read(appProvider.notifier).setIndex(x); - }, - backButtonBehavior: BackButtonBehavior.rememberHistory, - destinations: [ - for (int i = 0; i < items.length; i++) - DestinationRouter( - navbarItem: items[i], - destinations: [ - for (int j = 0; j < _routes[i]!.keys.length; j++) - Destination( - route: _routes[i]!.keys.elementAt(j), - widget: _routes[i]!.values.elementAt(j), - ), - ], - initialRoute: _routes[i]!.keys.first, - ), - ], - ), + }, + initialIndex: 0, + type: NavbarType.floating, + destinationAnimationCurve: Curves.fastOutSlowIn, + destinationAnimationDuration: 200, + decoration: FloatingNavbarDecoration( + height: 80, + // minExtendedWidth: 226, + // minWidth: 92, + borderRadius: BorderRadius.circular(20), + isExtended: size.width > 800 ? true : false, + // labelTextStyle: const TextStyle( + // color: Color.fromARGB(255, 176, 207, 233), fontSize: 14), + // elevation: 3.0, + // indicatorShape: const RoundedRectangleBorder( + // borderRadius: BorderRadius.all(Radius.circular(20)), + // ), + backgroundColor: + Theme.of(context).colorScheme.surfaceContainerHighest, + // indicatorColor: const Color.fromARGB(255, 176, 207, 233), + // // iconTheme: const IconThemeData(color: Colors.indigo), + // /// labelTextStyle: const TextStyle(color: Colors.white, fontSize: 14), + // labelBehavior: NavigationDestinationLabelBehavior.alwaysShow + ), + onChanged: (x) { + ref.read(appProvider.notifier).setIndex(x); + }, + backButtonBehavior: BackButtonBehavior.rememberHistory, + destinations: [ + for (int i = 0; i < items.length; i++) + DestinationRouter( + navbarItem: items[i], + destinations: [ + for (int j = 0; j < _routes[i]!.keys.length; j++) + Destination( + route: _routes[i]!.keys.elementAt(j), + widget: _routes[i]!.values.elementAt(j), + ), + ], + initialRoute: _routes[i]!.keys.first, + ), + ], + ); + }), ); } } @@ -416,9 +418,12 @@ class FeedTile extends StatelessWidget { Container( padding: const EdgeInsets.symmetric(horizontal: 8.0), alignment: Alignment.center, - child: Text( - placeHolderText.substring(0, 200), - textAlign: TextAlign.justify, + child: const Flexible( + child: Text( + placeHolderText, + maxLines: 4, + overflow: TextOverflow.ellipsis, + ), )) ], ), diff --git a/lib/src/navbar_notifier.dart b/lib/src/navbar_notifier.dart index 9365f0e..693a553 100644 --- a/lib/src/navbar_notifier.dart +++ b/lib/src/navbar_notifier.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:flutter/material.dart'; import 'package:navbar_router/navbar_router.dart'; @@ -99,9 +97,8 @@ class NavbarNotifier extends ChangeNotifier { // pop routes from the nested navigator stack and not the main stack // this is done based on the currentIndex of the bottom navbar // if the backButton is pressed on the initial route the app will be terminated - static FutureOr onBackButtonPressed( - {BackButtonBehavior behavior = - BackButtonBehavior.rememberHistory}) async { + static bool onBackButtonPressed( + {BackButtonBehavior behavior = BackButtonBehavior.rememberHistory}) { bool exitingApp = true; NavigatorState? currentState = _keys[_index!].currentState; if (currentState != null && currentState.canPop()) { diff --git a/lib/src/navbar_router.dart b/lib/src/navbar_router.dart index 3af314f..774ee78 100644 --- a/lib/src/navbar_router.dart +++ b/lib/src/navbar_router.dart @@ -338,13 +338,16 @@ class _NavbarRouterState extends State @override Widget build(BuildContext context) { - return WillPopScope( - onWillPop: () async { - final bool isExitingApp = await NavbarNotifier.onBackButtonPressed( + return PopScope( + canPop: false, + onPopInvoked: (bool didPop) async { + if (didPop) { + return; + } + final bool isExitingApp = NavbarNotifier.onBackButtonPressed( behavior: widget.backButtonBehavior); - final bool value = widget.onBackButtonPressed!(isExitingApp); + widget.onBackButtonPressed!(isExitingApp); _handleFadeAnimation(); - return value; }, child: AnimatedBuilder( animation: _navbarNotifier,