import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MainNavigationScreen(),
);
}
}
class MainNavigationScreen extends StatefulWidget {
const MainNavigationScreen({super.key});
State<MainNavigationScreen> createState() => _MainNavigationScreenState();
}
class _MainNavigationScreenState extends State<MainNavigationScreen> {
int _selectedIndex = 0;
final screens = [
const Center(
child: Text('Home'),
),
const Center(
child: Text('Search'),
)
];
void _onTap(int index) {
setState(() {
_selectedIndex = index;
});
}
Widget build(BuildContext context) {
return Scaffold(
body: screens[_selectedIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedIndex,
onTap: _onTap,
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: "Home",
tooltip: "What are you?",
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: "Search",
tooltip: "What are you?",
),
],
),
);
}
}
Material 3 design
bottomNavigationBar: NavigationBar(
// NavigationBar is Material 3 design
selectedIndex: _selectedIndex,
onDestinationSelected: _onTap,
destinations: [
NavigationDestination(
icon: Icon(Icons.home),
label: "Home",
),
NavigationDestination(
icon: Icon(Icons.search),
label: "Search",
),
],
),
Material 3 design을 한다면, BottomNavigationBar 대신 NavigationBar를 사용한다.
Cupertino design
bottomNavigationBar: CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: const [
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.house),
label: "Home",
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.search),
label: "Search",
),
],
),
tabBuilder: (context, index) => screens[index],
),