TabBars 및 서랍 메뉴
여러 화면을 위한 TabBars와 측면 탐색 메뉴를 위한 Drawers처럼 일반적인 탐색 패턴을 구현합니다.
TabBars 및 서랍 메뉴은(는) CoddyKit의 무료 Flutter Mobile Development 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Flutter Mobile Development 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Flutter Mobile Development 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Intro to Navigation Patterns
Flutter offers powerful patterns for app navigation. Today, we explore two common ones: TabBars and Drawers.
These widgets help users navigate between different sections of your app smoothly and intuitively.
- TabBars are great for switching between related views.
- Drawers provide a side menu for global navigation or settings.
What are TabBars?
A TabBar is a horizontal row of tabs, often found at the top or bottom of a screen. Each tab represents a different view or category of content.
When a user taps a tab, the content below it changes without leaving the current screen.
Building a Basic TabBar
To use tabs, you often wrap your Scaffold with a DefaultTabController. This controller manages the selected tab.
Then, place a TabBar widget inside your AppBar's bottom property or as a BottomNavigationBar.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3, // Number of tabs
child: Scaffold(
appBar: AppBar(
title: const Text('My Tab App'),
bottom: const TabBar(
tabs: [
Tab(icon: Icon(Icons.home), text: 'Home'),
Tab(icon: Icon(Icons.settings), text: 'Settings'),
Tab(icon: Icon(Icons.info), text: 'Info'),
],
),
),
body: const TabBarView(
children: [
Center(child: Text('Home Content')),
Center(child: Text('Settings Content')),
Center(child: Text('Info Content')),
],
),
),
),
);
}
}TabBarView in Action
The TabBarView widget works hand-in-hand with TabBar. It displays the content corresponding to the currently selected tab.
Make sure the number of children in TabBarView matches the length of your DefaultTabController and the number of Tab widgets.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3, // Must match the number of tabs
child: Scaffold(
appBar: AppBar(
title: const Text('Tab Views'),
bottom: const TabBar(
tabs: [
Tab(text: 'Explore'),
Tab(text: 'Favorites'),
Tab(text: 'Profile'),
],
),
),
body: TabBarView( // Content for each tab
children: [
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(Icons.explore, size: 80, color: Colors.blue),
Text('Explore new content!', style: TextStyle(fontSize: 20)),
],
),
),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(Icons.favorite, size: 80, color: Colors.red),
Text('Your favorite items.', style: TextStyle(fontSize: 20)),
],
),
),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(Icons.person, size: 80, color: Colors.green),
Text('Your user profile.', style: TextStyle(fontSize: 20)),
],
),
),
],
),
),
),
);
}
}Styling Your Tabs
You can customize the look of your TabBar. For example, change colors, indicator styles, or label styles.
The indicatorColor and labelColor properties are common ways to match your app's theme.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: const Text('Custom Tabs'),
bottom: TabBar(
indicatorColor: Colors.yellowAccent, // Custom indicator color
labelColor: Colors.white, // Color for selected tab
unselectedLabelColor: Colors.grey[400], // Color for unselected tabs
tabs: const [
Tab(icon: Icon(Icons.call), text: 'Calls'),
Tab(icon: Icon(Icons.chat), text: 'Chats'),
],
),
),
body: const TabBarView(
children: [
Center(child: Text('Calls Log')),
Center(child: Text('Chat Messages')),
],
),
),
),
);
}
}What is a Drawer?
A Drawer is a panel that slides out from the side of the screen, typically from the left. It's often used for main navigation links or global settings.
Drawers are perfect for apps with many sections that don't fit into a TabBar or BottomNavigationBar.
Adding a Side Drawer
To add a drawer, simply include a Drawer widget as a child of your Scaffold. Flutter automatically adds a menu icon to your AppBar.
Inside the Drawer, you'll typically use a ListView to hold multiple ListTile widgets, representing navigation items.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('My Drawer App'),
),
drawer: Drawer( // The Drawer widget
child: ListView(
padding: EdgeInsets.zero, // Remove padding
children: <Widget>[
const DrawerHeader( // Top part of the drawer
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(
'App Menu',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
ListTile( // A navigation item
leading: const Icon(Icons.message),
title: const Text('Messages'),
onTap: () {
// Handle tap
Navigator.pop(context); // Close the drawer
},
),
ListTile(
leading: const Icon(Icons.account_circle),
title: const Text('Profile'),
onTap: () {
// Handle tap
Navigator.pop(context);
},
),
ListTile(
leading: const Icon(Icons.settings),
title: const Text('Settings'),
onTap: () {
// Handle tap
Navigator.pop(context);
},
),
],
),
),
body: const Center(
child: Text('Pull from left to open drawer!'),
),
),
);
}
}Handling Drawer Taps
When a user taps an item in the drawer, you usually want to perform an action, like navigating to a new screen or updating content.
Remember to call Navigator.pop(context) after an item is tapped to close the drawer, providing a smooth user experience.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Drawer Actions'),
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
const DrawerHeader(
decoration: BoxDecoration(color: Colors.green),
child: Text('Navigation', style: TextStyle(color: Colors.white)),
),
ListTile(
leading: const Icon(Icons.home),
title: const Text('Go Home'),
onTap: () {
// Close drawer, then navigate (or just close if already on home)
Navigator.pop(context);
// Example: Navigator.push(context, MaterialPageRoute(builder: (context) => HomeScreen()));
},
),
ListTile(
leading: const Icon(Icons.bookmark),
title: const Text('View Bookmarks'),
onTap: () {
Navigator.pop(context); // Close drawer
// Show a snackbar as an example action
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Viewing Bookmarks!'))
);
},
),
],
),
),
body: const Center(
child: Text('Try opening the drawer and tapping an item.'),
),
),
);
}
}Customizing Your Drawer
Drawers can be highly customized. You can add user profile info, app version, or even search bars.
- Use
UserAccountsDrawerHeaderfor a pre-built header with user info. - Keep the number of items manageable to avoid clutter.
- Group related items logically using dividers.
TabBar vs. Drawer
Which of the following scenarios is best suited for using a Flutter TabBar?
TabBars & Drawers Recap
Great job! You've learned how to implement two essential navigation patterns:
- TabBars for switching between related views using
DefaultTabController,TabBar, andTabBarView. - Drawers for side navigation menus using the
Drawerwidget andListTiles.
Choose the right pattern based on your app's complexity and user flow. Keep exploring to build more intuitive UIs!
자주 묻는 질문
“TabBars 및 서랍 메뉴” 강의는 무료인가요?
네 — “TabBars 및 서랍 메뉴” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Flutter Mobile Development 강의 전체를 잠금 해제할 수 있습니다. Flutter Mobile Development 강의에는 총 4개의 강의가 포함되어 있습니다.
“TabBars 및 서랍 메뉴”에서 뭘 배우나요?
여러 화면을 위한 TabBars와 측면 탐색 메뉴를 위한 Drawers처럼 일반적인 탐색 패턴을 구현합니다. 브라우저에서 직접 실행하는 실습 코드로 Flutter Mobile Development을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Flutter Mobile Development을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Flutter Mobile Development은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“TabBars 및 서랍 메뉴” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Flutter Mobile Development 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Flutter Mobile Development 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 기본 페이지 탐색
- 이름이 지정된 경로 및 인수
- TabBars 및 서랍 메뉴
- 딥 링크와 URL 탐색