Flutter Boilerplate1 [Router]

Flutter Boilerplate1 [Router]

ย 
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') ) ); } }
ย 

๊ฒฐ๊ณผ

notion image
ย 
๋Œ“๊ธ€ 0๊ฐœ

๋“ฑ๋ก๋œ ๋Œ“๊ธ€์ด ํ•˜๋‚˜๋„ ์—†์Šต๋‹ˆ๋‹ค...๐Ÿ˜ข