简体   繁体   English

在flutter中如何使底部导航栏在访问前不加载屏幕

[英]How to make bottom navigation bar not to load the screen before visiting in flutter

I am using Getx State Management in my project.我在我的项目中使用 Getx State 管理。 My app is giving bad performance as too many controllers get loaded at the same time if I use bottom navigation bar in flutter.如果我在 flutter 中使用底部导航栏,我的应用程序性能不佳,因为同时加载了太多控制器。

I want to load only one screen at a time until and unless i dont visit to another index of navigation bar.我想一次只加载一个屏幕,直到并且除非我不访问导航栏的另一个索引。

How can i achieve this in my code, currently after login it is loading every screen before i visit in flutter which is making it tough for me as i want the data to reload before showing the screen.我如何在我的代码中实现这一点,目前登录后它会在我访问 flutter 之前加载每个屏幕,这对我来说很难,因为我希望在显示屏幕之前重新加载数据。

import 'package:projectName/views/home/index.dart';
import 'package:projectName/views/profile/index.dart';
import 'package:projectName/views/profile/views/Prescription/index.dart';
import 'package:projectName/views/profile/views/orders/index.dart';
import 'package:projectName/widgets/BottomNavigator/BottomNavigatorController.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class BottomNavigatorWidget extends StatelessWidget {
  final BottomNavigatorController controller =
      Get.put(BottomNavigatorController());
  @override
  Widget build(BuildContext context) {
    return GetBuilder<BottomNavigatorController>(
      builder: (controller) {
        return Scaffold(
          body: SafeArea(
            child: IndexedStack(
              index: controller.tabIndex.value,
              children: [
                Homepage(),
                MyOrders(),
                PrescriptionPage(),
                MyProfile(),
              ],
            ),
          ),
          bottomNavigationBar: BottomNavigationBar(
            unselectedItemColor: Colors.black45,
            selectedItemColor: Color(0xFF2e3192),
            onTap: controller.changeTabIndex,
            currentIndex: controller.tabIndex.value,
            showSelectedLabels: true,
            showUnselectedLabels: true,
            type: BottomNavigationBarType.fixed,
            backgroundColor: Colors.white,
            elevation: 50,
            items: [
              _bottomNavigationBarItem(
                icon: CupertinoIcons.house_fill,
                label: 'Home',
              ),
              _bottomNavigationBarItem(
                icon: CupertinoIcons.cube_box_fill,
                label: 'My Orders',
              ),
              _bottomNavigationBarItem(
                icon: CupertinoIcons.square_list_fill,
                label: 'Prescription',
              ),
              _bottomNavigationBarItem(
                icon: CupertinoIcons.person_alt_circle_fill,
                label: 'Account',
              ),
            ],
          ),
        );
      },
    );
  }

  _bottomNavigationBarItem({IconData icon, String label}) {
    return BottomNavigationBarItem(
      icon: Icon(icon),
      label: label,
    );
  }
}

Here is the Controller.dart file这是 Controller.dart 文件

import 'package:get/get.dart';

class BottomNavigatorController extends GetxController {
  Rxn<int> tabIndex = Rxn<int>(0);

  void changeTabIndex(int index) {
    tabIndex.value = index;
    update();
  }
}

IndexedStack widgets' purpose is to show one widget at a time among multiple widgets. IndexedStack 小部件的目的是在多个小部件中一次显示一个小部件。 But, IndexedStack will build and load all the widgets passed to it during load, which this result in too many controllers get loaded at the same time in your app.但是,IndexedStack 将构建并加载在加载期间传递给它的所有小部件,这会导致在您的应用程序中同时加载太多控制器。

try using TabBarView or a normal list instead of IndexedStack尝试使用 TabBarView 或普通列表而不是 IndexedStack

import 'package:projectName/views/home/index.dart';
import 'package:projectName/views/profile/index.dart';
import 'package:projectName/views/profile/views/Prescription/index.dart';
import 'package:projectName/views/profile/views/orders/index.dart';
import 'package:projectName/widgets/BottomNavigator/BottomNavigatorController.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class BottomNavigatorWidget extends StatelessWidget {
  final BottomNavigatorController controller =
      Get.put(BottomNavigatorController());

  final _pages = [
    Homepage(),
    MyOrders(),
    PrescriptionPage(),
    MyProfile(),
  ];

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BottomNavigatorController>(
      builder: (controller) {
        return Scaffold(
          body: SafeArea(
            child: _pages[controller.tabIndex.value],
          ),
          bottomNavigationBar: BottomNavigationBar(
            unselectedItemColor: Colors.black45,
            selectedItemColor: Color(0xFF2e3192),
            onTap: controller.changeTabIndex,
            currentIndex: controller.tabIndex.value,
            showSelectedLabels: true,
            showUnselectedLabels: true,
            type: BottomNavigationBarType.fixed,
            backgroundColor: Colors.white,
            elevation: 50,
            items: [
              _bottomNavigationBarItem(
                icon: CupertinoIcons.house_fill,
                label: 'Home',
              ),
              _bottomNavigationBarItem(
                icon: CupertinoIcons.cube_box_fill,
                label: 'My Orders',
              ),
              _bottomNavigationBarItem(
                icon: CupertinoIcons.square_list_fill,
                label: 'Prescription',
              ),
              _bottomNavigationBarItem(
                icon: CupertinoIcons.person_alt_circle_fill,
                label: 'Account',
              ),
            ],
          ),
        );
      },
    );
  }

  _bottomNavigationBarItem({IconData icon, String label}) {
    return BottomNavigationBarItem(
      icon: Icon(icon),
      label: label,
    );
  }
}

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

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