flutter 导航到新页面时底部导航栏消失

nlejzf6q  于 2022-11-17  发布在  Flutter
关注(0)|答案(2)|浏览(593)

我已经用我的应用程序创建了一个BottomNavigationBar,但是当我点击Profile导航到一个新页面时,BottomNavigationBar就消失了。我已经在这个问题上工作了几个小时,我打算给予lol。有什么想法吗?
app.dart

@override
  Widget build(BuildContext context, WidgetRef ref) {
    return MaterialApp(
      routes: {
        '/home':(context) => HomePage(),
        '/activity':(context) => ActivityPage(),
        '/profile':(context) => ProfilePage(),
      },
      home: Builder(
        builder: (context) => Scaffold(
          bottomNavigationBar: BottomNavigationBar(
            items: const [
              BottomNavigationBarItem(
                  icon: Icon(Icons.home_filled), label: 'Home',),
              BottomNavigationBarItem(
                  icon: Icon(Icons.track_changes), label: 'Activity'),
              BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
            ],
            onTap: (index) {
              switch (index) {
                case 0:
                  Navigator.pushNamed(context, '/home');
                break;
                case 1:
                  Navigator.pushNamed(context, '/activity');
                break;
                case 2:
                  Navigator.pushNamed(context, '/profile');
                break;
              }
            },
          ),
          body: MaterialApp.router(
            builder: EasyLoading.init(),
            theme: ThemeData(
              appBarTheme: const AppBarTheme(color: Color(0xFF13B9FF)),
              colorScheme: ColorScheme.fromSwatch(
                accentColor: const Color(0xFF13B9FF),
              ),
            ),
            routerDelegate: AutoRouterDelegate(
              _appRouter,
              navigatorObservers: () => [AppRouteObserver()],
            ),
            routeInformationProvider: _appRouter.routeInfoProvider(),
            routeInformationParser: _appRouter.defaultRouteParser(),
            localizationsDelegates: const [
              AppLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
            ],
            supportedLocales: AppLocalizations.supportedLocales,
            debugShowCheckedModeBanner: false,
          ),
        ),
      ),
    );
  }
}

我试着点击不同的页面,仍然没有得到导航栏跟随。

uemypmqf

uemypmqf1#

问题是Navigator.pushNamed,因为您正在导航到一个新页面,而在新页面上没有BottomNavigationBar。您可以使用PageView并为其分配一个PageController。之后,您可以重写BottomNavigationBaronTap,并通过先前分配的PageController控制PageView

gkn4icbw

gkn4icbw2#

我遇到了同样的问题,并用这个库解决了它。https://pub.dev/packages/persistent_bottom_nav_bar_v2/versions/4.0.3

pushNewScreenWithRouteSettings(
    context,
    settings: RouteSettings(name: MainScreen.routeName),
    screen: MainScreen(),
    withNavBar: true,
    pageTransitionAnimation: PageTransitionAnimation.cupertino,
);

导航栏:false-〉可通过此控制

相关问题