只需将 currentIndex 参数添加到您的导航器类。您可以使用以下命令导航到特定页面:
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
NavigationEngine(currentIndex: // index of the page you want to go,),
),
),
您的 BottomNavigationBar 类应如下所示:
class NavigationEngine extends StatefulWidget {
final int currentIndex;
const NavigationEngine({Key key, @required this.currentIndex})
: super(key: key);
@override
_NavigationEngineState createState() => _NavigationEngineState();
}
class _NavigationEngineState extends State<NavigationEngine> {
int _currentIndex;
@override
void initState() {
_currentIndex = widget.currentIndex;
super.initState();
}
void _onItemTapped(int index) {
setState(() {
_currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
showSelectedLabels: false,
showUnselectedLabels: false,
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
selectedItemColor: Theme.of(context).accentColor,
unselectedItemColor: Theme.of(context).canvasColor,
type: BottomNavigationBarType.fixed,
elevation: 0,
items: [
// your navigation bar items
BottomNavigationBarItem(icon: Icon(Icons.home), label: "home"),
],
currentIndex: _currentIndex,
onTap: _onItemTapped,
),
body: IndexedStack(
index: _currentIndex,
children: [
// your navigation pages
],
),
);
}
}