如何解决移动到其他页面时无法保持底部导航栏颤动
我是初学者,我试过这个教程 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 举报,一经查实,本站将立刻删除。