Skip to content

Commit

Permalink
go_router add
Browse files Browse the repository at this point in the history
  • Loading branch information
jwson-automation committed Jul 24, 2024
1 parent 65f509e commit 43e6263
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
28 changes: 6 additions & 22 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import 'package:blueberry_flutter_template/screens/mypage/password/PasswordResetPage.dart';
import 'package:blueberry_flutter_template/screens/TopScreen.dart';
import 'package:blueberry_flutter_template/router/RouterProvider.dart';
import 'package:blueberry_flutter_template/utils/AppStrings.dart';
import 'package:blueberry_flutter_template/utils/ResponsiveLayoutBuilder.dart';
import 'package:dart_openai/dart_openai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'firebase_options.dart';
import 'providers/ThemeProvider.dart';
import 'screens/SplashScreen.dart';
import 'utils/AppTheme.dart';

Future<void> main() async {
OpenAI.apiKey = ""; // OpenAI API Key를 넣어주세요.
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
Expand All @@ -25,32 +20,21 @@ Future<void> main() async {
class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return ProviderScope(
child: Consumer(
builder: (context, ref, child) {
// ThemeProvider를 구독하여 다크모드 상태를 가져옴
final themeMode = ref.watch(themeNotifierProvider);
final themeMode = ref.watch(themeNotifierProvider); // 테마 모드 상태 관리 객체
final router = ref.watch(routerProvider); // 라우터 객체

return MaterialApp(
navigatorKey: navigatorKey,
return MaterialApp.router(
routerConfig: router,
debugShowCheckedModeBanner: false,
title: AppStrings.appTitle,
// 라이트 모드 테마 설정
theme: lightTheme,
// 다크 모드 테마 설정
darkTheme: darkTheme,
// 현재 테마 모드 설정
themeMode: themeMode,
// 플랫폼에 따른 초기 화면 설정(web은 스플래쉬스크린 없음)
home: kIsWeb
? ResponsiveLayoutBuilder(
context,
const TopScreen(),
)
: const SplashScreen(),
);
},
),
Expand Down
25 changes: 25 additions & 0 deletions lib/router/RouterProvider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:blueberry_flutter_template/screens/TopScreen.dart';
import 'package:blueberry_flutter_template/screens/mbti/MBTIScreen.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

import '../screens/SplashScreen.dart';
import '../utils/ResponsiveLayoutBuilder.dart';

final routerProvider = Provider<GoRouter>((ref) {
return GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => ResponsiveLayoutBuilder(
context,
kIsWeb ? const TopScreen() : const SplashScreen(),
)),
GoRoute(
path: '/home',
builder: (context, state) => const TopScreen(),
),
],
);
});
16 changes: 5 additions & 11 deletions lib/screens/SplashScreen.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import 'package:blueberry_flutter_template/router/RouterProvider.dart';
import 'package:blueberry_flutter_template/screens/TopScreen.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart'; // kIsWeb 상수 사용
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

import '../utils/ResponsiveLayoutBuilder.dart';

/// 작성일: 2024-07-01
/// 작성자: 오물개
/// 내용: 앱 초기화와 필수 자원 로딩을 관리하는 스플래쉬 스크린 구현
class SplashScreen extends ConsumerStatefulWidget {
const SplashScreen({super.key});

Expand Down Expand Up @@ -43,13 +41,9 @@ class _SplashScreenState extends ConsumerState<SplashScreen> {
// 스플레쉬 스크린 확인을 위한 3초 지연
await Future.delayed(const Duration(seconds: 3));
// 초기화 완료 후 메인 화면으로 전환
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => ResponsiveLayoutBuilder(
context,
const TopScreen(),
)),
);
if (mounted) {
context.go('/home'); // GoRouter를 사용한 라우팅
}
} catch (e) {
// 초기화 중 발생한 오류 처리
print('초기화 오류: $e');
Expand Down
8 changes: 6 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ dependencies:
easy_engine: ^0.0.4
cloud_functions: ^5.0.2

# Lints
# Lint
flutter_lints: ^3.0.0

# Generate
flutter_gen: ^5.6.0

build_runner: ^2.4.11
json_serializable: ^6.8.0

# Router
go_router: ^14.2.1

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 43e6263

Please sign in to comment.