-
-
Notifications
You must be signed in to change notification settings - Fork 463
Fix popping when there is no entries in nested router #2086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
@ekrem-qb not sure if I understand the setup you're referring to but I tried and it works fine the way it as? |
|
I use Nested Navigation Example Code
import 'package:auto_route/auto_route.dart';
import 'package:example/nested-navigation/nested_navigation.router.gr.dart';
import 'package:flutter/material.dart';
void main() {
runApp(NestedNavigationApp());
}
class NestedNavigationApp extends StatelessWidget {
NestedNavigationApp({super.key});
final nestedRouter = NestedRouter();
@override
Widget build(BuildContext context) {
return MaterialApp.router(
darkTheme: ThemeData.dark(),
routerConfig: nestedRouter.config(),
);
}
}
@AutoRouterConfig(generateForDir: ['lib/nested-navigation'])
class NestedRouter extends RootStackRouter {
@override
List<AutoRoute> get routes => [
AutoRoute(
initial: true,
page: HostRoute.page,
children: [
AutoRoute(page: FirstRoute.page, initial: true),
AutoRoute(page: SecondRoute.page, children: [
AutoRoute(page: FirstSubRoute.page),
AutoRoute(page: SecondSubRoute.page),
AutoRoute(page: ThirdSubRoute.page),
]),
],
),
];
}
@RoutePage()
class HostScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Host Screen'),
/// This will automatically display a back button if the nested router can pop
leading: AutoLeadingButton(),
),
body: AutoRouter(),
);
}
}
@RoutePage()
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => context.pushRoute(SecondRoute()),
child: Text('Go to second screen'),
),
),
);
}
}
@RoutePage()
class FirstSubScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return PopScope(
canPop: false,
onPopInvokedWithResult: (final didPop, final result) {
if (!didPop && context.mounted) {
context.router.removeLast();
}
},
child: Scaffold(
appBar: AppBar(
leading: AutoLeadingButton(),
),
body: Center(
child: Text('1'),
),
),
);
}
}
@RoutePage()
class SecondSubScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return PopScope(
canPop: false,
onPopInvokedWithResult: (final didPop, final result) {
if (!didPop && context.mounted) {
context.router.removeLast();
}
},
child: Scaffold(
appBar: AppBar(
leading: AutoLeadingButton(),
),
body: Center(
child: Text('2'),
),
),
);
}
}
@RoutePage()
class ThirdSubScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return PopScope(
canPop: false,
onPopInvokedWithResult: (final didPop, final result) {
if (!didPop && context.mounted) {
context.router.removeLast();
}
},
child: Scaffold(
appBar: AppBar(
leading: AutoLeadingButton(),
),
body: Center(
child: Text('3'),
),
),
);
}
}
@RoutePage()
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Row(
children: [
Flexible(
flex: 1,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Card(
child: ListTile(
title: Text('Open 1'),
onTap: () => context.pushRoute(FirstSubRoute()),
),
),
Card(
child: ListTile(
title: Text('Open 2'),
onTap: () => context.pushRoute(SecondSubRoute()),
),
),
Card(
child: ListTile(
title: Text('Open 3'),
onTap: () => context.pushRoute(ThirdSubRoute()),
),
),
],
),
),
),
Flexible(
flex: 2,
child: Card.outlined(
child: AutoRouter(
placeholder: (context) {
return Center(
child: Text('No route selected'),
);
},
),
),
),
],
),
);
}
}
Nested Navigation Example Video
screen-20250303-041409.2.mp4The declarative dialog navigation example is a bit longer, but I can send it if needed. |
|
What do the introduced changes actually imply? |
|
@mrverdant13 The problem is that it immediately returns false and doesn't try to pop the parent navigator when child navigator is null.
|
Makes sense. Apologies for the confusion here.
Yeah, sorry about that. English is not my native language.
Sorry about that too. I totally messed up 🤦🏼♂️. I was trying to react with 👀 instead of 👎🏼 |
App closes when nested router has no entries, according to code and logic it should pop in parent router but it doesn't. I fixed that behavior.