import 'package:flutter/material.dart';
void main() {
runApp(const NavigationApp());
}
class NavigationApp extends StatelessWidget {
const NavigationApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Navigation Assignment',
theme: ThemeData(primarySwatch: Colors.blue),
// Defining routes is necessary for the pushNamed method
initialRoute: '/',
routes: {
'/': (context) => const HomeScreen(),
'/screenTwo': (context) => const ScreenTwo(),
'/screenThree': (context) => const ScreenThree(),
'/screenFour': (context) => const ScreenFour(),
},
);
}
}
// ==========================================
// SCREEN 1: HOME SCREEN
// ==========================================
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home Screen (Screen 1)'), backgroundColor: Colors.blue),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Stack: [Home]', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 20),
// 1. push
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const ScreenTwo()),
);
},
child: const Text('1. Navigator.push() to Screen 2'),
),
// 7. pushNamed
ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/screenTwo');
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.purple, foregroundColor: Colors.white),
child: const Text('7. Navigator.pushNamed() to Screen 2'),
),
// 6. maybePop
ElevatedButton(
onPressed: () async {
// maybePop only pops if there is a previous screen.
// Since this is the root screen, it will return false and do nothing,
// preventing the app from accidentally closing.
bool canPop = await Navigator.maybePop(context);
if (!canPop) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('maybePop: Cannot pop root screen!')),
);
}
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.orange, foregroundColor: Colors.white),
child: const Text('6. Navigator.maybePop() test'),
),
],
),
),
);
}
}
// ==========================================
// SCREEN 2
// ==========================================
class ScreenTwo extends StatelessWidget {
const ScreenTwo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Screen 2'), backgroundColor: Colors.green),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Stack: [Home -> Screen 2]', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 20),
// 2. pop
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.red, foregroundColor: Colors.white),
child: const Text('2. Navigator.pop() back to Home'),
),
// 3. pushReplacement
ElevatedButton(
onPressed: () {
// Replaces Screen 2 with Screen 3 in the stack
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const ScreenThree()),
);
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.teal, foregroundColor: Colors.white),
child: const Text('3. Navigator.pushReplacement() to Screen 3'),
),
// Normal push to continue the stack deeply for later tests
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const ScreenThree()),
);
},
child: const Text('Navigator.push() to Screen 3'),
),
],
),
),
);
}
}
// ==========================================
// SCREEN 3
// ==========================================
class ScreenThree extends StatelessWidget {
const ScreenThree({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Screen 3'), backgroundColor: Colors.orange),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Current Screen: Screen 3', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 20),
// Normal push to Screen 4
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const ScreenFour()),
);
},
child: const Text('Navigator.push() to Screen 4'),
),
// 4. pushAndRemoveUntil
ElevatedButton(
onPressed: () {
// Pushes a brand new Home Screen and destroys all previous routes in the stack
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const HomeScreen()),
(Route<dynamic> route) => false, // false means remove all existing routes
);
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.brown, foregroundColor: Colors.white),
child: const Text('4. Navigator.pushAndRemoveUntil() to Home'),
),
],
),
),
);
}
}
// ==========================================
// SCREEN 4
// ==========================================
class ScreenFour extends StatelessWidget {
const ScreenFour({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Screen 4'), backgroundColor: Colors.purple),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Stack: [Home -> ... -> Screen 4]', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 20),
// 5. popUntil
ElevatedButton(
onPressed: () {
// Keep popping screens until we reach the very first route (Home)
Navigator.popUntil(context, (route) => route.isFirst);
},
style: ElevatedButton.styleFrom(backgroundColor: Colors.indigo, foregroundColor: Colors.white),
child: const Text('5. Navigator.popUntil() back to First Screen'),
),
],
),
),
);
}
}⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.