TabBars และเมนูด้านข้าง
พัฒนารูปแบบการนำทางที่ใช้บ่อย เช่น TabBars สำหรับมุมมองหลายแบบ และเมนูด้านข้างสำหรับการนำทางด้านข้าง
TabBars และเมนูด้านข้าง เป็นบทเรียน Flutter Mobile Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 และเมนูด้านข้าง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flutter Mobile Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “TabBars และเมนูด้านข้าง”
พัฒนารูปแบบการนำทางที่ใช้บ่อย เช่น TabBars สำหรับมุมมองหลายแบบ และเมนูด้านข้างสำหรับการนำทางด้านข้าง คุณปฏิบัติ Flutter Mobile Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flutter Mobile Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flutter Mobile Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “TabBars และเมนูด้านข้าง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flutter Mobile Development นี้ได้ไหม
ได้ บทเรียน Flutter Mobile Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การนำทางระหว่างหน้าเบื้องต้น
- เส้นทางที่มีชื่อและอาร์กิวเมนต์
- TabBars และเมนูด้านข้าง
- ลิงก์เจาะจงและการนำทางด้วย URL