dart 永久性导航栏:我如何从另一个选项卡导航到导航选项卡?

vzgqcmou  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(157)

我使用persistent_bottom_nav_bar package作为我的导航栏,我想从另一个屏幕自动导航到导航栏的标签。

class MyBottomNavBar extends StatelessWidget {
  PersistentTabController controller = PersistentTabController(initialIndex: 0);
  MyBottomNavBar({super.key, required this.controller});
  @override
  Widget build(BuildContext context) {
    return PersistentTabView(
      context,
      controller: controller,
      screens: _buildScreens(),
      items: _navBarsItems(),
      confineInSafeArea: true,
      backgroundColor: Colors.white, // Default is Colors.white.
      handleAndroidBackButtonPress: true, // Default is true.
      resizeToAvoidBottomInset:
          true, // This needs to be true if you want to move up the screen when keyboard appears. Default is true.
      stateManagement: true, // Default is true.
      hideNavigationBarWhenKeyboardShows:
          true, // Recommended to set 'resizeToAvoidBottomInset' as true while using this argument. Default is true.
      decoration: NavBarDecoration(
        borderRadius: BorderRadius.circular(10.0),
        colorBehindNavBar: Colors.white,
      ),
      popAllScreensOnTapOfSelectedTab: true,
      popActionScreens: PopActionScreensType.all,
      itemAnimationProperties: const ItemAnimationProperties(
        // Navigation Bar's items animation properties.
        duration: Duration(milliseconds: 200),
        curve: Curves.ease,
      ),
      screenTransitionAnimation: const ScreenTransitionAnimation(
        // Screen transition animation on change of selected tab.
        animateTabTransition: true,
        curve: Curves.ease,
        duration: Duration(milliseconds: 200),
      ),
      navBarStyle:
          NavBarStyle.style10, // Choose the nav bar style with this property.
    );
  }

  List<Widget> _buildScreens() {
    return [
      const ZekrCounterScreen(),
      const ZekrListScreen(),
      const AddZekrScreen(),
      const Center(child: Text('آمار')),
      const Center(child: Text('تنظیمات')),
    ];
  }

  List<PersistentBottomNavBarItem> _navBarsItems() {
    return [
      PersistentBottomNavBarItem(
        icon: const Icon(CupertinoIcons.cube),
        title: ("ذکر شمار"),
        activeColorPrimary: CupertinoColors.activeBlue,
        inactiveColorPrimary: CupertinoColors.systemGrey,
        activeColorSecondary: Colors.white,
      ),
      PersistentBottomNavBarItem(
        icon: const Icon(CupertinoIcons.list_bullet),
        title: ("لیست اذکار"),
        activeColorPrimary: CupertinoColors.activeGreen,
        inactiveColorPrimary: CupertinoColors.systemGrey,
        activeColorSecondary: Colors.white,
        routeAndNavigatorSettings: RouteAndNavigatorSettings(
          initialRoute: "/",
          routes: {
            "/list": (final context) => const ZekrListScreen(),
            "/add": (final context) => const AddZekrScreen(),
            "/chart": (final context) => const Center(child: Text('آمار')),
            "/settings": (final context) =>
                const Center(child: Text('تنظیمات')),
          },
        ),
      ),
      PersistentBottomNavBarItem(
        icon: const Icon(CupertinoIcons.add),
        title: ("افزودن ذکر"),
        activeColorPrimary: Colors.blue.shade300,
        inactiveColorPrimary: CupertinoColors.systemGrey,
        activeColorSecondary: Colors.white,
        routeAndNavigatorSettings: RouteAndNavigatorSettings(
          initialRoute: "/",
          routes: {
            "/list": (final context) => const ZekrListScreen(),
            "/add": (final context) => const AddZekrScreen(),
            "/chart": (final context) => const Center(child: Text('آمار')),
            "/settings": (final context) =>
                const Center(child: Text('تنظیمات')),
          },
        ),
      ),
      PersistentBottomNavBarItem(
        icon: const Icon(CupertinoIcons.chart_pie),
        title: ("آمار"),
        activeColorPrimary: Colors.deepOrange,
        inactiveColorPrimary: CupertinoColors.systemGrey,
        activeColorSecondary: Colors.white,
        routeAndNavigatorSettings: RouteAndNavigatorSettings(
          initialRoute: "/",
          routes: {
            "/list": (final context) => const ZekrListScreen(),
            "/add": (final context) => const AddZekrScreen(),
            "/chart": (final context) => const Center(child: Text('آمار')),
            "/settings": (final context) =>
                const Center(child: Text('تنظیمات')),
          },
        ),
      ),
      PersistentBottomNavBarItem(
        icon: const Icon(CupertinoIcons.settings),
        title: ("تنظیمات"),
        activeColorPrimary: Colors.deepPurple,
        inactiveColorPrimary: CupertinoColors.systemGrey,
        activeColorSecondary: Colors.white,
        routeAndNavigatorSettings: RouteAndNavigatorSettings(
          initialRoute: "/",
          routes: {
            "/list": (final context) => const ZekrListScreen(),
            "/add": (final context) => const AddZekrScreen(),
            "/chart": (final context) => const Center(child: Text('آمار')),
            "/settings": (final context) =>
                const Center(child: Text('تنظیمات')),
          },
        ),
      ),
    ];
  }
}

我想改变底部导航栏的状态也,我认为我可以访问控制器在标签屏幕。任何建议或想法是接受的。

2q5ifsrm

2q5ifsrm1#

您可以使用一些状态管理包,如:

  1. flutter_bloc
  2. get
  3. provider
    1.或者使用StatefulWidget主页来控制PersistentTabController并自动更改您的:index类似于:
    持久化选项卡控制器(索引:值)

相关问题