Skip to content

Commit

Permalink
Fix: Issue #48 #51
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshj01 committed Jul 12, 2024
1 parent 1f892d3 commit a19c99b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 79 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
143 changes: 74 additions & 69 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,74 +239,76 @@ class _HomePageState extends ConsumerState<HomePage> {
}
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,
),
],
);
}),
);
}
}
Expand Down Expand Up @@ -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,
),
))
],
),
Expand Down
7 changes: 2 additions & 5 deletions lib/src/navbar_notifier.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:navbar_router/navbar_router.dart';

Expand Down Expand Up @@ -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<bool> 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()) {
Expand Down
13 changes: 8 additions & 5 deletions lib/src/navbar_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,16 @@ class _NavbarRouterState extends State<NavbarRouter>

@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,
Expand Down

0 comments on commit a19c99b

Please sign in to comment.