Skip to content

Instantly share code, notes, and snippets.

@Emex4gman
Created March 17, 2021 16:04
Show Gist options
  • Save Emex4gman/141008a439b70af0552e3bc38887bb16 to your computer and use it in GitHub Desktop.
Save Emex4gman/141008a439b70af0552e3bc38887bb16 to your computer and use it in GitHub Desktop.
A Nice way to Navigate in Flutter without Context class
import 'package:flutter/material.dart';
class NavigationService {
/// Creating the first instance
static final NavigationService _instance = NavigationService._internal();
NavigationService._internal();
/// With this factory setup, any time NavigationService() is called
/// within the appication _instance will be returned and not a new instance
factory NavigationService() => _instance;
///This would allow the app monitor the current screen state during navigation.
///
///This is where the singleton setup we did
///would help as the state is internally maintained
final GlobalKey<NavigatorState> navigationKey = GlobalKey<NavigatorState>();
/// For navigating back to the previous screen
dynamic goBack([dynamic popValue]) {
return navigationKey.currentState.pop(popValue);
}
/// This allows you to naviagte to the next screen by passing the screen widget
Future<dynamic> navigateToScreen(Widget page, {arguments}) async => navigationKey.currentState.push(
MaterialPageRoute(
builder: (_) => page,
),
);
/// This allows you to naviagte to the next screen and
/// also replace the current screen by passing the screen widget
Future<dynamic> replaceScreen(Widget page, {arguments}) async => navigationKey.currentState.pushReplacement(
MaterialPageRoute(
builder: (_) => page,
),
);
/// Allows you to pop to the first screen to when the app first launched.
/// This is useful when you need to log out a user,
/// and also remove all the screens on the navigation stack.
/// I find this very useful
void popToFirst() => navigationKey.currentState.popUntil((route) => route.isFirst);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment