在 Flutter 中使用 NavigationRail 和 BottomNavigationBar
在Flutter中,你可以创建一个使用NavigationRail
和BottomNavigationBar
的应用,其中NavigationRail
用于侧边导航,而BottomNavigationBar
用于底部导航。以下是一个简单的示例代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
List<Widget> _buildPages() {
return [
Icon(Icons.home),
Icon(Icons.favorite),
Icon(Icons.shopping_cart),
Icon(Icons.person),
];
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: NavigationRail(
selectedIndex: _selectedIndex,
onDestinationSelected: _onItemTapped,
leading: NavigationRailLogo(),
destinations: [
NavigationRailDestination(
icon: Icon(Icons.home),
label: Text('Home'),
),
NavigationRailDestination(
icon: Icon(Icons.favorite),
label: Text('Favorites'),
),
NavigationRailDestination(
icon: Icon(Icons.shopping_cart),
label: Text('Cart'),
),
NavigationRailDestination(
icon: Icon(Icons.person),
label: Text('Profile'),
),
],
body: const Text('Select a destination'),
),
bottomNavigationBar: BottomNavigationBar(
selectedItemColor: Colors.blue,
unselectedItemColor: Colors.grey,
selectedIconTheme: IconThemeData(size: 24),
unselectedIconTheme: IconThemeData(size: 22),
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
label: 'Favorites',
),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_cart),
label: 'Cart',
),
BottomNavigationBarItem(
icon: Icon(Icons.perso
评论已关闭