简体   繁体   English

使用 GetX 上的底部导航栏浏览页面时关闭并重新创建 controller Flutter

[英]Close and recreate controller when navigate through pages using bottom navigation bar on GetX Flutter

I have created bottom navigation bar for 4 pages.我为 4 个页面创建了底部导航栏。 Is it possible to make it create the controller only when I tap and close when I navigate to another page?是否可以仅在我导航到另一个页面时点击并关闭时才创建 controller?

In my case, I want it to create A's controller only when I tap A page and then when I go to B page, close A page's controller and create the B another page's controller. If I go back to A page, then recreate the A page's controller again.在我的例子中,我希望它只在我点击 A 页面时创建 A 的 controller 然后当我 go 到 B 页面时,关闭 A 页面的 controller 并创建 B 另一个页面的 controller。如果我 go 回到 A 页面,然后重新创建 A再次访问页面 controller。

Just like using Get.offAllNamed() .就像使用Get.offAllNamed()一样。 The reason I want to do this is to reload the page if the stream data inside the page changes especially to Obx() method used in every page.我想这样做的原因是,如果页面内的 stream 数据发生变化,尤其是每个页面中使用的Obx()方法发生变化,则重新加载页面。

Also reset any form inside a page if some of its field selected, for example like selecting a value from a dropdown menu and navigate to other page.如果选择了某些字段,也可以重置页面内的任何表单,例如从下拉菜单中选择一个值并导航到其他页面。 When navigate back, reset the dropdown back to default.返回时,将下拉菜单重置为默认值。

I put my appbar and bottom nav in separate files and link both files to a pagewrapper.我将我的 appbar 和底部导航放在单独的文件中,并将这两个文件链接到 pagewrapper。

All 4 pages controller were set on pagewrapper bindings like this:所有 4 页 controller 都设置在 pagewrapper 绑定上,如下所示:

class PagewrapperBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut<PagewrapperController>(
      () => PagewrapperController(),
    );
    Get.lazyPut<AppbarController>(
      () => AppbarController(),
    );
    Get.lazyPut<SidenavbarController>(
      () => SidenavbarController(),
    );
    Get.lazyPut<HomeController>(
      () => HomeController(),
    );    
    Get.lazyPut<ApplyiptController>(
      () => ApplyiptController(),
    );
    Get.lazyPut<ResultiptController>(
      () => ResultiptController(),
    );
    Get.lazyPut<ProfileController>(
      () => ProfileController(),
    );
  }
}

PageWrapper controller and view: PageWrapper controller 并查看:

class PagewrapperController extends GetxController {  
   BottomnavbarController bottomnavbarController =
      Get.put(BottomnavbarController());
      
  final bodyRoutes = [
    const HomeView(),
    const ApplyiptView(),
    const ResultiptView(),
    const ProfileView(),
  ];
}

return Scaffold(
  backgroundColor: Colors.white,
  appBar: const PreferredSize(
    preferredSize: Size.fromHeight(50),
    child: AppbarView(),
  ),
  endDrawer: const SidenavbarView(),
  body: Obx(
    () => Padding(
      padding: const EdgeInsets.only(
        left: 10,
        right: 10,
      ),          
      child: Column(
        children: [
          Expanded(
            child: controller
                .bodyRoutes[controller.bottomnavbarController.bottomNavIndex.value],
          ),
        ],
      ),
    ),
  ),
  bottomNavigationBar: const BottomnavbarView(),
);

my bottom nav controller and view:我的底部导航 controller 和视图:

class BottomnavbarController extends GetxController {
  var bottomNavIndex = 0.obs;  
}

return Obx(
  () => BottomNavigationBar(
    currentIndex: controller.bottomNavIndex.value,
    onTap: (index) {
      controller.bottomNavIndex.value = index;
    },
    type: BottomNavigationBarType.fixed,
    backgroundColor: Colors.white,
    showSelectedLabels: false,
    showUnselectedLabels: false,
    selectedItemColor: Colors.black87,
    unselectedItemColor: Colors.black38,
    items: const [
      BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
      BottomNavigationBarItem(icon: Icon(Icons.school), label: "Apply IPT"),
      BottomNavigationBarItem(
          icon: Icon(Icons.assignment_sharp), label: "Result IPT"),
      BottomNavigationBarItem(icon: Icon(Icons.person), label: "Profile"),
    ],
  ),
);

Here is my suggestion to get expected results.这是我获得预期结果的建议。

  1. UseIndexedStack to save the state of your screen使用IndexedStack保存屏幕的 state
  2. Create one common controller on main screen and access the controller on child screen.在主屏创建一个普通的controller,在子屏访问controller。 Use constructor to access that controller.使用构造函数访问该 controller。

class ExampleController extends GetxController{
//your code
List<StatelessWidget> screens(ExampleController controller){
    return [AScreen(
    controller: controller,
  ),
  BScreen(
    controller: controller,
  ),
  CScreen(
    controller: controller,
  ),
  DScreen(
    controller: controller,
  ),];
}}

Please check this link.请检查此链接。 I have written a demo for you.我已经为你写了一个演示。 https://pastebin.com/ZassgnJQ https://pastebin.com/ZassgnJQ

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

相关问题 如何在 flutter 底部导航栏的一部分中导航多个页面 - How to navigate through multiple pages in one part of the bottom navigation bar in flutter 带有底部导航栏的 Flutter GetX 不更新小部件 - Flutter GetX with bottom navigation bar not update the widget 如何使用 flutter 中的底部导航栏导航到几个新页面? - How can i navigate to several new pages using the bottom navigation bar in flutter? 如何使用 Animation 底部导航栏导航 flutter - How to navigate using Animation Bottom Navigation Bar flutter 单击底部导航栏时关闭 Flutter 抽屉 - Close Flutter drawer when bottom navigation bar is click 使用底部导航栏浏览Web视图 - Navigate through web view with bottom navigation bar Flutter 使用底部导航栏切换页面时计时器重置 - Flutter timer resets when switching pages with bottom navigation bar Flutter:我们可以使用 GetX 导航到 controller 内的其他屏幕吗? - Flutter: Can we navigate to other screen inside controller using GetX? 当我通过单击脚手架内的平面按钮导航到其他页面时,底部导航栏消失 - Bottom Navigation Bar disappears when I navigate to other pages by clicking on Flat Button inside Scaffold 如何使用底部导航栏导航到新页面? - How do i navigate to new pages using my bottom-navigation-bar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM