import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Simple Nav Demo',
home: const ScreenOne(),
);
}
}
class ScreenOne extends StatelessWidget {
const ScreenOne({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Simple Nav Demo')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const ScreenTwo()),
);
},
child: const Text('Go to Screen 2'),
),
),
);
}
}
class ScreenTwo extends StatelessWidget {
const ScreenTwo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Screen Two')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Go Back'),
),
),
);
}
}
We created two screens: ScreenOne and ScreenTwo. On ScreenOne, the button uses Navigator.push to open ScreenTwo. This means the app moves forward to the new screen.
On ScreenTwo, the button uses Navigator.pop to go back to the previous screen, which is ScreenOne. This is like pressing the back button.
This simple navigation shows how to move between screens in Flutter using the Navigator stack.