Search

λ„€λΉ„κ²Œμ΄μ…˜ μœ„μ ―

λ„€λΉ„κ²Œμ΄μ…˜ μœ„μ ―

β€’
Navigator
β€’
Routes
β€’
BottomNavigationBar

Navigator

μ•± λ‚΄μ—μ„œ ν™”λ©΄ κ°„ 이동을 κ΄€λ¦¬ν•˜λŠ” 클래슀
Navigator ν΄λž˜μŠ€λŠ” μ•± λ‚΄μ—μ„œ ν™”λ©΄ κ°„ 이동을 κ΄€λ¦¬ν•˜λŠ” 역할을 ν•©λ‹ˆλ‹€. μƒˆλ‘œμš΄ ν™”λ©΄μœΌλ‘œ μ΄λ™ν•˜κ±°λ‚˜ 이전 ν™”λ©΄μœΌλ‘œ λŒμ•„κ°€λŠ” λ“±μ˜ λ„€λΉ„κ²Œμ΄μ…˜μ„ μ œμ–΄ν•  λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€.

문법

Navigator.push( context, MaterialPageRoute(builder: (context) => SecondScreen()), ); Navigator.pop(context);
Dart
볡사

μ˜ˆμ‹œ

β€’
main.dart
β€’
first_screen.dart
β€’
second_screen.dart
β€’
파일 ꡬ쑰
- lib - screens - first_screen.dart - second_screen.dart - main.dart
Dart
볡사

main.dart

import 'package:flutter/material.dart'; import 'screens/first_screen.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp( home: FirstScreen(), ); } }
Dart
볡사

first_screen.dart

import 'package:flutter/material.dart'; import 'second_screen.dart'; class FirstScreen extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('First Screen'), ), body: Center( child: ElevatedButton( onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => SecondScreen()), ); }, child: Text('Go to Second Screen'), ), ), ); } }
Dart
볡사

second_screen.dart

import 'package:flutter/material.dart'; class SecondScreen extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Second Screen'), ), body: Center( child: ElevatedButton( onPressed: () { Navigator.pop(context); }, child: Text('Go back to First Screen'), ), ), ); } }
Dart
볡사

Routes

μ‚¬μš©λ˜λŠ” 각 화면에 λŒ€ν•œ 경둜λ₯Ό μ •μ˜ν•˜λŠ” 속성

RoutesλŠ” μ•±μ—μ„œ μ‚¬μš©λ˜λŠ” 각 화면에 λŒ€ν•œ 경둜λ₯Ό μ •μ˜ν•˜λŠ” 역할을 ν•©λ‹ˆλ‹€. MaterialApp의 routes 속성에 맡 ν˜•νƒœλ‘œ 경둜λ₯Ό μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

문법

MaterialApp( routes: { '/home': (context) => HomeScreen(), '/profile': (context) => ProfileScreen(), '/settings': (context) => SettingsScreen(), }, );
Dart
볡사

μ˜ˆμ‹œ

β€’
main.dart
β€’
home_screen.dart
β€’
profile_screen.dart
β€’
settings_screen.dart
β€’
파일 ꡬ쑰
- lib - screens - home_screen.dart - profile_screen.dart - settings_screen.dart - main.dart
Dart
볡사

main.dart

import 'package:flutter/material.dart'; import 'screens/home_screen.dart'; import 'screens/profile_screen.dart'; import 'screens/settings_screen.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp( initialRoute: '/home', routes: { '/home': (context) => HomeScreen(), '/profile': (context) => ProfileScreen(), '/settings': (context) => SettingsScreen(), }, ); } }
Dart
볡사

home_screen.dart

import 'package:flutter/material.dart'; class HomeScreen extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home Screen'), ), body: Center( child: Text('This is the Home Screen'), ), ); } }
Dart
볡사

profile_screen.dart

import 'package:flutter/material.dart'; class ProfileScreen extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Profile Screen'), ), body: Center( child: Text('This is the Profile Screen'), ), ); } }
Dart
볡사

settings_screen.dart

import 'package:flutter/material.dart'; class SettingsScreen extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Settings Screen'), ), body: Center( child: Text('This is the Settings Screen'), ), ); } }
Dart
볡사

BottomNavigationBar

ν™”λ©΄ ν•˜λ‹¨μ— λ‚˜νƒ€λ‚˜λŠ” λ„€λΉ„κ²Œμ΄μ…˜ λ°”
BottomNavigationBarλŠ” ν™”λ©΄ ν•˜λ‹¨μ— λ‚˜νƒ€λ‚˜λŠ” λ„€λΉ„κ²Œμ΄μ…˜ λ°”λ‘œ, μ—¬λŸ¬ 개의 화면을 νƒ­μœΌλ‘œ κ΅¬μ„±ν•˜κ³  각 탭을 λˆŒλ €μ„ λ•Œ ν•΄λ‹Ή ν™”λ©΄μœΌλ‘œ 이동할 수 μžˆλ„λ‘ ν•©λ‹ˆλ‹€.

문법

Scaffold( body: _selectedScreen, // ν˜„μž¬ μ„ νƒλœ 화면이 ν‘œμ‹œλ  λΆ€λΆ„ bottomNavigationBar: BottomNavigationBar( items: [ BottomNavigationBarItem( icon: Icon(Icons.home), label: 'Home', ), BottomNavigationBarItem( icon: Icon(Icons.person), label: 'Profile', ), BottomNavigationBarItem( icon: Icon(Icons.settings), label: 'Settings', ), ], currentIndex: _selectedIndex, // ν˜„μž¬ μ„ νƒλœ νƒ­μ˜ 인덱슀 onTap: _onTabTapped, // 탭이 λˆŒλ Έμ„ λ•Œ ν˜ΈμΆœλ˜λŠ” 콜백 ν•¨μˆ˜ ), );
Dart
볡사

μ˜ˆμ‹œ

β€’
main.dart
β€’
home_screen.dart
β€’
profile_screen.dart
β€’
settings_screen.dart
β€’
파일 ꡬ쑰
- lib - screens - home_screen.dart - profile_screen.dart - settings_screen.dart - main.dart
Dart
볡사

main.dart

import 'package:flutter/material.dart'; import 'screens/home_screen.dart'; import 'screens/profile_screen.dart'; import 'screens/settings_screen.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { // λ³€μˆ˜ μ„ μ–Έ Widget _selectedScreen = HomeScreen(); int _selectedIndex = 0; // 탭이 λˆŒλ Έμ„ λ•Œ ν˜ΈμΆœλ˜λŠ” ν•¨μˆ˜ void _onTabTapped(int index) { setState(() { _selectedIndex = index; // μ„ νƒλœ 탭에 따라 ν™”λ©΄ μ—…λ°μ΄νŠΈ if (_selectedIndex == 0) { _selectedScreen = HomeScreen(); } else if (_selectedIndex == 1) { _selectedScreen = ProfileScreen(); } else if (_selectedIndex == 2) { _selectedScreen = SettingsScreen(); } }); } Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: _selectedScreen, bottomNavigationBar: BottomNavigationBar( items: [ BottomNavigationBarItem( icon: Icon(Icons.home), label: 'Home', ), BottomNavigationBarItem( icon: Icon(Icons.person), label: 'Profile', ), BottomNavigationBarItem( icon: Icon(Icons.settings), label: 'Settings', ), ], currentIndex: _selectedIndex, onTap: _onTabTapped, ), ), ); } }
Dart
볡사

home_screen.dart

import 'package:flutter/material.dart'; class HomeScreen extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home Screen'), ), body: Center( child: Text('This is the Home Screen'), ), ); } }
Dart
볡사

profile_screen.dart

import 'package:flutter/material.dart'; class ProfileScreen extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Profile Screen'), ), body: Center( child: Text('This is the Profile Screen'), ), ); } }
Dart
볡사

settings_screen.dart

import 'package:flutter/material.dart'; class SettingsScreen extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Settings Screen'), ), body: Center( child: Text('This is the Settings Screen'), ), ); } }
Dart
볡사