Flutter 避免底部导航栏中的持久状态管理

发布于 2025-01-11 03:50:37 字数 903 浏览 0 评论 0原文

我正在使用库 persistent_bottom_nav_bar 并将 stateManagement 选项设置为 false,因为在页面中我希望每次进入页面时都使用未来的构建器。

问题是未来的构建器不再被调用,或者它被调用但页面仍然是以前的状态。

这是我的 PersistentTabView

PersistentTabView(
  context,
  screens: _NavScreens(),
  items: _navBarsItems(),
  confineInSafeArea: true,
  backgroundColor: Colors.white,
  handleAndroidBackButtonPress: true,
  resizeToAvoidBottomInset: true,
  hideNavigationBarWhenKeyboardShows: true,
  hideNavigationBar: false,
  stateManagement: false,
  decoration: NavBarDecoration(
    border: Border(top: BorderSide(color: Colors.grey, width: 0.5))
  ),
  popAllScreensOnTapOfSelectedTab: true,
  navBarStyle: NavBarStyle.style6,
),

在示例中,我第一次进入页面时,将调用未来构建器并显示列表。如果我更改屏幕并返回到前一个屏幕,则未来的构建器将无法工作。

例子: https://imgur.com/a/jJWJPvU

I'm using the library persistent_bottom_nav_bar and I settled the stateManagement option equals to false because in a page I want to use a future builder each time that I enter the page.

The problem is that the future builder is not called anymore, or It is called but the page is still with the previous state.

This is my PersistentTabView:

PersistentTabView(
  context,
  screens: _NavScreens(),
  items: _navBarsItems(),
  confineInSafeArea: true,
  backgroundColor: Colors.white,
  handleAndroidBackButtonPress: true,
  resizeToAvoidBottomInset: true,
  hideNavigationBarWhenKeyboardShows: true,
  hideNavigationBar: false,
  stateManagement: false,
  decoration: NavBarDecoration(
    border: Border(top: BorderSide(color: Colors.grey, width: 0.5))
  ),
  popAllScreensOnTapOfSelectedTab: true,
  navBarStyle: NavBarStyle.style6,
),

In the example the first time that I enter in the page the future builder is called and the list appears. If I change screen and I go back in the previous screen the future builder is not working.

Example:
https://imgur.com/a/jJWJPvU

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖彼岸 2025-01-18 03:50:37

我对 persistent_bottom_nav_bar 的 stateManagement 选项也有同样的问题。我将其描述为 false,它对状态没有影响所以我找到了另一个解决方案。我在我的项目中使用了 bloc 和提供程序。我用全局键解决了它,我将分享我的代码希望它有效。

 Map<int, GlobalKey<NavigatorState>> navigatorKeys = {
0: GlobalKey<NavigatorState>(),
1: GlobalKey<NavigatorState>(),
2: GlobalKey<NavigatorState>(),
3: GlobalKey<NavigatorState>(),
4: GlobalKey<NavigatorState>(),
         };

 late final List<Widget> _widgetOptions = 
  <Widget>[
  HomeView(companyId: 0, isDialogShow: isShow, bannerListModel: 
  bannerList, notificationModel: notificationList),
   CategoryTabView(),
     FavoritiesView(),
   BasketView(),
  ProfileView()
   ];

   @override
  Widget build(BuildContext context) {
  final basketProvider = Provider.of<BasketProvider>(context);
  final progressProvider = Provider.of<ProgressProvider>(context);
  final favoriteProvider = Provider.of<FavoriteProvider>(context);
  final bottomProvider = Provider.of<BottomNavBarProvider>(context);

return Scaffold(
  bottomNavigationBar: BottomNavigationBar(
    items: <BottomNavigationBarItem>[
      BottomNavigationBarItem(
          icon: Image.asset(
            ImageConstants.instance.homeIcon,
            color: context.colorScheme.error,
            scale: 3,
          ),
          label: ""),
        BottomNavigationBarItem(
          icon: Image.asset(
            ImageConstants.instance.categoryIcon,
            scale: 3,
          ),
          label: ""),
      BottomNavigationBarItem(
          icon: favoriteIcon(favoriteProvider), activeIcon: 
       favoriteIcon(favoriteProvider), label: ""),
      BottomNavigationBarItem(
          icon: badgeBasket(basketProvider, false), activeIcon: 
      badgeBasket(basketProvider, true), label: ""),
      BottomNavigationBarItem(
          icon: Image.asset(
            ImageConstants.instance.profileIcon,
            scale: 3,
          ),
          activeIcon: Image.asset(
            ImageConstants.instance.profileRedIcon,
            scale: 3,
          ),
          label: ""),
       ],
        currentIndex: bottomProvider.selectedIndex,
        onTap: bottomProvider.onItemTapped,
       ),
      body: buildNavigator(bottomProvider),
       );
         }

   buildNavigator(BottomNavBarProvider bottomProvider) {
   return Navigator(
    key: navigatorKeys[bottomProvider.selectedIndex],
     onGenerateRoute: (RouteSettings settings) {
     return MaterialPageRoute(builder: (_) => 
     _widgetOptions.elementAt(bottomProvider.selectedIndex));
     },
     );
     }
    }

     //this my provider

class BottomNavBarProvider with ChangeNotifier {
  int _selectedIndex = 0;
  int get selectedIndex => _selectedIndex;

  set selectedIndex(int index) {
    _selectedIndex = index;
    notifyListeners();
  }

  void onItemTapped(int index) {
    selectedIndex = index;
    notifyListeners();
  }
}

使用全局键方法,您可以使用静态底部导航栏,并且可以避免持久状态管理。希望它对您有所帮助。

i had same problem with persistent_bottom_nav_bar's stateManagement option.I describe it to false it did not effect to state.So i found another solution.I was using bloc and provider in my project.I solved it with global keys i'll share my code hope it works.

 Map<int, GlobalKey<NavigatorState>> navigatorKeys = {
0: GlobalKey<NavigatorState>(),
1: GlobalKey<NavigatorState>(),
2: GlobalKey<NavigatorState>(),
3: GlobalKey<NavigatorState>(),
4: GlobalKey<NavigatorState>(),
         };

 late final List<Widget> _widgetOptions = 
  <Widget>[
  HomeView(companyId: 0, isDialogShow: isShow, bannerListModel: 
  bannerList, notificationModel: notificationList),
   CategoryTabView(),
     FavoritiesView(),
   BasketView(),
  ProfileView()
   ];

   @override
  Widget build(BuildContext context) {
  final basketProvider = Provider.of<BasketProvider>(context);
  final progressProvider = Provider.of<ProgressProvider>(context);
  final favoriteProvider = Provider.of<FavoriteProvider>(context);
  final bottomProvider = Provider.of<BottomNavBarProvider>(context);

return Scaffold(
  bottomNavigationBar: BottomNavigationBar(
    items: <BottomNavigationBarItem>[
      BottomNavigationBarItem(
          icon: Image.asset(
            ImageConstants.instance.homeIcon,
            color: context.colorScheme.error,
            scale: 3,
          ),
          label: ""),
        BottomNavigationBarItem(
          icon: Image.asset(
            ImageConstants.instance.categoryIcon,
            scale: 3,
          ),
          label: ""),
      BottomNavigationBarItem(
          icon: favoriteIcon(favoriteProvider), activeIcon: 
       favoriteIcon(favoriteProvider), label: ""),
      BottomNavigationBarItem(
          icon: badgeBasket(basketProvider, false), activeIcon: 
      badgeBasket(basketProvider, true), label: ""),
      BottomNavigationBarItem(
          icon: Image.asset(
            ImageConstants.instance.profileIcon,
            scale: 3,
          ),
          activeIcon: Image.asset(
            ImageConstants.instance.profileRedIcon,
            scale: 3,
          ),
          label: ""),
       ],
        currentIndex: bottomProvider.selectedIndex,
        onTap: bottomProvider.onItemTapped,
       ),
      body: buildNavigator(bottomProvider),
       );
         }

   buildNavigator(BottomNavBarProvider bottomProvider) {
   return Navigator(
    key: navigatorKeys[bottomProvider.selectedIndex],
     onGenerateRoute: (RouteSettings settings) {
     return MaterialPageRoute(builder: (_) => 
     _widgetOptions.elementAt(bottomProvider.selectedIndex));
     },
     );
     }
    }

     //this my provider

class BottomNavBarProvider with ChangeNotifier {
  int _selectedIndex = 0;
  int get selectedIndex => _selectedIndex;

  set selectedIndex(int index) {
    _selectedIndex = index;
    notifyListeners();
  }

  void onItemTapped(int index) {
    selectedIndex = index;
    notifyListeners();
  }
}

With global key method you can use your bottom nav bar static and you can avoid persistent state management.Hope it helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文