简体   繁体   English

GetMidleware 在底部导航栏上不起作用 - flutter - GetX

[英]GetMidleware not working on bottomNavigationBar - flutter - GetX

i have problem with GetMidleware , so i have 3 item in bottomNavigationBar :我对GetMidleware有问题,所以我在bottomNavigationBar中有 3 个项目:

  1. Home
  2. MyTicket我的票
  3. Profile轮廓

i want user can acces home without login so i put midleware just on myTicket and Profile , but when i click MyTicket or Profile i still can access that and not redirect to login page.我希望用户无需登录即可访问主页,因此我将中间件仅放在myTicketProfile上,但是当我单击 MyTicket 或 Profile 时,我仍然可以访问它并且不会重定向到登录页面。

this is my midleware class:这是我的中间件 class:

class AuthMiddleware extends GetMiddleware {
  final authService = Get.find<AuthService>();

  @override
  RouteSettings? redirect(String? route) {
    if (!authService.currentUser.value.isAuthenticated) {
      Get.log(authService.currentUser.value.isAuthenticated.toString());
      return const RouteSettings(name: Routes.LOGIN);
    }
    return null;
  }
}

this is my appPage class:这是我的 appPage class:

class AppPages {
   
  static const INITIAL = Routes.ROOT;
   static final routes = [

      GetPage(
      name: _Paths.ROOT,
      page: () => const RootView(),
      binding: RootBinding(),
    ),

      GetPage(
      name: _Paths.HOME,
      page: () => const HomeView(),
      binding: RootBinding(),
    ),
      
      GetPage(
      name: _Paths.MYTICKET,
      page: () => const MyTicketView(),
      binding: RootBinding(),
      middlewares: [AuthMiddleware()],
    ),

     GetPage(
      name: _Paths.PROFILE,
      page: () => const ProfileEditView(),
      binding: RootBinding(),
      middlewares: [AuthMiddleware()],
    ),
   ]
}

this is my root controller:这是我的根 controller:

class RootController extends GetxController {
  final Rx<String> title = Strings.home.obs;
  final Rx<int> currentIndex = 0.obs;
  final authService = Get.find<AuthService>();

  @override
  void onClose() {
    super.dispose();
  }

  List<Widget> pages = [
    const HomeView(),
    const MyTicketView(),
    const ProfileView(),
    // const BlankView(),
  ];

  //get current page for view
  Widget get currentPage => pages[currentIndex.value];

  //change page when bottom nav item is taped
  Future<void> changePage(int _index) async {
    changeTitle(_index);
    if (Get.currentRoute == Routes.ROOT) {
      await changePageInRoot(_index);
    } else {
      await changePageOutRoot(_index);
    }
  }

  Future changeTitle(int index) async {
    switch (index) {
      case 0:
        title.value = Strings.home;
        break;
      case 1:
        title.value = Strings.myTicket;
        break;
      case 2:
        title.value = Strings.myProfile;
        break;
      default:
    }
  }

  //change page if previously page in root
  Future<void> changePageInRoot(int _index) async {
    currentIndex.value = _index;
    await refreshPage(_index);
  }

  //change page if previously page out of root
  Future<void> changePageOutRoot(int _index) async {
    currentIndex.value = _index;
    await refreshPage(_index);
    await Get.offNamedUntil(Routes.ROOT, (Route route) {
      if (route.settings.name == Routes.ROOT) {
        return true;
      }
      return false;
    }, arguments: _index);
  }
}

//call page by index
Future<void> refreshPage(int _index) async {
  switch (_index) {
    case 0:
      {
        Get.find<HomeController>().onInit();
        break;
      }
    case 1:
      {
        Get.find<MyTicketController>().onInit();
        break;
      }
    case 2:
      {
        Get.find<ProfileController>().onInit();
        // Get.find<BlankController>().onInit();
        break;
      }
  }
}

and this is my root view where i put my bottomNavigationBar :这是我将bottomNavigationBar放在其中的根视图:

class RootView extends GetView<RootController> {
  const RootView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Scaffold(
        appBar: PreferredSize(
          preferredSize: const Size.fromHeight(Dimensions.appBarHeight),
          child: BackWidget(title: controller.title.value),
        ),
        backgroundColor: Colors.white,
        bottomNavigationBar: Material(
          elevation: 10,
          // borderRadius: BorderRadius.circular(20),
          child: BottomNavigationBar(
              selectedIconTheme:
                  const IconThemeData(color: CustomColor.primaryColor),
              selectedLabelStyle: const TextStyle(fontSize: 15),
              selectedItemColor: CustomColor.primaryColor,
              backgroundColor: Colors.white,
              elevation: 25,
              type: BottomNavigationBarType.fixed,
              currentIndex: controller.currentIndex.value,
              onTap: (index) => controller.changePage(index),
              items: [
                bottomNavigationBarItemWidget(
                  'assets/svg/home.svg',
                  Strings.home,
                ),
                bottomNavigationBarItemWidget(
                  'assets/svg/document.svg',
                  Strings.myTicket,
                ),
                bottomNavigationBarItemWidget(
                  'assets/svg/profile.svg',
                  Strings.myProfile,
                ),
              ]),
        ),
        body: controller.currentPage,
      ),
    );
  }

  bottomNavigationBarItemWidget(String icon, String label) {
    return BottomNavigationBarItem(
      icon: SvgPicture.asset(
        icon,
        color: Colors.grey,
        height: 24,
        width: 24,
      ),
      activeIcon: SvgPicture.asset(
        icon,
        color: CustomColor.primaryColor,
        height: 24,
        width: 24,
      ),
      label: label,
      tooltip: label,
    );
  }
}

BottomNavigationBar (also TabBar) doesn't change routes. BottomNavigationBar(也是 TabBar)不会改变路线。 It's essentially single page.它本质上是单页。 Therefore route middlewares can't be used with them without complications.因此,路由中间件不能与它们一起使用而不会出现并发症。 Instead, you can use the auth check on the respective controllers of the views of navigation bar items.相反,您可以在导航栏项目视图的各个控制器上使用身份验证检查。 A very good place is in the onReady method of the GetxController s.一个非常好的地方是在GetxControlleronReady方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM