【问题标题】:Bottom Navigation Not Persisting On Some Pages底部导航未保留在某些页面上
【发布时间】:2021-10-16 13:47:20
【问题描述】:

我一直在尝试让底部导航栏在所有页面屏幕上持续存在,但它看起来只持续存在于底部导航上的页面,即 HomeScreen()、DiscoverScreen()、GivingScreen()、EventsScreen () 和设置屏幕()。其他页面没有得到底部导航。这是我的底部导航栏的代码。如何在应用中的所有页面上添加底部导航栏?

class CustomBottomNavBar extends StatefulWidget {
  const CustomBottomNavBar({Key key}) : super(key: key);

  @override
  _CustomBottomNavBarState createState() => _CustomBottomNavBarState();
}

class _CustomBottomNavBarState extends State<CustomBottomNavBar> {
  int currentIndex = 0;

  final screens = [
    HomeScreen(),
    DiscoverScreen(),
    GivingScreen(),
    EventsScreen(),
    SettingsScreen(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: currentIndex,
        children: screens,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        backgroundColor: kPrimaryColor,
        selectedItemColor: kSelectedItemColor,
        iconSize: kBottomNavIconSize,
        unselectedItemColor: kAlternativeColor,
        selectedFontSize: kBottomNavFontSize,
        unselectedFontSize: kBottomNavFontSize,
        showUnselectedLabels: true,
        showSelectedLabels: true,
        currentIndex: currentIndex,
        onTap: (index) => setState(() => currentIndex = index),
        items: [
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.mic_fill,
            ),
            label: 'Sermons',
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.wand_stars_inverse,
            ),
            label: 'Discover',
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.heart_fill,
            ),
            label: 'Giving',
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.calendar_today,
            ),
            label: 'Events',
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.gear_alt_fill,
            ),
            label: 'Settings',
          ),
        ],
      ),
    );
  }
}

这是一张底部导航的视图图片

这是一张没有底部的视图的图片 nav

import 'package:church_app/components/widgets/animated_like_button.dart';
import 'package:church_app/components/widgets/custom_bottom_nav_bar.dart';
import 'package:church_app/components/widgets/navigation_drawer.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:church_app/utilities/constants.dart';
import 'package:church_app/components/widgets/action_and_text.dart';

class SermonDescriptionScreen extends StatefulWidget {
  @override
  _SermonDescriptionScreenState createState() =>
      _SermonDescriptionScreenState();
}

class _SermonDescriptionScreenState extends State<SermonDescriptionScreen> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        endDrawer: NavigationDrawer(),
        appBar: AppBar(
          leading:
              (ModalRoute.of(context)?.canPop ?? false) ? BackButton() : null,
          iconTheme: IconThemeData(color: kPrimaryColor),
          elevation: 0,
          title: Text(
            'Protect The Vessel',
            style: kMainStyling,
          ),
          centerTitle: true,
        ),
        body: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: SafeArea(
            left: true,
            right: true,
            top: true,
            bottom: true,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  height: 250,
                  decoration: BoxDecoration(
                    boxShadow: <BoxShadow>[
                      BoxShadow(
                        color: Colors.grey[300],
                        blurRadius: 30,
                        offset: Offset(0, 10),
                      ),
                    ],
                    image: DecorationImage(
                      fit: BoxFit.cover,
                      image: AssetImage('assets/images/pastor.jpg'),
                    ),
                  ),
                ),
                addVSpace(30),
                Container(
                  margin: EdgeInsets.only(left: 10, right: 10, bottom: 10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'Protect The Vessel',
                        style: kDescriptionTitle,
                        softWrap: true,
                      ),
                      addVSpace(3),
                      Text(
                        'Pastor James Wiseman',
                        style: kMainStyling.copyWith(
                          color: kPrimaryColor.withOpacity(.8),
                        ),
                        softWrap: true,
                      ),
                      addVSpace(3),
                      Row(
                        children: [
                          Text(
                            'Jan 25, 2021',
                            style: kMainStyling.copyWith(
                              color: kPrimaryColor.withOpacity(.8),
                            ),
                            softWrap: true,
                          ),
                          Padding(
                            padding: const EdgeInsets.only(left: 10, right: 10),
                            child: Container(
                              decoration: BoxDecoration(
                                shape: BoxShape.circle,
                                color: kPrimaryColor,
                              ),
                              width: 8,
                              height: 8,
                            ),
                          ),
                          Container(
                            child: AnimatedLikeButton(),
                          ),
                        ],
                      ),
                      addVSpace(20),
                      Text(
                        'Are you protecting what matters? In Protect What Matters, Pastor James Wiseman reminds us that we are vessels that can either foster bitterness or make way for the healing hand of God in our lives.',
                        style: kMainStyling,
                        softWrap: true,
                        textAlign: TextAlign.justify,
                        maxLines: 6,
                      ),
                      addVSpace(30),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          ActionAndText(
                            correspondingIcon:
                                CupertinoIcons.arrow_down_to_line_alt,
                            correspondingText: 'Download',
                            gesture: () {},
                          ),
                          ActionAndText(
                            correspondingIcon: CupertinoIcons.heart,
                            correspondingText: 'Like',
                            gesture: () {},
                          ),
                          ActionAndText(
                            correspondingIcon: CupertinoIcons.play,
                            correspondingText: 'Listen',
                            gesture: () {
                              Navigator.pushNamed(context, '/playerScreen');
                            },
                          ),
                          ActionAndText(
                            correspondingIcon: CupertinoIcons.square_arrow_up,
                            correspondingText: 'Share',
                            gesture: () {},
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 你能显示第二张图片视图的代码吗?
  • 是的,这里是:[链接] (mega.nz/file/…)
  • 您必须在 SermonDescriptionScreen 类的 build 方法中包含 bottomNavigatorBar。 bottomNavigationBar: CustomBottomNavBar()

标签: flutter dart widget


【解决方案1】:

使用来自 pub dev 网站的 custom_navigator 0.3.0 包

【讨论】:

    【解决方案2】:

    要获得这个,您可以将 pageView 与 bottomNavigationBar 一起使用...这是一个示例代码。这将给出如何去做的想法..

     Scaffold buildAuthScreen() {
        return Scaffold(
          body: PageView(
            scrollDirection: Axis.horizontal,
            children: [
              Timeline(),
              ActivityFeedItem(),
              Search(),
              Upload(),
              Profile(
                currentUser: _auth.currentUser.uid,
              ),
            ],
            controller: _pageController,
            onPageChanged: (value) {
              setState(() {
                pageIndex = value;
              });
            },
            physics: BouncingScrollPhysics(),
          ),
          bottomNavigationBar: CurvedNavigationBar(
            key: _bottomNavigationKey,
            index: pageIndex,
            height: 50.0,
            color: Theme.of(context).primaryColor,
            items: [
              Icon(Icons.whatshot, color: Colors.white),
              Icon(Icons.notifications_active, color: Colors.white),
              Icon(Icons.search, color: Colors.white),
              Icon(Icons.photo_camera, color: Colors.white),
              Icon(Icons.account_circle, color: Colors.white),
            ],
            animationDuration: Duration(milliseconds: 200),
            buttonBackgroundColor: Theme.of(context).accentColor,
            backgroundColor: Colors.white,
            onTap: (int pageIndex1) {
              _pageController.animateToPage(pageIndex1,
                  duration: Duration(microseconds: 300), curve: Curves.bounceInOut);
            },
          ),
        );
      }
    

    【讨论】:

      【解决方案3】:

      您可以使用这个包https://pub.dev/packages/persistent_bottom_nav_bar 在所有屏幕中作为持久底部导航器,

      当尝试导航到新屏幕时,请使用下面的导航器功能,

      pushNewScreen(
              context,
              screen: MainScreen(),
              withNavBar: true, // OPTIONAL VALUE. True by default.
              pageTransitionAnimation: PageTransitionAnimation.cupertino,
          );
      

      【讨论】:

        【解决方案4】:

        内置BottomNavigationBar的问题不能显示到每个屏幕,有一个名为Presistant_bottom_nav_bar的插件,这里有一些示例代码,用于更多检查PersistentTabView的其他属性,动画,状态,键盘时隐藏底部栏出现了一些东西

        如果您对使用插件不感兴趣,请查看此材料指南注意事项bottom-navigation

         PersistentTabController _controller =PersistentTabController(initialIndex: 
                      0);
            
            //Screens for each nav items.
              List<Widget> _NavScreens() {
                return [
                 HomeScreen(),
                 DiscoverScreen(),
                 GivingScreen(),
                 EventsScreen(),
                 SettingsScreen(),
                  
                ];
              }
            
            
              List<PersistentBottomNavBarItem> _navBarsItems() {
                return [
                  PersistentBottomNavBarItem(
                   icon: Icon(CupertinoIcons.mic_fill),
                    title: ("Home"),
                    activeColor: CupertinoColors.activeBlue,
                    inactiveColor: CupertinoColors.systemGrey,
                  ),
                  PersistentBottomNavBarItem(
                    icon: Icon(CupertinoIcons.wand_stars_inverse),
                    title: ("Discover"),
                    activeColor: CupertinoColors.activeGreen,
                    inactiveColor: CupertinoColors.systemGrey,
                  ),
                  PersistentBottomNavBarItem(
                    icon: Icon(CupertinoIcons.heart_fill),
                    title: ("Giving"),
                    activeColor: CupertinoColors.systemRed,
                    inactiveColor: CupertinoColors.systemGrey,
                  ),
                  PersistentBottomNavBarItem(
                    icon: Icon(CupertinoIcons.calendar_today),
                    title: ("Events"),
                    activeColor: CupertinoColors.systemIndigo,
                    inactiveColor: CupertinoColors.systemGrey,
                  ),
             PersistentBottomNavBarItem(
                    icon: Icon(CupertinoIcons.gear_alt_fill),
                    title: ("Settings"),
                    activeColor: CupertinoColors.systemIndigo,
                    inactiveColor: CupertinoColors.systemGrey,
                  ),
            
                ];
              }
            @override
            Widget build(BuildContext context) {
                return Center(
                  child: PersistentTabView(
                    controller: _controller,
                    screens: _NavScreens(),
                    items: _navBarsItems(),
                    confineInSafeArea: true,
                    backgroundColor: Colors.white,
                    handleAndroidBackButtonPress: true,
                    resizeToAvoidBottomInset: true,
                    hideNavigationBarWhenKeyboardShows: true,
                    decoration: NavBarDecoration(
                      borderRadius: BorderRadius.circular(10.0),
                    ),
                    popAllScreensOnTapOfSelectedTab: true,
                    navBarStyle: NavBarStyle.style9,
                  ),
                );
            }
        

        您可以使用此导航并使用带有插件提供的导航器的底部导航栏

        pushNewScreen(
                context,
                screen: SubScreenNames(),
                withNavBar: true,
                pageTransitionAnimation: PageTransitionAnimation.cupertino,
            );
        

        【讨论】:

          猜你喜欢
          • 2019-08-05
          • 1970-01-01
          • 1970-01-01
          • 2013-01-01
          • 2022-12-10
          • 2012-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多