我是初学者,我尝试了本教程 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,
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;
}
}
我在这里附上了主要和导航栏文件!请帮忙!