navigation_without_context 0.0.4
navigation_without_context: ^0.0.4 copied to clipboard
A Flutter package that enables global navigation without requiring BuildContext, using a clean abstraction and GetIt for dependency injection.
import 'package:flutter/material.dart';
import 'package:navigation_without_context/navigation_without_context.dart';
void main() {
registerNavigatorGetItDi();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigator.navigatorKey,
title: 'Navigation Example',
initialRoute: '/',
routes: {
'/': (context) => HomePage(),
'/second': (context) => SecondPage(),
},
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home Page')),
body: Center(
child: ElevatedButton(
onPressed: () => navigator.pushNamed('/second'),
child: Text('Go to Second Page'),
),
),
);
}
}
class SecondPage extends StatelessWidget {
const SecondPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Second Page')),
body: Center(
child: ElevatedButton(
onPressed: () => navigator.goBack(),
child: Text('Go Back'),
),
),
);
}
}