๋ค๋น๊ฒ์ด์ ์์ ฏ
โข
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
๋ณต์ฌ
ํ๋ผ๋ฏธํฐ ์ ๋ฌ๊ณผ ํ๋ฉด ์ด๋
โข
push ํจ์๋ก ์ด๋
โข
pushNamed ํจ์๋ก ์ด๋
push ํจ์๋ก ์ด๋
๊ธฐ์กด ํ๋ฉด ์์, ์ง์ ํ ํ๋ฉด์ ์คํ ๊ตฌ์กฐ๋ก ์๊ณ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉฐ ์ด๋ํ๋ ๋ฐฉ๋ฒ
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => SecondScreen(data: "value")),
);
Navigator.pop(context);
Dart
๋ณต์ฌ
์ฌ๊ธฐ์๋ data ๋ผ๋ ํ๋ผ๋ฏธํฐ์ โvalueโ ๋ผ๋ ๊ฐ์ ํ๋ฉด์ ์ด๋ํ๋ฉด์ ์ ๋ฌํ๊ฒ ๋๋ค.
pushNamed ํจ์๋ก ์ด๋
๊ธฐ์กด ํ๋ฉด ์์, ์ง์ ํ ํ๋ฉด์ ์คํ ๊ตฌ์กฐ๋ก ์๊ณ , ๋งคํํ ๊ฒฝ๋ก๋ก ์ด๋ํ๋ ๋ฐฉ๋ฒ
1.
๊ฐ์ ํ๋ ์ ๋ฌํ๋ ๊ฒฝ์ฐ
2.
๊ฐ์ ์ฌ๋ฌ ๊ฐ ์ ๋ฌํ๋ ๊ฒฝ์ฐ
a.
๊ฐ์ฒด๋ก ์ ๋ฌ
b.
Map ์ผ๋ก ์ ๋ฌ
๊ฐ์ ํ๋ ์ ๋ฌํ๋ ๊ฒฝ์ฐ
โข
main.dart
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const FirstScreen(),
initialRoute: '/main',
routes: {
'/main' : (context) => MainScreen(),
'/home' : (context) => HomeScreen(),
'/user' : (context) => UserScreen(user: User(id: '', name: ''),),
'/community' : (context) => CommunityScreen(),
},
);
}
}
Dart
๋ณต์ฌ
โข
HomeScreen(/home) ์์ CommunityScreen(/community) ์ผ๋ก ์ด๋ํ๋ ๊ฒฝ์ฐ
Navigator.pushNamed(context, "/community", arguments: 'community');
Dart
๋ณต์ฌ
import 'package:flutter/material.dart';
class CommunityScreen extends StatelessWidget {
const CommunityScreen({super.key});
Widget build(BuildContext context) {
// ๋ฐ์ดํฐ ์ ๋ฌ๋ฐ๊ธฐ
String? data = ModalRoute.of(context)!.settings.arguments as String?;
return Scaffold(
appBar: AppBar(title: const Text("์ปค๋ฎค๋ํฐ"),),
body: Center(
child: Text(
"์ปค๋ฎค๋ํฐ : $data",
style: TextStyle(fontSize: 30.0),
)
),
bottomSheet: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: () {
// ๋ผ์ฐํ
๊ฒฝ๋ก๋ก ํ๋ฉด ์ด๋
// * arguments : ํ๋ฉด ์ด๋ ์ ์ ๋ฌํ ๋ฐ์ดํฐ ์ง์
// 1. ์คํ์ ์ ํ๋ฉด์ ์ถ๊ฐ
// Navigator.pushNamed(context, "/home", arguments: 'home');
// 2. ํ์ฌ ํ๋ฉด ์ ๊ฑฐ ํ, ์ ํ๋ฉด์ ์ถ๊ฐ
// Navigator.pop(context); // ํ์ฌ ํ๋ฉด ์คํ์์ ์ ๊ฑฐ
// Navigator.pushNamed(context, "/home", arguments: 'home');
// 3. ํ์ฌ ํ๋ฉด์ ์ ํ๋ฉด์ผ๋ก ๋์ฒด
Navigator.pushReplacementNamed(context, "/home");
},
child: const Text("ํ ํ๋ฉด")
),
ElevatedButton(
onPressed: () {
Navigator.pushReplacementNamed(context, "/community", arguments: 'community');
},
child: const Text("์ปค๋ฎค๋ํฐ")
),
],
),
),
);
}
}
Dart
๋ณต์ฌ
๊ฐ์ ์ฌ๋ฌ ๊ฐ ์ ๋ฌํ๋ ๊ฒฝ์ฐ
1.
๊ฐ์ฒด๋ก ์ ๋ฌ
2.
Map ์ผ๋ก ์ ๋ฌ
๊ฐ์ฒด๋ก ์ ๋ฌ
โข
HomeScreen(/home) ์์ CommunityScreen(/community) ์ผ๋ก ์ด๋ํ๋ ๊ฒฝ์ฐ
โฆ
๊ฐ์ฒด ์ ์
โฆ
๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ์ ๋ฌํ๋ฉด ์ด๋
โฆ
์ด๋ํ ํ๋ฉด์์ ์ ๋ฌ ๋ฐ๊ธฐ
โข
๊ฐ์ฒด ์ ์
class Community {
final int id;
final String name;
final String content;
Community({required this.id, required this.name, required this.content});
}
Dart
๋ณต์ฌ
โข
๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ์ ๋ฌํ๋ฉด ์ด๋
Navigator.pushNamed(context, "/community",
arguments: Community(
id: 1001,
name : 'aloha',
content : 'content'
)
);
Dart
๋ณต์ฌ
โข
์ด๋ํ ํ๋ฉด์์ ์ ๋ฌ ๋ฐ๊ธฐ
import 'package:flutter/material.dart';
class CommunityScreen extends StatelessWidget {
const CommunityScreen({super.key});
Widget build(BuildContext context) {
// ๋ฐ์ดํฐ ์ ๋ฌ๋ฐ๊ธฐ
Community? community = ModalRoute.of(context)!.settings.arguments as Community?;
return Scaffold(
appBar: AppBar(title: const Text("์ปค๋ฎค๋ํฐ"),),
body: Center(
child: Text(
"์ปค๋ฎค๋ํฐ : ${community.name}",
style: TextStyle(fontSize: 30.0),
)
),
...
Dart
๋ณต์ฌ
Map ์ผ๋ก ์ ๋ฌ
โข
HomeScreen(/home) ์์ CommunityScreen(/community) ์ผ๋ก ์ด๋ํ๋ ๊ฒฝ์ฐ
โฆ
Map ์์ฑํ์ฌ ์ ๋ฌํ๋ฉด ์ด๋
โฆ
์ด๋ํ ํ๋ฉด์์ ์ ๋ฌ ๋ฐ๊ธฐ
โข
Map ์์ฑํ์ฌ ์ ๋ฌํ๋ฉด ์ด๋
Navigator.pushNamed(context, "/community",
arguments: {
'id' : 1001,
'name' : 'aloha',
'content' : 'community'
}
);
Dart
๋ณต์ฌ
โข
์ด๋ํ ํ๋ฉด์์ ์ ๋ฌ ๋ฐ๊ธฐ
import 'package:flutter/material.dart';
class CommunityScreen extends StatelessWidget {
const CommunityScreen({super.key});
Widget build(BuildContext context) {
// ๋ฐ์ดํฐ ์ ๋ฌ๋ฐ๊ธฐ
final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
return Scaffold(
appBar: AppBar(title: const Text("์ปค๋ฎค๋ํฐ"),),
body: Center(
child: Text(
"์ปค๋ฎค๋ํฐ : ${args['name']}",
style: TextStyle(fontSize: 30.0),
)
),
...
Dart
๋ณต์ฌ