-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnavigation_service.dart
75 lines (60 loc) · 2.47 KB
/
navigation_service.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Flutter imports:
import 'package:flutter/material.dart';
// Project imports:
import 'package:notredame/features/app/analytics/analytics_service.dart';
import 'package:notredame/features/app/analytics/remote_config_service.dart';
import 'package:notredame/features/app/navigation/router_paths.dart';
import 'package:notredame/utils/locator.dart';
//SERVICE
//CONSTANT
//OTHERS
/// Navigation service who doesn't use the BuildContext which allow us to call it from anywhere.
class NavigationService {
static const String tag = "NavigationService";
final RemoteConfigService remoteConfigService =
locator<RemoteConfigService>();
final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
/// Will be used to report event and error.
final AnalyticsService _analyticsService = locator<AnalyticsService>();
GlobalKey<NavigatorState> get navigatorKey => _navigatorKey;
/// Pop the last route of the navigator if possible
bool pop() {
final currentState = _navigatorKey.currentState;
if (currentState != null && currentState.canPop()) {
currentState.pop();
return true;
}
return false;
}
/// Push a named route ([routeName] onto the navigator.
Future<dynamic> pushNamed(String routeName, {dynamic arguments}) {
final currentState = _navigatorKey.currentState;
if (currentState == null) {
_analyticsService.logError(tag, "Navigator state is null");
return Future.error("Navigator state is null");
}
if (remoteConfigService.outage) {
return currentState.pushNamedAndRemoveUntil(
RouterPaths.serviceOutage, (route) => false);
}
return currentState.pushNamed(routeName, arguments: arguments);
}
/// Replace the current route of the navigator by pushing the route named
/// [routeName] and then delete the stack of previous routes
Future<dynamic> pushNamedAndRemoveUntil(String routeName,
[String removeUntilRouteNamed = RouterPaths.dashboard,
Object? arguments]) {
final currentState = _navigatorKey.currentState;
if (currentState == null) {
_analyticsService.logError(tag, "Navigator state is null");
return Future.error("Navigator state is null");
}
if (remoteConfigService.outage) {
return currentState.pushNamedAndRemoveUntil(
RouterPaths.serviceOutage, (route) => false);
}
return currentState.pushNamedAndRemoveUntil(
routeName, ModalRoute.withName(removeUntilRouteNamed),
arguments: arguments);
}
}