import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
final navigatorKey = GlobalKey<NavigatorState>();
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Navigation without Context',
navigatorKey: navigatorKey,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Screen'),
),
body: Center(
child: ElevatedButton(
child: Text('Open Details Screen'),
onPressed: () {
navigatorKey.currentState!.push(
MaterialPageRoute(
builder: (context) => DetailsScreen(),
),
);
},
),
),
);
}
}
class DetailsScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Details Screen'),
),
body: Center(
child: ElevatedButton(
child: Text('Go Back'),
onPressed: () {
navigatorKey.currentState!.pop();
},
),
),
);
}
}