๐ Rout
๋จ์ํ ์ค๋งํธํฐ์์์ ๋ณด์ฌ์ง๋ ํ๋์ ํ์ด์ง ํน์ ํ๋ฉด
๐งญ Navigator
๋ผ์ฐํฐ(๋ชจ๋ ์ฑํ์ด์ง)๋ฅผ ๋ค๋น๊ฒ์ดํฐ ์์ ฏ์ด ๊ด๋ฆฌํ๋ฉฐ, ์คํ ๊ตฌ์กฐ๋ก ๋ผ์ฐํฐ ๊ฐ์ฒด๋ค์ ๊ด๋ฆฌ
๋ค๋น๊ฒ์ดํฐ๋ ์ฑ ํ์ด์ง์ ๋ํ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ํด ๋ฐ์ดํฐ๋ฅผ ์ฐจ๊ณก์ฐจ๊ณก ์๋ ์คํ ์๋ฃ๊ตฌ์กฐ ์ฌ์ฉ
์คํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๊ด๋ฆฌ ์ํด push, pop ๋ฉ์๋ ์ ๊ณต
๋ผ์ฐํฐ๋ ๋ฐ๋์ MaterialAPP ์๋์ child ๋ก ์์ฑ๋์ด์ผํจ
์์ ฏํธ๋ฆฌ์ ๋ชจ๋ ์์ ฏ์ MaterialApp์ด ๊ฐ์ธ๊ณ ์์ด์ผํจ
๋ค์ด๊ฒ์ดํฐ ์์ ฏ์ ๋ถ๋ฌ์ด
๋ช๊ฐ์ง ์ค์ ํ์
ํ๋ฌํฐ๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํด์ฃผ๋ MaterialPageRout() ์
builder(์ด๋ค ์์ ฏ์ด ๋ผ์ฐํฐ์ ๋์์ ๋ฐ์ ์์ฑ๋์ด์ผํ ์ง๋ฅผ ์ ์ํ๋ ๊ฒ) ์ ๋ถ๋ฌ์ค๊ณ
-> BuildContext ์ context ๋ฅผ ์ธ์๋ก ๋ฐ๊พธ๊ณ , SecondPage ๋ฅผ ๋ฆฌํดํด์ค
builder ์ ์ผ์ข
์ ์์ ์ฅ์น
secont page -> firstpage ์ด๋
: pop ์ ํด์ค
SecondPage ์ BuildContext ๋ฅผ ์ธ์๋ก ๋ฐ์ ์ด๋ฅผ ์ญ์ ํจ.
import 'package:flutter/material.dart';
void main() => runApp(MyAPP());
class MyAPP extends StatelessWidget {
const MyAPP({super.key});
Widget build(BuildContext context) {
return MaterialApp(
home: FirstPage(),
);
}
}
class FirstPage extends StatelessWidget {
const FirstPage({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First page'),
),
body: Center(
child: ElevatedButton(
child: Text('Go to the Second Page'),
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (BuildContext context) {
return SecondPage();
}));
},
),
),
);
}
}
class SecondPage extends StatelessWidget {
const SecondPage({super.key});
Widget build(BuildContext context2) {
return Scaffold(
appBar: AppBar(
title: Text('Second page'),
),
body: Center(
child: ElevatedButton(
child: Text('Go to the First Page'),
onPressed: () {
Navigator.pop(context2);
},
),
),
);
}
}
initailRoute
-> ๋ฉํฐ ํ์ด์ง ๋ผ์ฐํธํ ๋ ํ๋ฉด์ ์ ์ผ ์ฒ์ ์ถ๋ ฅ๋๋ ๋ผ์ฐํธ๋ฅผ ๋ถ๋ฌ์ผํค๋ ์ญํ
routes argument๋ ์ด๋ํ ํ์ด์ง๋ค์ ์ด๋ฆ ์ง์ ,
string๊ณผ Widget ์ด Map ํํ๋ก ํ์์ผ๋ก ์ด๋ฃจ์ด์ ธ์ผํจ
import 'package:flutter/material.dart';
import 'package:navigator/ScreenA.dart';
import 'package:navigator/ScreenB.dart';
import 'package:navigator/ScrennC.dart';
void main() => runApp(MyAPP());
class MyAPP extends StatelessWidget {
const MyAPP({super.key});
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => ScreenA(),
'/b': (context) => ScreenB(),
'/c': (context) => ScreenC(),
},
);
}
}
import 'package:flutter/material.dart';
import 'package:navigator/main.dart';
void main() => runApp(ScreenA());
class ScreenA extends StatelessWidget {
const ScreenA({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ScreenA'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/b');
},
child: Text('Go to ScreenB'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.redAccent,
),
),
ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/c');
},
child: Text('Go to ScreenC'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.redAccent,
),
),
],
),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:navigator/main.dart';
void main() => runApp(ScreenB());
class ScreenB extends StatelessWidget {
const ScreenB({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ScreenB'),
),
body: Center(
child: Text(
'ScreenB',
style: TextStyle(
fontSize: 24.0,
),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:navigator/main.dart';
void main() => runApp(ScreenC());
class ScreenC extends StatelessWidget {
const ScreenC({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ScreenC'),
),
body: Center(
child: Text(
'ScreenC',
style: TextStyle(
fontSize: 24.0,
),
),
),
);
}
}
ScreenA | ScreenB | ScreenC |
---|---|---|