import 'package:flutter/material.dart';
void main() {
runApp(const NavigationAssignmentApp());
}
class NavigationAssignmentApp extends StatelessWidget {
const NavigationAssignmentApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Navigation Methods',
theme: ThemeData(primarySwatch: Colors.blue),
// We set an initial route and named routes to demonstrate 'pushNamed'
initialRoute: '/',
routes: {
'/': (context) => const Screen1(),
'/screen4': (context) => const Screen4(),
},
);
}
}
// --- SCREEN 1 (Home) ---
class Screen1 extends StatelessWidget {
const Screen1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Screen 1 (Root)')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('You are at the start of the stack.'),
const SizedBox(height: 20),
// 1. push
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Screen2()),
);
},
child: const Text('1. Navigator.push() to Screen 2'),
),
// 2. pushNamed
ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/screen4');
},
child: const Text('2. Navigator.pushNamed() to Screen 4'),
),
// 3. maybePop
ElevatedButton(
onPressed: () async {
bool didPop = await Navigator.maybePop(context);
if (!didPop) {
// This will trigger because we are at the root route and can't pop further
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('maybePop: Can\'t pop the root route!')),
);
}
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.grey),
child: const Text('3. Navigator.maybePop()'),
),
],
),
),
);
}
}
// --- SCREEN 2 ---
class Screen2 extends StatelessWidget {
const Screen2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Screen 2')),
backgroundColor: Colors.lightGreen[100],
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 4. pop
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('4. Navigator.pop() (Go Back)'),
),
// 5. pushReplacement
ElevatedButton(
onPressed: () {
// Replaces Screen 2 with Screen 3 in the stack
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const Screen3()),
);
},
child: const Text('5. Navigator.pushReplacement() to Screen 3'),
),
],
),
),
);
}
}
// --- SCREEN 3 ---
class Screen3 extends StatelessWidget {
const Screen3({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Screen 3')),
backgroundColor: Colors.orange[100],
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Notice there is no back button to Screen 2\nbecause we used pushReplacement.'),
const SizedBox(height: 20),
// 6. pushAndRemoveUntil
ElevatedButton(
onPressed: () {
// Pushes Screen 1 and removes all other screens in the stack (like a logout)
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const Screen1()),
(Route<dynamic> route) => false,
);
},
child: const Text('6. pushAndRemoveUntil() (Reset to Screen 1)'),
),
// 7. popUntil
ElevatedButton(
onPressed: () {
// Pops everything on top until it finds the route named '/'
Navigator.popUntil(context, ModalRoute.withName('/'));
},
child: const Text('7. Navigator.popUntil() (Back to Root)'),
),
],
),
),
);
}
}
// --- SCREEN 4 ---
class Screen4 extends StatelessWidget {
const Screen4({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Screen 4 (Named Route)')),
backgroundColor: Colors.purple[100],
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Navigator.pop() to return'),
),
),
);
}
}⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.