如果没有路由,如何路由自定义导航器?我尝试了许多替代方法,但无法同时路由两个按钮。只有主页有效。
代码如下:
BottomNavigationBarItem _bottomIcons(IconData icon) {
return BottomNavigationBarItem(icon: Icon(icon), title: Text(""));
}
@override
Widget build(BuildContext context) {
width = MediaQuery.of(context).size.width;
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
backgroundColor: LightColor.purple,
showSelectedLabels: false,
showUnselectedLabels: false,
selectedItemColor: Colors.white,
unselectedItemColor: LightColor.extraDarkPurple,
type: BottomNavigationBarType.fixed,
currentIndex: 1,
items: [
_bottomIcons(Icons.home),
_bottomIcons(Icons.arrow_back_ios),
],
onTap: (index) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => HomePage(),
),
);
},
),
答案 0 :(得分:0)
您可以尝试使用基于索引值的if条件导航到使用路由的两个不同页面。尝试下面提到的代码。
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: MyButton(),
));
}
class MyButton extends StatefulWidget {
@override
MyButtonState createState() {
return MyButtonState();
}
}
class MyButtonState extends State<MyButton> {
BottomNavigationBarItem _bottomIcons(IconData icon) {
return BottomNavigationBarItem(icon: Icon(icon), title: Text(""));
}
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.purple,
showSelectedLabels: false,
showUnselectedLabels: false,
selectedItemColor: Colors.white,
unselectedItemColor: Colors.blueAccent,
type: BottomNavigationBarType.fixed,
currentIndex: 1,
items: [
_bottomIcons(Icons.home),
_bottomIcons(Icons.arrow_back_ios),
],
onTap: (index) {
if(index == 0){
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => HomePage(),
),
);
}
else if(index == 1){
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => SecondPage(),
),
);
}
},
),
);
}
}
编码愉快!