1 路由

  1. Get.to(NextScreen()); // 导航到新页面
  2. Get.toNamed('/details'); // 用别名导航到新页面
  3. Get.back(); // 要关闭snackbars, dialogs, bottomsheets
  4. Get.off(NextScreen()); // 进入下一个页面,但没有返回上一个页面的选项(用于闪屏页,登录页面等)。
  5. Get.offAll(NextScreen()); // 进入下一个页面并取消之前的所有路由。

(1) 设置初始页, 注册路由

  1. Future<void> main() async {
  2. runApp(MyApp());
  3. }
  4. class MyApp extends StatelessWidget {
  5. const MyApp({Key? key}) : super(key: key);
  6. @override
  7. Widget build(BuildContext context) {
  8. return GetMaterialApp(
  9. debugShowCheckedModeBanner: false,
  10. initialRoute: AppPages.INITIAL,
  11. getPages: AppPages.routes,
  12. );
  13. }
  14. }
  1. import 'package:fltest/pages/home/index.dart';
  2. import 'package:fltest/pages/list/index.dart';
  3. import 'package:fltest/pages/list_detail/index.dart';
  4. import 'package:get/get.dart';
  5. abstract class AppRoutes {
  6. static const Home = '/home';
  7. static const List = '/list';
  8. static const Detail = '/detail';
  9. }
  10. class AppPages {
  11. static const INITIAL = AppRoutes.Home;
  12. static final routes = [
  13. GetPage(name: AppRoutes.Home, page: () => HomeView(), children: [
  14. GetPage(name: AppRoutes.List, page: () => MyListView(), children: [
  15. GetPage(name: AppRoutes.Detail, page: () => DetailView()),
  16. ]),
  17. ]),
  18. ];
  19. }

(2) 各页面

  1. import 'package:fltest/pages/list_detail/index.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. class HomeView extends StatelessWidget {
  5. const HomeView({ Key? key }) : super(key: key);
  6. @override
  7. Widget build(BuildContext context) {
  8. return Scaffold(
  9. appBar: AppBar(
  10. title: Text("首页"),
  11. ),
  12. body: ListView(
  13. children: [
  14. // 路由&导航
  15. ListTile(
  16. title: Text("导航-命名路由 home > list"),
  17. subtitle: Text('Get.toNamed("/home/list")'),
  18. onTap: () => Get.toNamed("/home/list"),
  19. ),
  20. ListTile(
  21. title: Text("导航-命名路由 home > list > detail"),
  22. subtitle: Text('Get.toNamed("/home/list/detail")'),
  23. onTap: () => Get.toNamed("/home/list/detail"),
  24. ),
  25. ListTile(
  26. title: Text("导航-类对象"),
  27. subtitle: Text('Get.to(DetailView())'),
  28. onTap: () => Get.to(DetailView()),
  29. ),
  30. Divider(),
  31. ],
  32. ),
  33. );
  34. }
  35. }
  1. import 'package:fltest/pages/list_detail/index.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. class MyListView extends StatelessWidget {
  5. const MyListView({Key? key}) : super(key: key);
  6. @override
  7. Widget build(BuildContext context) {
  8. return Scaffold(
  9. appBar: AppBar(
  10. title: Text("列表页"),
  11. ),
  12. body: ListView(
  13. children: [
  14. ListTile(
  15. title: Text("点击详情"),
  16. onTap: () => Get.to(DetailView()),
  17. ),
  18. ],
  19. ));
  20. }
  21. }
  1. class DetailView extends StatelessWidget {
  2. const DetailView({Key? key}) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: Text("详情页"),
  8. ),
  9. body: ListView(
  10. children: [
  11. ListTile(
  12. title: Text("导航-返回"),
  13. subtitle: Text('Get.back()'),
  14. onTap: () => Get.back(),
  15. ),
  16. ],
  17. ),
  18. );
  19. }
  20. }

2 传值+返回值

除了下面这种, 也支持查询字符串的形式传参, 不过接收值使用Get.parameters

(1) 传值

  1. ListTile(
  2. title: Text("导航-arguments传值+返回值"),
  3. onTap: () async {
  4. var result = await Get.toNamed(
  5. "/home/list/detail",
  6. arguments: {"id": 999},
  7. );
  8. Get.snackbar("返回值", "success -> " + result["success"].toString());
  9. },
  10. )

(2) 返回值

  1. class DetailView extends StatelessWidget {
  2. const DetailView({Key? key}) : super(key: key);
  3. buildListViewVar(dynamic arg) {
  4. return arg == null
  5. ? Container()
  6. : ListTile(
  7. title: Text("$arg"),
  8. onTap: () => Get.back(result: {"success": true}),
  9. );
  10. }
  11. @override
  12. Widget build(BuildContext context) {
  13. return Scaffold(
  14. appBar: AppBar(
  15. title: Text("详情页"),
  16. ),
  17. body: ListView(
  18. children: [
  19. ListTile(
  20. title: Text("导航-返回"),
  21. subtitle: Text("Get.back()"),
  22. onTap: () => Get.back(),
  23. ),
  24. buildListViewVar(Get.arguments)
  25. ],
  26. ));
  27. }
  28. }

3 404页面

  1. abstract class AppRoutes {
  2. static const NotFound = '/notfound';
  3. }
  4. class AppPages {
  5. static const INITIAL = AppRoutes.Home;
  6. static final routes = [
  7. ];
  8. static final unknownRoute =
  9. GetPage(name: AppRoutes.NotFound, page: () => NotFoundView());
  10. }
  1. class NotFoundView extends StatelessWidget {
  2. const NotFoundView({Key? key}) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(title: Text("路由没有找到")),
  7. body: ListTile(
  8. title: Text("返回首页"),
  9. onTap: () => Get.offAllNamed(AppRoutes.Home),
  10. ));
  11. }
  12. }
  1. class MyApp extends StatelessWidget {
  2. const MyApp({Key? key}) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return GetMaterialApp(
  6. debugShowCheckedModeBanner: false,
  7. initialRoute: AppPages.INITIAL,
  8. getPages: AppPages.routes,
  9. unknownRoute: AppPages.unknownRoute,
  10. );
  11. }
  12. }

4 中间件

  1. class LoginView extends StatelessWidget {
  2. const LoginView({ Key? key }) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: Text("登录"),
  8. ),
  9. body: ListTile(
  10. title: Text("返回首页"),
  11. onTap: () => Get.offAllNamed(AppRoutes.Home),
  12. ),
  13. );
  14. }
  15. }
  1. class MyView extends StatelessWidget {
  2. const MyView({Key? key}) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: Text("我的"),
  8. ),
  9. body: ListTile(
  10. title: Text("返回首页"),
  11. onTap: () => Get.offAllNamed(AppRoutes.Home),
  12. ),
  13. );
  14. }
  15. }
  1. class RouteAuthMiddleware extends GetMiddleware {
  2. @override
  3. RouteSettings? redirect(String? route) {
  4. Future.delayed(Duration(seconds: 1), () => Get.snackbar("提示", "请先登录APP"));
  5. return RouteSettings(name: AppRoutes.Login);
  6. }
  7. }
  1. GetPage(name: AppRoutes.Login, page: () => LoginView()),
  2. GetPage(
  3. name: AppRoutes.My,
  4. page: () => MyView(),
  5. middlewares: [RouteAuthMiddleware()])
  1. ListTile(
  2. title: Text("导航-中间件Auth"),
  3. onTap: () => Get.toNamed("/my"),
  4. )

5 转场动画

  1. GetPage(
  2. name: AppRoutes.Login,
  3. page: () => LoginView(),
  4. transition: Transition.downToUp),