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

Flutter 移动到其他页面时无法保持底部导航栏抖动,flutter,dart,bottomnavigationview,scaffold,flutter-bottomnavigation,Flutter,Dart,Bottomnavigationview,Scaffold,Flutter Bottomnavigation,我是初学者,我试过这个教程 但当我按下其他按钮时,它会导航到另一个屏幕。。底部栏正在消失。如何让它坚持? 我用“底部溢出无限像素”来代替底部导航栏 #main.dart class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home:

我是初学者,我试过这个教程 但当我按下其他按钮时,它会导航到另一个屏幕。。底部栏正在消失。如何让它坚持? 我用“底部溢出无限像素”来代替底部导航栏

#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,
                    height: 80,
                    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,
                              size: 30.0,
                              color: currentIndex == 1
                                  ? Colors.blueAccent[100]
                                  : Colors.grey.shade400,
                            ),
                            onPressed: () {
                              setBottomBarIndex(1);
                            }),
                        Container(
                          width: size.width * 0.20,
                        ),
                        IconButton(
                            icon: Icon(
                              Icons.workspaces_filled,
                              size: 30.0,
                              color: currentIndex == 2
                                  ? Colors.blueAccent[100]
                                  : Colors.grey.shade400,
                            ),
                            onPressed: () {
                              setBottomBarIndex(2);
                              Navigator.pushReplacementNamed(
                                  context, "/department");
                            }),
                        IconButton(
                            icon: Icon(
                              Icons.notifications,
                              size: 30.0,
                              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, 0, size.width * 0.35, 0);
    path.quadraticBezierTo(size.width * 0.40, 0, 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, 0, size.width * 0.65, 0);
    path.quadraticBezierTo(size.width * 0.80, 0, size.width, 20);
    path.lineTo(size.width, size.height);
    path.lineTo(0, size.height);
    path.lineTo(0, 20);
    canvas.drawShadow(path, Colors.black, 5, true);
    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}
#main.dart
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
主页:Splashscreen(),
路线:{
“/login”:(构建上下文)=>LoginSignupScreen(),
“/profile”:(BuildContext上下文)=>profile(),
“/home”:(BuildContext上下文)=>home(),
“/splashscreen”:(构建上下文)=>splashscreen(),
“/department”:(BuildContext)=>department(),
},
);
}
}
#导航栏
进口“包装:颤振/材料.省道”;
导入“包:mini_项目/main.dart”;
类Navbar扩展StatefulWidget{
@凌驾
_NavbarState createState();
}
类_NavbarState扩展状态{
int currentIndex=0;
setBottomBarIndex(指数){
设置状态(){
currentIndex=索引;
});
}
@凌驾
小部件构建(构建上下文){
最终大小=MediaQuery.of(context).Size;
返回脚手架(
背景颜色:颜色。白色。带有Alpha(55),
主体:堆栈(
儿童:[
定位(
底部:0,
左:0,,
子:容器(
宽度:size.width,
身高:80,
子:堆栈(
//溢出:溢出。可见,
儿童:[
定制油漆(
尺寸:尺寸(尺寸.宽度,80),
油漆工:b油漆工(),
),
居中(
高度系数:0.6,
子:浮动操作按钮(
背景颜色:Colors.blueAccent[100],
子:图标(Icons.home),
标高:0.1,
onPressed:(){}),
),
容器(
宽度:size.width,
身高:80,
孩子:排(
主轴对准:
MainAxisAlignment.spaceAlquired,//以均匀对齐
儿童:[
图标按钮(
图标:图标(
Icons.account_圈,
尺寸:30.0,
颜色:currentIndex==0
?颜色。蓝色调[100]
:Colors.grey.shade400,
),
已按下:(){
setBottomBarIndex(0);
Navigator.pushReplacementNamed(上下文,“/profile”);
},
颜色:颜色。白色,
),
图标按钮(
图标:图标(
Icons.dynamic_提要,
尺寸:30.0,
颜色:currentIndex==1
?颜色。蓝色调[100]
:Colors.grey.shade400,
),
已按下:(){
setBottomBarIndex(1);
}),
容器(
宽度:size.width*0.20,
),
图标按钮(
图标:图标(
图标。工作空间已填充,
尺寸:30.0,
颜色:currentIndex==2
?颜色。蓝色调[100]
:Colors.grey.shade400,
),
已按下:(){
setBottomBarIndex(2);
Navigator.pushReplacementNamed(
上下文“/部门”);
}),
图标按钮(
图标:图标(
图标、通知、,
尺寸:30.0,
颜色:currentIndex==3
?颜色。蓝色调[100]
:Colors.grey.shade400,
),
已按下:(){
setBottomBarIndex(3);
}),
],
),
)
],
),
),
)
],
),
);
}
}
//导航条的形状
类BCustomPainter扩展了CustomPainter{
@凌驾
空心油漆(帆布,尺寸){
油漆=新油漆()
…颜色=颜色。白色
..风格=绘画风格。填充;
//二次贝塞尔托曲线
路径=路径();
path.moveTo(0,20);//开始
路径。方形贝塞尔托(大小。宽度*0.20,0,大小。宽度*0.35,0);
路径。方形贝塞尔托(大小。宽度*0.40,0,大小。宽度*0.40,20);
路径弧点(偏移量(尺寸宽度*0.60,20),
半径:半径。圆形(20.0),顺时针:假);
路径。方形贝塞尔托(大小。宽度*0.60,0,大小。宽度*0.65,0);
路径。方形贝塞尔托(size.width*0.80,0,size.width,20);
path.lineTo(大小.宽度,大小.高度);
path.lineTo(0,大小.高度);
lineTo路径(0,20);
画布.drawShadow(路径,颜色.黑色,5,真);
画布.绘制路径(路径,绘制);
}
@凌驾
bool应重新绘制(自定义代理){
返回false;
}
}
我已连接主导航栏和导航栏
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,
        inactiveColor: CupertinoColors.systemGrey,
      ),
      PersistentBottomNavBarItem(
        icon: Icon(Icons.person_pin),
        title: ("Help"),
        activeColor: CupertinoColors.systemRed,
        inactiveColor: CupertinoColors.systemGrey,
      ),
      PersistentBottomNavBarItem(
        icon: Icon(Icons.local_activity),
        title: ("ProfileScreen"),
        activeColor: CupertinoColors.systemIndigo,
        inactiveColor: CupertinoColors.systemGrey,
      ),
 PersistentBottomNavBarItem(
        icon: Icon(Icons.shop_cart),
        title: ("Cart"),
        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,
      ),
    );
}