https://pub.flutter-io.cn/packages/get
vscode 插件
Android Studio/Intellij 插件
https://www.bilibili.com/video/BV1yU4y1876r/
https://github.com/ducafecat/getx_quick_start
dependencies:
...
get: ^3.26.0
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class HomeView extends StatelessWidget {
const HomeView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("首页"),
),
body: ListView(
children: [
// 路由&导航
Divider(),
],
),
);
}
}
part of 'app_pages.dart';
abstract class AppRoutes {
static const Home = '/home';
}
import 'package:get/get.dart';
part 'app_routes.dart';
class AppPages {
static const INITIAL = AppRoutes.Home;
static final routes = [
GetPage(
name: AppRoutes.Home,
page: () => HomeView(),
),
];
}
Future main() async {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
initialRoute: AppPages.INITIAL,
getPages: AppPages.routes,
);
}
}
class ListView extends StatelessWidget {
const ListView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("列表页"),
),
);
}
}
class DetailView extends StatelessWidget {
const DetailView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("详情页"),
),
body: ListView(
children: [
ListTile(
title: Text("导航-返回"),
subtitle: Text('Get.back()'),
onTap: () => Get.back(),
),
],
),
);
}
}
abstract class AppRoutes {
static const Home = '/home';
static const List = '/list';
static const Detail = '/detail';
GetPage(
name: AppRoutes.Home,
page: () => HomeView(),
children: [
GetPage(
name: AppRoutes.List,
page: () => ListView(),
children: [
GetPage(
name: AppRoutes.Detail,
page: () => DetailView(),
),
],
),
],
),
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()),
),
ListTile(
title: Text("导航-清除上一个"),
subtitle: Text("Get.off(DetailView())"),
onTap: () => Get.off(DetailView()),
),
ListTile(
title: Text("导航-清除所有"),
subtitle: Text("Get.offAll(DetailView())"),
onTap: () => Get.offAll(DetailView()),
),
ListTile(
title: Text("导航-arguments传值+返回值"),
subtitle: Text(
'Get.toNamed("/home/list/detail", arguments: {"id": 999})'),
onTap: () async {
var result = await Get.toNamed("/home/list/detail",
arguments: {"id": 999});
Get.snackbar("返回值", "success -> " + result["success"].toString());
},
),
_buildBackListTileRow(Map? val) {
return val == null
? Container()
: ListTile(
title: Text("传值 id = " + val["id"].toString()),
subtitle: Text('Get.back(result: {"success": true}'),
onTap: () => Get.back(result: {"success": true}),
);
}
@override
Widget build(BuildContext context) {
final details = Get.arguments as Map;
final parameters = Get.parameters;
return Scaffold(
appBar: AppBar(
title: Text("详情页"),
),
body: ListView(
children: [
ListTile(
title: Text("导航-返回"),
subtitle: Text('Get.back()'),
onTap: () => Get.back(),
),
_buildBackListTileRow(details),
_buildBackListTileRow(parameters),
],
),
);
}
ListTile(
title: Text("导航-parameters传值+返回值"),
subtitle: Text('Get.toNamed("/home/list/detail?id=666")'),
onTap: () async {
var result = await Get.toNamed("/home/list/detail?id=666");
Get.snackbar("返回值", "success -> " + result["success"].toString());
},
),
@override
Widget build(BuildContext context) {
final parameters = Get.parameters;
static const Detail_ID = '/detail/:id';
...
GetPage(
name: AppRoutes.Detail_ID,
page: () => DetailView(),
),
ListTile(
title: Text("导航-参数传值+返回值"),
subtitle: Text('Get.toNamed("/home/list/detail/777")'),
onTap: () async {
var result = await Get.toNamed("/home/list/detail/777");
Get.snackbar("返回值", "success -> " + result["success"].toString());
},
),
class NotfoundView extends StatelessWidget {
const NotfoundView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("路由没有找到"),
),
body: ListTile(
title: Text("返回首页"),
subtitle: Text('Get.offAllNamed(AppRoutes.Home)'),
onTap: () => Get.offAllNamed(AppRoutes.Home),
),
);
}
}
static const NotFound = '/notfound';
static final unknownRoute = GetPage(
name: AppRoutes.NotFound,
page: () => NotfoundView(),
);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
...
unknownRoute: AppPages.unknownRoute,
);
}
class LoginView extends StatelessWidget {
const LoginView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("登录"),
),
body: ListTile(
title: Text("返回首页"),
subtitle: Text('Get.offAllNamed(AppRoutes.Home)'),
onTap: () => Get.offAllNamed(AppRoutes.Home),
),
);
}
}
class MyView extends StatelessWidget {
const MyView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("我的"),
),
body: ListTile(
title: Text("返回首页"),
subtitle: Text('Get.offAllNamed(AppRoutes.Home)'),
onTap: () => Get.offAllNamed(AppRoutes.Home),
),
);
}
}
static const Login = '/login';
static const My = '/my';
class RouteAuthMiddleware extends GetMiddleware {
@override
int priority = 0;
RouteAuthMiddleware({required this.priority});
@override
RouteSettings? 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(priority: 1),
],
),
ListTile(
title: Text("导航-中间件-认证Auth"),
subtitle: Text('Get.toNamed(AppRoutes.My)'),
onTap: () => Get.toNamed(AppRoutes.My),
),
GetPage(
name: AppRoutes.Detail_ID,
page: () => DetailView(),
transition: Transition.downToUp,
),
© 猫哥
https://ducafecat.tech