|
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), |
|
)), |
|
); |
|
} |
|
} |
|
|