ย
MeterialApp ํด๋์ค์ navigatorKey ์ onGenerateRoute , builder ์์ฑ์ ์ด์ฉํ์ฌ ๋ทฐ ๋ผ์ฐํฐ๋ฅผ ๋ง๋๋ ค๊ณ ํ๋ค routes.dart ํ์ผ์ AppNavigator ํด๋์ค ์์ฑํ navigatorKey ์ onGenerateRoute ์ฝ๋ฐฑ ํจ์๋ฅผ ๋ง๋ค๊ณ ์ฑ ์คํ์ ํด๋ดค์๋ onGeneraterRoute ์ฝ๋ฐฑ์ผ๋ก โ/โ ๊ฐ ๋ฆฌํด๋์๋ค. ํด๋น ๋ณ์๋ฅผ ํตํด์ ๊ทธ์ ๋ง๋ Widget ๋ณ์๋ฅผ ๋ฆฌํดํ์ฌ ํ๋ฉด์ ํ์ํ๋ฉด ๋๋คonGeneraterRoute ํจ์์์ ํ ํ๋ฉด์ ๋ฆฌํดํด ๋ณด์
ย
routes.dart
class _Paths { static const String home = '/home'; } class AppNavigator { static GlobalKey<NavigatorState> navigatorKey = GlobalKey(); static Route onGenerateRoute(RouteSettings settings) { print("generate Route settings name ${settings.name}"); switch (settings.name) { case _Paths.home: default: return FadeRoute(page: HomeScreen()); } }
app.dart
@override Widget build(BuildContext context){ return MaterialApp( color : Colors.white, navigatorKey: AppNavigator.navigatorKey, onGenerateRoute: AppNavigator.onGenerateRoute, theme: ThemeData( primarySwatch: Colors.blue, ), title : "UserManager", builder: (context, child) { print("[UserManagerApp Builder]"); print("child => ${child}"); print("context => ${context}"); if(child == null) return SizedBox.shrink(); final data = MediaQuery.of(context); final smallestSize = min(data.size.width, data.size.height); final textScaleFactor = min(smallestSize / const Size(375, 754).width, 1.0); return MediaQuery(data: data.copyWith(textScaleFactor: textScaleFactor), child: child); }, ); }
ย
home.dart
class _HomeScreenState extends State<HomeScreen> { @override void initState() { super.initState(); } @override Widget build(BuildContext context){ return Scaffold( body : Center( child : Text('this is home') ) ); } }
ย
๊ฒฐ๊ณผ
ย