- Goals, requirements, initial setup, and overview: Link
- Login-page branching with FlutterSecureStorage: Link
This post assumes you have configured the project by following those links in order. It was written for Flutter 3.24.
๐ฆ lib
โฃ ๐ model // ๋ฐ์ดํฐ ๋ณด๊ดํ๋ ๋ชจ๋ธ ํด๋(์ด๋ฒ ํฌ์คํ
์์๋ ์ฌ์ฉํ์ง ์์)
โฃ ๐ view // ํ๋ฉด์ ๊ตฌ์ฑํ๋ ๋ทฐ ํด๋
โ โฃ ๐ tabs // ํญ 4๊ฐ๋ฅผ ๊ตฌ์ฑํ๋ ํ๋ฉด
โ โ โฃ ๐ home_screen.dart
โ โ โฃ ๐ notifications_screen.dart
โ โ โฃ ๐ profile_screen.dart
โ โ โ ๐ search_screen.dart
โ โฃ ๐ login_page.dart
โ โ ๐ main_page.dart
โฃ ๐ viewModel // ๋ฐ์ดํฐ์ ํ๋ฉด์ ์๋ viewModel ํด๋
โ โ ๐ auth_service.dart
โ ๐ main.dartimport 'package:flutter/material.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: Text('Home Screen', style: TextStyle(fontSize: 24)),
);
}
}import 'package:flutter/material.dart';
class SearchScreen extends StatelessWidget {
const SearchScreen({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: Text('Search Screen', style: TextStyle(fontSize: 24)),
);
}
}import 'package:flutter/material.dart';
class NotificationsScreen extends StatelessWidget {
const NotificationsScreen({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: Text('Notifications Screen', style: TextStyle(fontSize: 24)),
);
}
}import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:navigationbar_example/view/login_page.dart';
import 'package:navigationbar_example/viewModel/auth_service.dart';
class ProfileScreen extends StatelessWidget {
final AuthService _authService = AuthService();
ProfileScreen({super.key}); // AuthService ์ธ์คํด์ค ์์ฑ
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Profile Page'), // ํ๋กํ ํ๋ฉด ์ ๋ชฉ
actions: [
IconButton(
onPressed: () async {
// ๋ก๊ทธ์์ ์ ์ํ ์ญ์
await _authService.logout();
// ๋ก๊ทธ์์ ํ ๋ก๊ทธ์ธ ํ์ด์ง๋ก ์ด๋
Get.offAll(() => LoginPage());
},
icon: const Icon(Icons.logout), // ๋ก๊ทธ์์ ๋ฒํผ ์์ด์ฝ
),
],
),
body: const Center(
child: Text('Profile Screen', style: TextStyle(fontSize: 24)),
),
);
}
}import 'package:flutter/material.dart';
import 'package:navigationbar_example/view/tabs/home_screen.dart';
import 'package:navigationbar_example/view/tabs/notifications_screen.dart';
import 'package:navigationbar_example/view/tabs/profile_screen.dart';
import 'package:navigationbar_example/view/tabs/search_screen.dart';
class MainPage extends StatefulWidget {
const MainPage({super.key});
@override
MainPageState createState() => MainPageState();
}
class MainPageState extends State<MainPage> {
int _currentIndex = 0; // ํ์ฌ ์ ํ๋ ํญ์ ์ธ๋ฑ์ค
// ๊ฐ ํญ์์ ๋ณด์ฌ์ค ํ๋ฉด ๋ฆฌ์คํธ
final List<Widget> _pages = [
const HomeScreen(), // ์ฒซ ๋ฒ์งธ ํญ ํ๋ฉด
const SearchScreen(), // ๋ ๋ฒ์งธ ํญ ํ๋ฉด
const NotificationsScreen(), // ์ธ ๋ฒ์งธ ํญ ํ๋ฉด
ProfileScreen(), // ๋ค ๋ฒ์งธ ํญ ํ๋ฉด
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: _pages[_currentIndex], // ํ์ฌ ์ ํ๋ ํญ์ ํด๋นํ๋ ํ๋ฉด ํ์
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex, // ์ ํ๋ ํญ ์ธ๋ฑ์ค
onTap: (index) {
setState(() {
_currentIndex = index; // ํญ ์ ํ ์ ์ํ ์
๋ฐ์ดํธ
});
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Search',
),
BottomNavigationBarItem(
icon: Icon(Icons.notifications),
label: 'Notifications',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
selectedItemColor: Colors.blue, // ์ ํ๋ ์์ดํ
์์
unselectedItemColor: Colors.grey, // ์ ํ๋์ง ์์ ์์ดํ
์์
),
);
}
}