移动到其他页面时无法保持底部导航栏颤动

如何解决移动到其他页面时无法保持底部导航栏颤动

我是初学者,我试过这个教程 https://youtu.be/1ToqYMSnNhA 但是当我按下其他按钮并导航到另一个屏幕时..底部栏正在消失。如何让它粘住? 我得到“底部充满无限像素”代替底部导航栏。

#main.dart
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,home: Splashscreen(),routes: <String,WidgetBuilder>{
        "/login": (BuildContext context) => LoginSignupScreen(),"/profile": (BuildContext context) => Profile(),"/home": (BuildContext context) => Home(),"/splashscreen": (BuildContext context) => Splashscreen(),"/department": (BuildContext context) => Department(),},);
  }
}


#navbar.dart
import 'package:Flutter/material.dart';
import 'package:mini_project/main.dart';

class Navbar extends StatefulWidget {
  @override
  _NavbarState createState() => _NavbarState();
}

class _NavbarState extends State<Navbar> {
  int currentIndex = 0;

  setBottomBarIndex(index) {
    setState(() {
      currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: Colors.white.withAlpha(55),body: Stack(
        children: [
          Positioned(
            bottom: 0,left: 0,child: Container(
              width: size.width,height: 80,child: Stack(
                //overflow: Overflow.visible,children: [
                  CustomPaint(
                    size: Size(size.width,80),painter: BCustomPainter(),),Center(
                    heightFactor: 0.6,child: FloatingActionButton(
                        backgroundColor: Colors.blueAccent[100],child: Icon(Icons.home),elevation: 0.1,onpressed: () {}),Container(
                    width: size.width,child: Row(
                      mainAxisAlignment:
                          MainAxisAlignment.spaceEvenly,//to align evenly
                      children: [
                        IconButton(
                          icon: Icon(
                            Icons.account_circle,size: 30.0,color: currentIndex == 0
                                ? Colors.blueAccent[100]
                                : Colors.grey.shade400,onpressed: () {
                            setBottomBarIndex(0);
                            Navigator.pushReplacementNamed(context,"/profile");
                          },splashColor: Colors.white,IconButton(
                            icon: Icon(
                              Icons.dynamic_Feed,color: currentIndex == 1
                                  ? Colors.blueAccent[100]
                                  : Colors.grey.shade400,onpressed: () {
                              setBottomBarIndex(1);
                            }),Container(
                          width: size.width * 0.20,IconButton(
                            icon: Icon(
                              Icons.workspaces_filled,color: currentIndex == 2
                                  ? Colors.blueAccent[100]
                                  : Colors.grey.shade400,onpressed: () {
                              setBottomBarIndex(2);
                              Navigator.pushReplacementNamed(
                                  context,"/department");
                            }),IconButton(
                            icon: Icon(
                              Icons.notifications,color: currentIndex == 3
                                  ? Colors.blueAccent[100]
                                  : Colors.grey.shade400,onpressed: () {
                              setBottomBarIndex(3);
                            }),],)
                ],)
        ],);
  }
}

//for the shape of the nav bar
class BCustomPainter extends CustomPainter {
  @override
  void paint(Canvas canvas,Size size) {
    Paint paint = new Paint()
      ..color = Colors.white
      ..style = PaintingStyle.fill;
//quadratic BezierTo curve
    Path path = Path();
    path.moveto(0,20); // Start
    path.quadraticBezierTo(size.width * 0.20,size.width * 0.35,0);
    path.quadraticBezierTo(size.width * 0.40,size.width * 0.40,20);
    path.arcToPoint(Offset(size.width * 0.60,20),radius: Radius.circular(20.0),clockwise: false);
    path.quadraticBezierTo(size.width * 0.60,size.width * 0.65,0);
    path.quadraticBezierTo(size.width * 0.80,size.width,20);
    path.lineto(size.width,size.height);
    path.lineto(0,20);
    canvas.drawShadow(path,Colors.black,5,true);
    canvas.drawPath(path,paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}

在这里附上了主文件和导航栏文件!请帮忙!

解决方法

使用此插件Presistant_bottom_nav_bar。现在您可以在每个屏幕上使用底部导航栏

初始化变量

PersistentTabController _controller =PersistentTabController(initialIndex: 0);

//Screens for each nav items.

  List<Widget> _NavScreens() {
    return [
     HomeScreen(),OfferScreen(),HelpScreen(),ProfileScreen(),CartViewScreen(),];
  }


  List<PersistentBottomNavBarItem> _navBarsItems() {
    return [
      PersistentBottomNavBarItem(
       icon: Icon(Icons.home),title: ("Home"),activeColor: CupertinoColors.activeBlue,inactiveColor: CupertinoColors.systemGrey,),PersistentBottomNavBarItem(
        icon: Icon(Icons.favorite),title: ("OFFERS"),activeColor: CupertinoColors.activeGreen,PersistentBottomNavBarItem(
        icon: Icon(Icons.person_pin),title: ("Help"),activeColor: CupertinoColors.systemRed,PersistentBottomNavBarItem(
        icon: Icon(Icons.local_activity),title: ("ProfileScreen"),activeColor: CupertinoColors.systemIndigo,PersistentBottomNavBarItem(
        icon: Icon(Icons.shop_cart),title: ("Cart"),];
  }
@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,);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?