一般我們使用drawer into appBar(position drawer/endDrawer)。我必須使用主頁,設定將抽屜功能設定到底部導航欄專案中。這 3 個專案將適用于所有頁面,并且必須顯示這三 (3) 個專案的選定索引。我如何才能做到這一點。
class BottomNavBar extends StatefulWidget {
@override
_BottomNavBarState createState() => _BottomNavBarState();
}
class _BottomNavBarState extends State<BottomNavBar> {
int _selectedIndex = 1;
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
currentIndex: _selectedIndex,
onTap: onTabTapped,
//for more than three items
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(processIcon),
label: 'My Task',
),
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(settingIcon),
label: 'Setting',
),
BottomNavigationBarItem(
icon: Icon(menuIcon),
label: 'Menu',
),
],
);
}
void onTabTapped(int index) {
setState(() {
_selectedIndex = index;
print('index ${index}');
});
}
}
在這里,抽屜將導航到選單中。
uj5u.com熱心網友回復:
您可以使用GlobalKey 上Scaffold,并用它來打開的抽屜。
void onTabTapped(int index) {
setState(() {
_selectedIndex = index;
if (_selectedIndex == 3 && _scaffoldKey.currentState != null) {
_scaffoldKey.currentState!.openDrawer();
} });
}
class _BottomNavBarState extends State<BottomNavBar> {
int _selectedIndex = 1;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: Text("B $_selectedIndex"),
drawer: const Drawer(
backgroundColor: Colors.cyanAccent,
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/396904.html
