我只是想知道,我可以使bottomnavigationbar可重用吗,因为我在其他类似问题中看到的是他们使用屏幕列表,然后使用它制作的脚手架不能在我的情况下重用,我只想知道是这可能吗?
import 'package:conditional/HistoryScreen.dart';
import 'package:conditional/HomeScreen.dart';
import 'package:flutter/material.dart';
class NavBar extends StatefulWidget{
NavBar({Key? key}):super(key: key);
@override
_NavBar createState() => _NavBar();
}
class _NavBar extends State<NavBar>{
int currentIndex = 0;
@override
Widget build(BuildContext context){
return BottomNavigationBar(
currentIndex: currentIndex,
onTap: (index){
setState(() {
currentIndex = index;
});
switch(index){
case 0:
Navigator.push(context, MaterialPageRoute(builder: (context) => HomeScreen()));
break;
case 1:
Navigator.push(context, MaterialPageRoute(builder: (context) => HistoryScreen()), );
break;
}
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
activeIcon: Icon(Icons.home, color: currentIndex == 0 ? Colors.red : Colors.grey),
label: "Home"),
BottomNavigationBarItem(
icon:Icon(Icons.history),
activeIcon: Icon(Icons.history, color: currentIndex == 1 ? Colors.red : Colors.grey),
label: "History" )
],
selectedItemColor: Colors.red,
);
}
}
import 'package:conditional/NavBar.dart';
import 'package:flutter/material.dart';
class HomeScreen extends StatelessWidget{
HomeScreen({Key? key}):super(key: key);
@override
Widget build(BuildContext context){
return Scaffold(
body: Center(child: Text("This is Homescreen"),),
bottomNavigationBar: NavBar(),
);
}
}
import 'package:conditional/NavBar.dart';
import 'package:flutter/material.dart';
class HistoryScreen extends StatefulWidget{
HistoryScreen({Key? key}):super(key: key);
@override
_HistoryScreen createState()=> _HistoryScreen();
}
class _HistoryScreen extends State<HistoryScreen>{
@override
Widget build(BuildContext context){
return Scaffold(
body: Text("This is History Screen"),
bottomNavigationBar: NavBar(),
);
}
}
主要问题是,如果我单击历史按钮,主页按钮仍然是红色,我不知道问题出在哪里,但是导航器按预期正常工作,只是单击时的颜色,你们能帮我吗?
您可以通过将其封装在单独的小部件中来在 Flutter 中创建可重用的 BottomNavigationBar。
import 'package:flutter/material.dart';
class ReusableBottomNavigationBar extends StatelessWidget {
final int currentIndex;
final Function(int) onTap;
ReusableBottomNavigationBar({
@required this.currentIndex,
@required this.onTap,
});
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
currentIndex: currentIndex,
onTap: onTap,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
);
}
}
然后,您可以在任何要显示底部导航栏的屏幕中使用此 ReusableBottomNavigationBar 小部件。这是一个例子:
import 'package:flutter/material.dart';
import 'package:your_app/reusable_bottom_navigation_bar.dart';
class MyScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Screen'),
),
body: Center(
child: Text('This is my screen content'),
),
bottomNavigationBar: ReusableBottomNavigationBar(
currentIndex: 0,
onTap: (index) {
// Handle bottom navigation bar item taps
},
),
);
}
}
要实现您想要的,您可以使用 IndexedStacked 小部件。你的代码将如下所示:
class AppIndex extends StatefulWidget {
const AppIndex({super.key});
@override
State<AppIndex> createState() => _AppIndexState();
}
class _AppIndexState extends State<AppIndex> {
//create a list of your pages
List<Widget> pages = [
HomeScreen(),
HistoryScreen(),
];
int currentIndex = 0;
//create a function to increament tab
void incrementTab(index) {
setState(() {
currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
//use an index stacked widget
body: IndexedStack(
index: currentIndex,
children: pages,
),
// your bottomNavigationBar would look like this
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentIndex,
onTap: incrementTab,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
activeIcon: Icon(Icons.home, color: currentIndex == 0 ? Colors.red : Colors.grey),
label: "Home"),
BottomNavigationBarItem(
icon:Icon(Icons.history),
activeIcon: Icon(Icons.history, color: currentIndex == 1 ? Colors.red : Colors.grey),
label: "History" )
],
selectedItemColor: Colors.red,
)
);
}
}