When I come back from second screen to home screen, It should be not possiblity to come back to second sreen by back button in home screen.
Actually when I tested it by using only browser’s back button in step 2, It’s working fine.
- Click “Go to the second screen”
- Click “back to the Home screen”
- By clicking browser’s back button I can come back to Second Screen
Tested on go_router: ^14.1.3
When I come back from second screen to home screen, It should be not possiblity to come back to second sreen by back button in home screen.
Actually when I tested it by using only browser’s back button in step 2, It’s working fine.
<code>import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(const MyApp());
}
final GoRouter _router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
name: 'home', // Optional, add name to your routes. Allows you navigate by name instead of path
path: '/',
builder: (context, state) => HomeScreen(),
),
GoRoute(
name: 'second', // Optional, add name to your routes. Allows you navigate by name instead of path
path: '/second',
builder: (context, state) => SecondScreen(),
),
],
);
class MyApp extends StatelessWidget {
/// Constructs a [MyApp]
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home Screen')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/second'),
child: const Text('Go to the second screen'),
),
),
);
}
}
class SecondScreen extends StatelessWidget {
const SecondScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Second Screen')),
body: PopScope(
canPop: false,
child: Column(
children: [
ElevatedButton(
onPressed: () => context.go('/'),
child: const Text('back to the Home screen'),
),
],
),
),
);
}
}
</code>
<code>import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(const MyApp());
}
final GoRouter _router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
name: 'home', // Optional, add name to your routes. Allows you navigate by name instead of path
path: '/',
builder: (context, state) => HomeScreen(),
),
GoRoute(
name: 'second', // Optional, add name to your routes. Allows you navigate by name instead of path
path: '/second',
builder: (context, state) => SecondScreen(),
),
],
);
class MyApp extends StatelessWidget {
/// Constructs a [MyApp]
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home Screen')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/second'),
child: const Text('Go to the second screen'),
),
),
);
}
}
class SecondScreen extends StatelessWidget {
const SecondScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Second Screen')),
body: PopScope(
canPop: false,
child: Column(
children: [
ElevatedButton(
onPressed: () => context.go('/'),
child: const Text('back to the Home screen'),
),
],
),
),
);
}
}
</code>
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(const MyApp());
}
final GoRouter _router = GoRouter(
initialLocation: '/',
routes: [
GoRoute(
name: 'home', // Optional, add name to your routes. Allows you navigate by name instead of path
path: '/',
builder: (context, state) => HomeScreen(),
),
GoRoute(
name: 'second', // Optional, add name to your routes. Allows you navigate by name instead of path
path: '/second',
builder: (context, state) => SecondScreen(),
),
],
);
class MyApp extends StatelessWidget {
/// Constructs a [MyApp]
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: _router,
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home Screen')),
body: Center(
child: ElevatedButton(
onPressed: () => context.go('/second'),
child: const Text('Go to the second screen'),
),
),
);
}
}
class SecondScreen extends StatelessWidget {
const SecondScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Second Screen')),
body: PopScope(
canPop: false,
child: Column(
children: [
ElevatedButton(
onPressed: () => context.go('/'),
child: const Text('back to the Home screen'),
),
],
),
),
);
}
}