繁体   English   中英

如何在Flutter的详细页面中保持底部导航栏可见?

[英]How to keep the bottom navigation bar visible in detail pages in Flutter?

我试图在应用程序的初始页面中构建一个底部导航栏,我在这个底部栏中添加了几个选项卡,用户应该能够点击其中一个以访问不同的选项卡。 但该栏仅用于导航到其他页面。 在其中一个页面中,我添加了card ,用户应该点击它们来查看详细信息页面。 但是,当它在详细信息页面中时,导航栏消失了。 我想知道如何让我的导航栏在应用程序中始终可见。

下面的代码是关于我如何在主页中建立底部栏,

......

  int currentTab = 0; // to keep track of active tab index
  final List<Widget> screens = [
    recommend(),
    optional(),
    backtest(),
    machine(),
  ]; // to store tab views

  Widget currentScreen = recommend(); // initial pages

  final PageStorageBucket bucket = PageStorageBucket(); 

......

  bottomNavigationBar: BottomAppBar(
        color: Colors.white,
        elevation: 0.2,
        shape: CircularNotchedRectangle(),
        notchMargin: 10,
        child: Container(
          height: 60,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  MaterialButton(
                    minWidth: 40,
                    onPressed: (){
                      setState(() {
                        currentScreen = recommend();
                        currentTab = 0;
                      });
                    },
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                      Icon(
                        Icons.format_line_spacing,
                        color: currentTab == 0 ? Colors.redAccent : Colors.grey,),
                      Text(
                        '',
                        style: TextStyle(
                          color: currentTab == 0 ? Colors.redAccent : Colors.grey, ),)
                    ],),
                  ),
                  MaterialButton(
                    minWidth: 40,
                    onPressed: (){
                      setState(() {
                        currentScreen = optional();
                        currentTab = 1;
                      });
                    },
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                      Icon(
                        Icons.offline_pin,
                        color: currentTab == 1 ? Colors.redAccent : Colors.grey,),
                      Text(
                        '',
                        style: TextStyle(
                          color: currentTab == 1 ? Colors.redAccent : Colors.grey, ),)
                    ],),
                  ),
                ],
              ),

              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  MaterialButton(
                    minWidth: 40,
                    onPressed: (){
                      setState(() {
                        currentScreen = backtest();
                        currentTab = 2;
                      });
                    },
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Icon(
                          Icons.replay,
                          color: currentTab == 2 ? Colors.redAccent : Colors.grey,),
                        Text(
                          '',
                          style: TextStyle(
                            color: currentTab == 2 ? Colors.redAccent : Colors.grey, ),)
                      ],),
                  ),
                  MaterialButton(
                    minWidth: 40,
                    onPressed: (){
                      setState(() {
                        currentScreen = machine();
                        currentTab = 3;
                      });
                    },
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Icon(
                          Icons.android,
                          color: currentTab == 3 ? Colors.redAccent : Colors.grey,),
                        Text(
                          '',
                          style: TextStyle(
                            color: currentTab == 3 ? Colors.redAccent : Colors.grey, ),)
                      ],),
                  ),
                ],
              )

            ],
          ),
        ),
      ),

下面的代码是我如何导航到详细信息页面,

 return Container(
      child: Card(
        child: new InkWell(
          onTap: (){
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => stockDetail()));
          },
......

我正在寻找一种基于我现有代码解决问题的方法,但如果有任何其他有效的方法来解决它,我将不胜感激。

使用此插件P Resistance_bottom_nav_bar 。根据问题,您需要每个页面中的底部导航栏。您可以在链接上方的特定屏幕结帐中禁用底部导航

在我看来这是一个非常好的插件。在这个链接中结帐导航样式。你可以更改底部导航栏 navBarStyle 的设计:NavBarStyle.style9,只需将 style9 更改为插件提供的任何数字。 我相信其中有 15 个可用

您可以使用自定义图标代替默认图标,也可以代替此 CupertinoColors.systemPurple 您也可以使用 Colors.red 之类的让我知道它是否有效

PersistentTabController _controller =PersistentTabController(initialIndex: 0);

//Screens for each nav items.
  List<Widget> _NavScreens() {
    return [
      recommend(),
      optional(),
      backtest(),
      machine(),
      
    ];
  }


  List<PersistentBottomNavBarItem> _navBarsItems() {
    return [
      PersistentBottomNavBarItem(
       icon: Icon(Icons.home),
        title: ("Recommend"),
        activeColor: CupertinoColors.activeBlue,
        inactiveColor: CupertinoColors.systemGrey,
      ),
      PersistentBottomNavBarItem(
        icon: Icon(Icons.favorite),
        title: ("Optional"),
        activeColor: CupertinoColors.activeGreen,
        inactiveColor: CupertinoColors.systemGrey,
      ),
      PersistentBottomNavBarItem(
        icon: Icon(Icons.person_pin),
        title: ("Backtest"),
        activeColor: CupertinoColors.systemRed,
        inactiveColor: CupertinoColors.systemGrey,
      ),
      PersistentBottomNavBarItem(
        icon: Icon(Icons.local_activity),
        title: ("Machine"),
        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,
      ),
    );
}

我遇到了类似的问题。 根据我从您的查询中了解到的是,在不同的选项卡(在底部导航栏中)上调用了不同的页面,但应用栏/底部导航栏本身不可见。

我建议您创建另一个 dart 文件。 让我们说它blank.dart为方便起见。 在此空白中。dart 您为底部导航栏编写代码(您已经编写过)并在此空白中调用所有不同的页面。dart 所以基本上,导航栏对于所有其他将被调用的页面都是通用的在这个空白中。dart 您可以将其理解为空白。dart 是父级,所有其他不同的选项卡都是其子级,因此父级可以导航到其子小部件。

您可以在 lib/screens/blank.dart: http://github.com/samflab/fitility查看我的 GitHub 以获取类似的解决方案

您好,您可以通过在 main.dart 中添加 BottomNavigationBar 来解决此问题,然后根据 BottomNavigationBaritem 的 currentIndex 您可以更改脚手架中的屏幕(body:anyScreenAccordingToindex)。例如,如果 currentIndex==2 那么 body:secondPage()

暂无
暂无

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

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