1 路由
Get.to(NextScreen()); // 导航到新页面Get.toNamed('/details'); // 用别名导航到新页面Get.back(); // 要关闭snackbars, dialogs, bottomsheetsGet.off(NextScreen()); // 进入下一个页面,但没有返回上一个页面的选项(用于闪屏页,登录页面等)。Get.offAll(NextScreen()); // 进入下一个页面并取消之前的所有路由。
(1) 设置初始页, 注册路由
Future<void> main() async {runApp(MyApp());}class MyApp extends StatelessWidget {const MyApp({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return GetMaterialApp(debugShowCheckedModeBanner: false,initialRoute: AppPages.INITIAL,getPages: AppPages.routes,);}}
import 'package:fltest/pages/home/index.dart';import 'package:fltest/pages/list/index.dart';import 'package:fltest/pages/list_detail/index.dart';import 'package:get/get.dart';abstract class AppRoutes {static const Home = '/home';static const List = '/list';static const Detail = '/detail';}class AppPages {static const INITIAL = AppRoutes.Home;static final routes = [GetPage(name: AppRoutes.Home, page: () => HomeView(), children: [GetPage(name: AppRoutes.List, page: () => MyListView(), children: [GetPage(name: AppRoutes.Detail, page: () => DetailView()),]),]),];}
(2) 各页面
import 'package:fltest/pages/list_detail/index.dart';import 'package:flutter/material.dart';import 'package:get/get.dart';class HomeView extends StatelessWidget {const HomeView({ Key? key }) : super(key: key);@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("首页"),),body: ListView(children: [// 路由&导航ListTile(title: Text("导航-命名路由 home > list"),subtitle: Text('Get.toNamed("/home/list")'),onTap: () => Get.toNamed("/home/list"),),ListTile(title: Text("导航-命名路由 home > list > detail"),subtitle: Text('Get.toNamed("/home/list/detail")'),onTap: () => Get.toNamed("/home/list/detail"),),ListTile(title: Text("导航-类对象"),subtitle: Text('Get.to(DetailView())'),onTap: () => Get.to(DetailView()),),Divider(),],),);}}
import 'package:fltest/pages/list_detail/index.dart';import 'package:flutter/material.dart';import 'package:get/get.dart';class MyListView extends StatelessWidget {const MyListView({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("列表页"),),body: ListView(children: [ListTile(title: Text("点击详情"),onTap: () => Get.to(DetailView()),),],));}}
class DetailView extends StatelessWidget {const DetailView({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("详情页"),),body: ListView(children: [ListTile(title: Text("导航-返回"),subtitle: Text('Get.back()'),onTap: () => Get.back(),),],),);}}
2 传值+返回值
除了下面这种, 也支持查询字符串的形式传参, 不过接收值使用Get.parameters
(1) 传值
ListTile(title: Text("导航-arguments传值+返回值"),onTap: () async {var result = await Get.toNamed("/home/list/detail",arguments: {"id": 999},);Get.snackbar("返回值", "success -> " + result["success"].toString());},)
(2) 返回值
class DetailView extends StatelessWidget {const DetailView({Key? key}) : super(key: key);buildListViewVar(dynamic arg) {return arg == null? Container(): ListTile(title: Text("$arg"),onTap: () => Get.back(result: {"success": true}),);}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("详情页"),),body: ListView(children: [ListTile(title: Text("导航-返回"),subtitle: Text("Get.back()"),onTap: () => Get.back(),),buildListViewVar(Get.arguments)],));}}
3 404页面
abstract class AppRoutes {static const NotFound = '/notfound';}class AppPages {static const INITIAL = AppRoutes.Home;static final routes = [];static final unknownRoute =GetPage(name: AppRoutes.NotFound, page: () => NotFoundView());}
class NotFoundView extends StatelessWidget {const NotFoundView({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("路由没有找到")),body: ListTile(title: Text("返回首页"),onTap: () => Get.offAllNamed(AppRoutes.Home),));}}
class MyApp extends StatelessWidget {const MyApp({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return GetMaterialApp(debugShowCheckedModeBanner: false,initialRoute: AppPages.INITIAL,getPages: AppPages.routes,unknownRoute: AppPages.unknownRoute,);}}
4 中间件
class LoginView extends StatelessWidget {const LoginView({ Key? key }) : super(key: key);@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("登录"),),body: ListTile(title: Text("返回首页"),onTap: () => Get.offAllNamed(AppRoutes.Home),),);}}
class MyView extends StatelessWidget {const MyView({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("我的"),),body: ListTile(title: Text("返回首页"),onTap: () => Get.offAllNamed(AppRoutes.Home),),);}}
class RouteAuthMiddleware extends GetMiddleware {@overrideRouteSettings? redirect(String? route) {Future.delayed(Duration(seconds: 1), () => Get.snackbar("提示", "请先登录APP"));return RouteSettings(name: AppRoutes.Login);}}
GetPage(name: AppRoutes.Login, page: () => LoginView()),GetPage(name: AppRoutes.My,page: () => MyView(),middlewares: [RouteAuthMiddleware()])
ListTile(title: Text("导航-中间件Auth"),onTap: () => Get.toNamed("/my"),)
5 转场动画
GetPage(name: AppRoutes.Login,page: () => LoginView(),transition: Transition.downToUp),
