λ€λΉκ²μ΄μ μμ ―
β’
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
볡μ¬