【问题标题】:BottomNavigationBar and Provider?BottomNavigationBar 和提供者?
【发布时间】:2021-03-30 20:35:58
【问题描述】:

我使用底部导航栏并在底部添加了 3 个按钮。我正在使用 setate 交换页面。这些按钮工作正常。但是,如果我在使用这些按钮调用的页面中调用另一个页面,BottomNavigationBar 就会消失。我已经研究过了,发现了这个click me。使用Provider调用页面合乎逻辑吗?我认为它将每一页都保存在内存中,它不会消耗太多内存吗?

 import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int selectedPage = 0;

  final _pageOptions = [
    HomeScreen(),
    InboxScreen(),
    SignInScreen()
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: _pageOptions[selectedPage],
        bottomNavigationBar: BottomNavigationBar(
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.home, size: 30), title: Text('Home')),
            BottomNavigationBarItem(icon: Icon(Icons.mail, size: 30), title: Text('Inbox')),
            BottomNavigationBarItem(icon: Icon(Icons.account_circle, size: 30), title: Text('Account')),
          ],
          selectedItemColor: Colors.green,
          elevation: 5.0,
          unselectedItemColor: Colors.green[900],
          currentIndex: selectedPage,
          backgroundColor: Colors.white,
          onTap: (index){
            setState(() {
              selectedPage = index;
            });
          },
        )
    );
  }
}

这是我的主页。如果我点击了文字,底部导航栏就会消失

class _HomePage extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(appBar: AppBar(title: Text('Page2')),body:GestureDetector(
        onTap: () {
    
            Navigator.of(context).push(MaterialPageRoute(builder: (context) => NewScreen()));

        },
        child:Text('clik')));
  }
}

我想喜欢这个 但我的代码运行这个

【问题讨论】:

  • 一些代码将有助于理解您的问题!如果您需要在页面之间共享值和/或操作,Provider 库会很有帮助,您的问题在其他地方:)
  • 只是我想调用另一个页面 keep bottomnavigationbar 。想象一下 instagram 。如果您在 Instagram 上搜索某人并单击他们的个人资料,底部导航栏仍然存在

标签: flutter flutter-provider flutter-bottomnavigation


【解决方案1】:

您发布的代码看起来非常好!
您的问题一定出在其他地方,可能在其中一个小部件中

final _pageOptions = [
  HomeScreen(),
  InboxScreen(),
  SignInScreen()
];

您的问题可能有很多根本原因,您可以首先确定是哪个 Widget 使 bottomNavigationBar 消失。
发现后,请确保您没有使用 Navigator.of(context).push 方法,这可能是您的底部导航栏消失的原因之一。

如果您仍然卡住,请随时使用 Widget 的源代码更新您的问题,使栏消失,我会更新我的答案

编辑
正如我之前提到的,每当您调用 Navigator.push(context, UserProfilePage); 方法时,您都在用新的小部件替换之前的小部件。
由于您之前的 Widget 持有您的 bottomNavigationBar,这就是它消失的原因。

您在这里寻找的是,如何拥有一个持久的导航栏。
这里有两个有用的链接,可以帮助您和其他有此需求的人,a Video tutorial to understand how to implement itwell written article to fully understand how it works

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    相关资源
    最近更新 更多