[Day20] Flutter GetX routing

这篇主要讲GetX在页面切换之间的路由(上下页的前後文关系)
初步先建立一个routes的资料夹 里面主要有三个档案

239ibQ6MuyhcE2ZILgBhnQ

app_pages.dart
主要定义了有哪些页面与关系
FirstPage()里的children涵盖了可能会push到下一页的页面(类似详情页)

class AppPages {
  static const initPage = AppRoutes.FirstPage;
  static final routes = [
    GetPage(
      name: AppRoutes.FirstPage,
      page: () => FirstPage(),
      binding: PagesBind(),
      children: [
        GetPage(
          name: AppRoutes.CarouselSlidePage,
          page: () => CarouselSlidePage(),
          binding: PagesBind(),
        ),
        GetPage(
          name: AppRoutes.LottiePage,
          page: () => LottiePage(),
          binding: PagesBind(),
        ),
      ],
    ),
  ];
}

app_routes.dart
里面定义了页面之间的route name

abstract class AppRoutes {
  static const FirstPage = "/FirstPage";
  static const CarouselSlidePage = '/CarouselSlidePage';
  static const LottiePage = '/LottiePage';
  static const ImagePickerPage = '/ImagePickerPage';
  static const CachedImagePage = '/CachedImagePage';
  static const ToastPage = '/ToastPage';
  static const WrapPage = '/WrapPage';
  static const QRCodePage = '/QRCodePage';
  static const QRCodeScanPage = '/QRCodeScanPage';
  static const BindingPage = '/BindingPage';
  static const ExtensionPage = '/ExtensionPage';
  static const NewsPage = '/NewsPage';
}

PagesBind.dart
里面定义了如果push到哪一页 , 与那一页面绑定的GetXcontroller 
生命周期会被呼叫

class PagesBind extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut<FirstPageController>(() => FirstPageController());
    Get.lazyPut<CarouselSlidePageController>(() => CarouselSlidePageController());
    Get.lazyPut<LottiePageController>(() => LottiePageController());
    Get.lazyPut<ImagePickPageController>(() => ImagePickPageController());
  }
}

实际要去的页面 这样呼叫
Get.toNamed('CarouselSlidePage);
Get.toNamed('ImagePickerPage);
(对应到app_routes.dart定义的名称)

如果要回前一页
Get.back();

效果借用前几篇的Gif
animation

本篇的GitHub source code

下一篇将为大家介绍 GetX with animation
GetX与原生的AnimationController交互


<<:  [Day 26] BDD - 组合技

>>:  Day19 Plugin 从零开始到上架 - 取得授权码(Android)

Day 14 Flask 传入参数

在网页中不可能只是按照设定好的 URL 去取得网页页面,在许多时候都需要带入不同的参数去取得不同的资...

Day1 工业控制系统与普渡模型

工业控制系统 Industrial Control System 简称 ICS = 电脑与工业设备...

Day03【JS】立即呼叫函式 IIFE

IIFE 全称为 Immediately Invoked Function Expression 中...

课堂笔记 - 物联网概论(4)

应用层 1.节能 智慧电网系统 自动将感测到家庭家电上的用电资料并上传到网路 电力公司才可以藉由智...

[Day 29] 永和美食纪录-和陞屋牛排

前言 转眼间食记已经来到了倒数第二篇,不晓得这些日子以来笔者推荐的美食有没有读者去试过了呢?不过美食...