Skip to content

Instantly share code, notes, and snippets.

@BarryDaBee
Last active April 5, 2022 08:24
Show Gist options
  • Select an option

  • Save BarryDaBee/784727f1c64535e3a5f877f622f1d01b to your computer and use it in GitHub Desktop.

Select an option

Save BarryDaBee/784727f1c64535e3a5f877f622f1d01b to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class NavigatorService{
///The next three lines makes this class a singleton
///You may also use a locator to register this class as a singleton
NavigatorService._internal();
static final NavigatorService _instance = NavigatorService._internal();
factory NavigatorService() => _instance;
final GlobalKey<NavigatorState> _navKey = GlobalKey<NavigatorState>();
GlobalKey<NavigatorState> get key => _navKey;
pushNamed(String routeName, {dynamic arguments}) {
return _navKey.currentState!.pushNamed(routeName, arguments: arguments);
}
pop(value) {
return _navKey.currentState!.pop(value);
}
goBack() {
return _navKey.currentState!.pop();
}
popUntil(String routeName, {dynamic arguments}) {
return _navKey.currentState!.popUntil(ModalRoute.withName(routeName));
}
pushNamedAndRemoveUntil(String routeName, {dynamic arguments}) {
return _navKey.currentState!.pushNamedAndRemoveUntil(routeName, (route) => false, arguments: arguments);
}
pushReplacementNamed(String routeName, {dynamic arguments}) {
return _navKey.currentState!.pushReplacementNamed(routeName, arguments: arguments);
}
BuildContext get context => _navKey.currentState!.context;
}
class DialogService{
///The next three lines makes this class a singleton
///You may also use a locator to register this class as a singleton
DialogService._internal();
static final DialogService _instance = DialogService._internal();
factory DialogService() => _instance;
final NavigatorService _navigatorService = NavigatorService();
///You can also use a dialog here
showSuccessDialog(String message){
ScaffoldMessenger.of(_navigatorService.context).showSnackBar(
SnackBar(
duration: const Duration(milliseconds: 1000),
backgroundColor: AppColors.green,
content: Text(message,
style: Theme.of(_navigatorService.context).textTheme.headline3!.copyWith(color: Colors.white),
)),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment