-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.dart
27 lines (19 loc) · 924 Bytes
/
router.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
import 'package:flutter/material.dart';
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
Route<dynamic>? onGenerateRoute(RouteSettings settings) => null;
class MagicRouter {
static BuildContext? currentContext = navigatorKey.currentContext;
static Future<dynamic> navigateTo(Widget page) =>
navigatorKey.currentState!.push(_materialPageRoute(page));
static Future<dynamic> navigateAndPopAll(Widget page) =>
navigatorKey.currentState!.pushAndRemoveUntil(
_materialPageRoute(page),
(_) => false,
);
static Future<dynamic> navigateAndPopUntilFirstPage(Widget page) =>
navigatorKey.currentState!.pushAndRemoveUntil(
_materialPageRoute(page), (route) => route.isFirst);
static void pop() => navigatorKey.currentState!.pop();
static Route<dynamic> _materialPageRoute(Widget page) =>
MaterialPageRoute(builder: (_) => page);
}