Skip to content

Commit a19c99b

Browse files
committed
Fix: Issue #48 #51
1 parent 1f892d3 commit a19c99b

File tree

5 files changed

+88
-79
lines changed

5 files changed

+88
-79
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## [0.7.5] April 30, 2024
22

3+
- Migrate WillPopScope to PopScope [Issue 48](https://github.com/maheshmnj/navbar_router/pull/51/)
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/)
57

68
## [0.7.4] April 30, 2024
79

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 74 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,12 @@ 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 Flexible(
422+
child: Text(
423+
placeHolderText,
424+
maxLines: 4,
425+
overflow: TextOverflow.ellipsis,
426+
),
422427
))
423428
],
424429
),

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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +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);
349+
widget.onBackButtonPressed!(isExitingApp);
346350
_handleFadeAnimation();
347-
return value;
348351
},
349352
child: AnimatedBuilder(
350353
animation: _navbarNotifier,

0 commit comments

Comments
 (0)