Skip to content

Commit 1d020ed

Browse files
authored
Merge pull request #52 from maheshmnj/fix-51
Migrate to PopScope and Fix back button not showing destination issue
2 parents 85b3cad + c150aa4 commit 1d020ed

File tree

7 files changed

+100
-88
lines changed

7 files changed

+100
-88
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
## [0.7.5] April 30, 2024
1+
## [0.7.5] July 12, 2024
22

3+
- Migrate WillPopScope to PopScope [Issue 48](https://github.com/maheshmnj/navbar_router/pull/48/)
34
- Fix Deprecation [Issue 49](https://github.com/maheshmnj/navbar_router/pull/49/)
45
- Migrate example app to Flutter's Gradle plugins
6+
- Fix: Navbar Destination not updating on backbutton press [Issue 51](https://github.com/maheshmnj/navbar_router/pull/51/)
7+
- docs: Navbar Router should be wrapped in builder for Snackbar to work
58

69
## [0.7.4] April 30, 2024
710

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## **navbar_router v0.7.4**
1+
## **navbar_router v0.7.5**
22

33
<a href="https://github.com/maheshmnj/searchfield" rel="noopener" target="_blank"><img src="https://img.shields.io/badge/platform-flutter-ff69b4.svg" alt="Flutter Platform Badge"></a>
44
<a href="https://pub.dev/packages/navbar_router"><img src="https://img.shields.io/pub/v/navbar_router.svg" alt="Pub"></a>
@@ -189,6 +189,8 @@ You can show a snackbar on top of the navbar by using the `NavbarNotifier.showSn
189189

190190
![snackbar](https://github.com/flutter/flutter/assets/31410839/b2c95c3b-45fa-474c-acee-6f48a051f8ef)
191191

192+
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.
193+
192194
```dart
193195
NavbarNotifier.showSnackBar(
194196
context,

example/lib/main.dart

Lines changed: 72 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -239,74 +239,76 @@ class _HomePageState extends ConsumerState<HomePage> {
239239
}
240240
return const SizedBox.shrink();
241241
}),
242-
body: NavbarRouter(
243-
errorBuilder: (context) {
244-
return const Center(child: Text('Error 404'));
245-
},
246-
isDesktop: size.width > 600 ? true : false,
247-
onBackButtonPressed: (isExitingApp) {
248-
if (isExitingApp) {
249-
newTime = DateTime.now();
250-
int difference = newTime.difference(oldTime).inMilliseconds;
251-
oldTime = newTime;
252-
if (difference < 1000) {
253-
NavbarNotifier.hideSnackBar(context);
254-
return isExitingApp;
242+
body: Builder(builder: (context) {
243+
return NavbarRouter(
244+
errorBuilder: (context) {
245+
return const Center(child: Text('Error 404'));
246+
},
247+
isDesktop: size.width > 600 ? true : false,
248+
onBackButtonPressed: (isExitingApp) {
249+
if (isExitingApp) {
250+
newTime = DateTime.now();
251+
int difference = newTime.difference(oldTime).inMilliseconds;
252+
oldTime = newTime;
253+
if (difference < 1000) {
254+
NavbarNotifier.hideSnackBar(context);
255+
return isExitingApp;
256+
} else {
257+
final state = Scaffold.of(context);
258+
NavbarNotifier.showSnackBar(
259+
context,
260+
"Tap back button again to exit",
261+
bottom: state.hasFloatingActionButton ? 0 : kNavbarHeight,
262+
);
263+
return false;
264+
}
255265
} else {
256-
final state = Scaffold.of(context);
257-
NavbarNotifier.showSnackBar(
258-
context,
259-
"Tap back button again to exit",
260-
bottom: state.hasFloatingActionButton ? 0 : kNavbarHeight,
261-
);
262-
return false;
266+
return isExitingApp;
263267
}
264-
} else {
265-
return isExitingApp;
266-
}
267-
},
268-
initialIndex: 0,
269-
type: NavbarType.floating,
270-
destinationAnimationCurve: Curves.fastOutSlowIn,
271-
destinationAnimationDuration: 200,
272-
decoration: FloatingNavbarDecoration(
273-
height: 80,
274-
// minExtendedWidth: 226,
275-
// minWidth: 92,
276-
borderRadius: BorderRadius.circular(20),
277-
isExtended: size.width > 800 ? true : false,
278-
// labelTextStyle: const TextStyle(
279-
// color: Color.fromARGB(255, 176, 207, 233), fontSize: 14),
280-
// elevation: 3.0,
281-
// indicatorShape: const RoundedRectangleBorder(
282-
// borderRadius: BorderRadius.all(Radius.circular(20)),
283-
// ),
284-
backgroundColor:
285-
Theme.of(context).colorScheme.surfaceContainerHighest,
286-
// indicatorColor: const Color.fromARGB(255, 176, 207, 233),
287-
// // iconTheme: const IconThemeData(color: Colors.indigo),
288-
// /// labelTextStyle: const TextStyle(color: Colors.white, fontSize: 14),
289-
// labelBehavior: NavigationDestinationLabelBehavior.alwaysShow
290-
),
291-
onChanged: (x) {
292-
ref.read(appProvider.notifier).setIndex(x);
293-
},
294-
backButtonBehavior: BackButtonBehavior.rememberHistory,
295-
destinations: [
296-
for (int i = 0; i < items.length; i++)
297-
DestinationRouter(
298-
navbarItem: items[i],
299-
destinations: [
300-
for (int j = 0; j < _routes[i]!.keys.length; j++)
301-
Destination(
302-
route: _routes[i]!.keys.elementAt(j),
303-
widget: _routes[i]!.values.elementAt(j),
304-
),
305-
],
306-
initialRoute: _routes[i]!.keys.first,
307-
),
308-
],
309-
),
268+
},
269+
initialIndex: 0,
270+
type: NavbarType.floating,
271+
destinationAnimationCurve: Curves.fastOutSlowIn,
272+
destinationAnimationDuration: 200,
273+
decoration: FloatingNavbarDecoration(
274+
height: 80,
275+
// minExtendedWidth: 226,
276+
// minWidth: 92,
277+
borderRadius: BorderRadius.circular(20),
278+
isExtended: size.width > 800 ? true : false,
279+
// labelTextStyle: const TextStyle(
280+
// color: Color.fromARGB(255, 176, 207, 233), fontSize: 14),
281+
// elevation: 3.0,
282+
// indicatorShape: const RoundedRectangleBorder(
283+
// borderRadius: BorderRadius.all(Radius.circular(20)),
284+
// ),
285+
backgroundColor:
286+
Theme.of(context).colorScheme.surfaceContainerHighest,
287+
// indicatorColor: const Color.fromARGB(255, 176, 207, 233),
288+
// // iconTheme: const IconThemeData(color: Colors.indigo),
289+
// /// labelTextStyle: const TextStyle(color: Colors.white, fontSize: 14),
290+
// labelBehavior: NavigationDestinationLabelBehavior.alwaysShow
291+
),
292+
onChanged: (x) {
293+
ref.read(appProvider.notifier).setIndex(x);
294+
},
295+
backButtonBehavior: BackButtonBehavior.rememberHistory,
296+
destinations: [
297+
for (int i = 0; i < items.length; i++)
298+
DestinationRouter(
299+
navbarItem: items[i],
300+
destinations: [
301+
for (int j = 0; j < _routes[i]!.keys.length; j++)
302+
Destination(
303+
route: _routes[i]!.keys.elementAt(j),
304+
widget: _routes[i]!.values.elementAt(j),
305+
),
306+
],
307+
initialRoute: _routes[i]!.keys.first,
308+
),
309+
],
310+
);
311+
}),
310312
);
311313
}
312314
}
@@ -416,9 +418,10 @@ class FeedTile extends StatelessWidget {
416418
Container(
417419
padding: const EdgeInsets.symmetric(horizontal: 8.0),
418420
alignment: Alignment.center,
419-
child: Text(
420-
placeHolderText.substring(0, 200),
421-
textAlign: TextAlign.justify,
421+
child: const Text(
422+
placeHolderText,
423+
maxLines: 4,
424+
overflow: TextOverflow.ellipsis,
422425
))
423426
],
424427
),

lib/src/animated_navbar.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ class _AnimatedNavBarState extends State<_AnimatedNavBar>
9494
}
9595

9696
void setUpAnimation() {
97-
double offset = 75.0;
97+
double offset = kNavbarHeight * 2;
9898
if (widget.isDesktop) {
9999
if (widget.decoration!.isExtended) {
100100
offset = widget.decoration!.minExtendedWidth;
101101
} else {
102102
offset = widget.decoration!.minWidth;
103103
}
104+
} else {
105+
offset += (widget.decoration!.margin?.vertical ?? 0) / 2;
104106
}
105107
animation = Tween(begin: 0.0, end: offset).animate(_controller);
106108
}
@@ -312,8 +314,9 @@ class _AnimatedNavBarState extends State<_AnimatedNavBar>
312314
indicatorColor: widget.decoration!.indicatorColor ??
313315
theme.colorScheme.secondaryContainer,
314316
labelBehavior: NavigationDestinationLabelBehavior.alwaysShow,
315-
indicatorShape: widget.decoration!.indicatorShape,
316-
margin: widget.decoration!.margin,
317+
indicatorShape: widget.decoration!.indicatorShape ??
318+
defaultDecoration.indicatorShape,
319+
margin: widget.decoration!.margin ?? defaultDecoration.margin,
317320
showSelectedLabels: widget.decoration?.showSelectedLabels,
318321
selectedLabelTextStyle:
319322
widget.decoration!.selectedLabelTextStyle ??
@@ -339,7 +342,7 @@ class _AnimatedNavBarState extends State<_AnimatedNavBar>
339342
items: widget.menuItems,
340343
onTap: widget.onItemTapped,
341344
borderRadius: widget.decoration!.borderRadius,
342-
margin: widget.decoration!.margin,
345+
margin: widget.decoration!.margin ?? defaultDecoration.margin,
343346
navBarElevation: defaultDecoration.elevation,
344347
);
345348
}
@@ -1018,8 +1021,8 @@ class FloatingNavbarState extends State<FloatingNavbar> {
10181021
backgroundColor: widget.decoration.backgroundColor ??
10191022
Theme.of(context).colorScheme.surface,
10201023
elevation: widget.elevation,
1021-
labelTextStyle: WidgetStateProperty.all(
1022-
widget.decoration.selectedLabelTextStyle),
1024+
labelTextStyle:
1025+
WidgetStateProperty.all(widget.decoration.selectedLabelTextStyle),
10231026
indicatorShape: widget.decoration.indicatorShape ??
10241027
RoundedRectangleBorder(borderRadius: BorderRadius.circular(18.0)),
10251028
iconTheme:

lib/src/navbar_notifier.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:async';
2-
31
import 'package:flutter/material.dart';
42
import 'package:navbar_router/navbar_router.dart';
53

@@ -99,9 +97,8 @@ class NavbarNotifier extends ChangeNotifier {
9997
// pop routes from the nested navigator stack and not the main stack
10098
// this is done based on the currentIndex of the bottom navbar
10199
// if the backButton is pressed on the initial route the app will be terminated
102-
static FutureOr<bool> onBackButtonPressed(
103-
{BackButtonBehavior behavior =
104-
BackButtonBehavior.rememberHistory}) async {
100+
static bool onBackButtonPressed(
101+
{BackButtonBehavior behavior = BackButtonBehavior.rememberHistory}) {
105102
bool exitingApp = true;
106103
NavigatorState? currentState = _keys[_index!].currentState;
107104
if (currentState != null && currentState.canPop()) {

lib/src/navbar_router.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,16 @@ class _NavbarRouterState extends State<NavbarRouter>
338338

339339
@override
340340
Widget build(BuildContext context) {
341-
return WillPopScope(
342-
onWillPop: () async {
343-
final bool isExitingApp = await NavbarNotifier.onBackButtonPressed(
341+
return PopScope(
342+
canPop: false,
343+
onPopInvoked: (bool didPop) async {
344+
if (didPop) {
345+
return;
346+
}
347+
final bool isExitingApp = NavbarNotifier.onBackButtonPressed(
344348
behavior: widget.backButtonBehavior);
345-
final bool value = widget.onBackButtonPressed!(isExitingApp);
346-
return value;
349+
widget.onBackButtonPressed!(isExitingApp);
350+
_handleFadeAnimation();
347351
},
348352
child: AnimatedBuilder(
349353
animation: _navbarNotifier,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: navbar_router
22
description: A flutter package to build advanced bottomnavbar with minimal code and hassle
33

4-
version: 0.7.4
4+
version: 0.7.5
55
homepage: https://github.com/maheshmnj/navbar_router
66
repository: https://github.com/maheshmnj/navbar_router
77
issue_tracker: https://github.com/maheshmnj/navbar_router/issues

0 commit comments

Comments
 (0)