BottomNavigationBar
BottomNavigationBar๋ Flutter์์ ์ฑ์ ํ๋จ์ ์์นํ๋ ๋ด๋น๊ฒ์ด์
๋ฐ๋ฅผ ๊ตฌํํ๋ ์์ ฏ์
๋๋ค.
์ฃผ์ ํน์ง
โข
ํ๋จ์ ๊ณ ์ ๋ ๋ด๋น๊ฒ์ด์
๋ฐ ์ ๊ณต
โข
์์ด์ฝ๊ณผ ๋ผ๋ฒจ ์กฐํฉ ์ฌ์ฉ ๊ฐ๋ฅ
โข
์ ํ๋ ํญ๋ชฉ ๊ฐ์กฐ ํ์
โข
์ต์ 2๊ฐ์์ ์ต๋ 5๊ฐ๊น์ง์ ํญ๋ชฉ ์ง์
โข
์ ๋๋ฉ์ด์
ํจ๊ณผ ๋ด์ฅ
โข
BottomNavigationBar ์์ ์ฝ๋
โข
BottomNavigationBar ์์ ์ฝ๋
โข
main.dart
โข
screens
โฆ
home_screen.dart
main.dart
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
Widget build(BuildContext context) {
return MaterialApp(
title: '๋ก๊ทธ์ธ ์ฑ',
theme: ThemeData(
useMaterial3: true,
),
debugShowCheckedModeBanner: false,
initialRoute: '/',
routes: {
'/': (context) => HomeScreen(),
},
);
}
}
Dart
๋ณต์ฌ
home_screen.dart
class HomeScreen extends StatelessWidget {
HomeScreen({super.key});
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: const Text("Home"),
),
body: Center(
child: Column(
children: [],
),
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: "๋ฉ์ธ",
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: "๊ฒ์",
),
BottomNavigationBarItem(
icon: Icon(Icons.category),
label: "์ํ",
),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_bag),
label: "์ฅ๋ฐ๊ตฌ๋",
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: "๋ง์ด",
),
],
),
);
}
}
Dart
๋ณต์ฌ
๊ณตํต ์์ ฏ์ผ๋ก ์ ์ํ๊ธฐ
BottomNavigationBar๋ฅผ ๊ณตํต ์์ ฏ์ผ๋ก ์ ์ํ๋ฉด ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ณ ์ ์ง๋ณด์๊ฐ ์ฉ์ดํด์ง๋๋ค.
common_bottom_navigation_bar.dart
import 'package:flutter/material.dart';
class CommonBottomNavigationBar extends StatelessWidget {
final int currentIndex;
final Function(int) onTap;
const CommonBottomNavigationBar({
Key? key,
required this.currentIndex,
required this.onTap,
}) : super(key: key);
Widget build(BuildContext context) {
return BottomNavigationBar(
currentIndex: currentIndex,
onTap: onTap,
type: BottomNavigationBarType.fixed,
selectedItemColor: Theme.of(context).primaryColor,
unselectedItemColor: Colors.grey,
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: "๋ฉ์ธ",
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: "๊ฒ์",
),
BottomNavigationBarItem(
icon: Icon(Icons.category),
label: "์ํ",
),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_bag),
label: "์ฅ๋ฐ๊ตฌ๋",
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: "๋ง์ด",
),
],
);
}
}
Dart
๋ณต์ฌ
๊ณตํต ์์ ฏ ์ฌ์ฉํ๊ธฐ
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int _currentIndex = 0;
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Home"),
),
body: Center(
child: Column(
children: [],
),
),
bottomNavigationBar: CommonBottomNavigationBar(
currentIndex: _currentIndex,
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
),
);
}
}
Dart
๋ณต์ฌ
์ฅ์
โข
์ฝ๋ ์ฌ์ฌ์ฉ์ฑ ํฅ์
โข
์ผ๊ด๋ ๋์์ธ ์ ์ง
โข
์ ์ง๋ณด์ ์ฉ์ด
โข
์ํ ๊ด๋ฆฌ ํธ์์ฑ
์ด๋ ๊ฒ ๊ณตํต ์์ ฏ์ผ๋ก ๋ถ๋ฆฌํ๋ฉด ์ฌ๋ฌ ํ๋ฉด์์ ๋์ผํ BottomNavigationBar๋ฅผ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.