Skip to content

Latest commit

 

History

History
236 lines (192 loc) · 5.92 KB

File metadata and controls

236 lines (192 loc) · 5.92 KB

Flutter Login GetX - Refactored Structure

🏗️ Cấu trúc mới được refactor

Dự án đã được tổ chức lại theo cấu trúc đơn giản và dễ hiểu:

lib/
├── bindings/
│   └── auth_binding.dart           # Dependency injection
├── controllers/
│   └── auth_controller.dart        # State management
├── middlewares/
│   └── auth_middleware.dart        # Route protection
├── models/
│   └── app_user.dart              # Data models
├── pages/
│   ├── home/
│   │   └── home_page.dart
│   ├── login/
│   │   └── login_page.dart
│   ├── register/
│   │   └── register_page.dart
│   └── loading_page.dart
├── routes/
│   └── app_pages.dart             # Route configuration
├── services/
│   ├── auth_service.dart          # Service interface
│   └── firebase_auth_service.dart # Firebase implementation
├── themes/
│   ├── dark_mode.dart
│   └── light_mode.dart
├── firebase_options.dart
├── main.dart                      # Original main (Bloc)
└── main_refactored.dart           # New main (GetX)

🎯 So sánh với cấu trúc cũ

Cũ (Features-based) Mới (Layer-based)
features/auth/presentation/controllers/ controllers/
features/auth/domain/entities/ models/
features/auth/data/ services/
features/auth/presentation/pages/ pages/login/, pages/register/, pages/home/
core/routes/ routes/
features/auth/presentation/bindings/ bindings/
features/auth/presentation/middlewares/ middlewares/

🚀 Cách sử dụng

1. Chạy ứng dụng refactored

# Thay đổi main file trong pubspec.yaml hoặc chạy:
flutter run lib/main_refactored.dart

2. Cấu trúc các thành phần

Controllers (lib/controllers/)

  • auth_controller.dart: Quản lý toàn bộ authentication state với GetX
final AuthController authController = Get.find<AuthController>();
authController.loginWithEmailPassword(email, password);

Models (lib/models/)

  • app_user.dart: Data model cho user
class AppUser {
  final String uid;
  final String email;
  // ...
}

Services (lib/services/)

  • auth_service.dart: Interface cho authentication
  • firebase_auth_service.dart: Implementation với Firebase
abstract class AuthService {
  Future<AppUser?> loginWithEmailPassword(String email, String password);
  // ...
}

Pages (lib/pages/)

Các pages được tổ chức theo feature:

  • login/login_page.dart: Login UI
  • register/register_page.dart: Register UI
  • home/home_page.dart: Home UI
  • loading_page.dart: Loading UI

Routes (lib/routes/)

  • app_pages.dart: Cấu hình tất cả routes
static final routes = [
  GetPage(name: '/login', page: () => const LoginPage()),
  GetPage(name: '/home', page: () => const HomePage()),
];

Bindings (lib/bindings/)

  • auth_binding.dart: Dependency injection setup
class AuthBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut<AuthService>(() => FirebaseAuthService());
    Get.put<AuthController>(AuthController(authService: Get.find()));
  }
}

Middlewares (lib/middlewares/)

  • auth_middleware.dart: Route protection và navigation guards

💡 Lợi ích của cấu trúc mới

1. Đơn giản hóa

  • Ít thư mục lồng nhau
  • Dễ tìm file theo tính năng
  • Cấu trúc phẳng, dễ navigate

2. Tách biệt rõ ràng

  • Controllers: State management logic
  • Services: Business logic và data access
  • Models: Data structures
  • Pages: UI components
  • Routes: Navigation configuration

3. Dễ mở rộng

Khi thêm feature mới:

lib/
├── controllers/
│   ├── auth_controller.dart
│   └── profile_controller.dart    # New
├── pages/
│   ├── login/
│   ├── home/
│   └── profile/                   # New
│       └── profile_page.dart
├── services/
│   ├── auth_service.dart
│   └── profile_service.dart       # New

4. Maintenance

  • Một controller cho mỗi feature
  • Services có thể được reused
  • Models độc lập
  • Pages focus vào UI

🔧 Migration từ cấu trúc cũ

Để chuyển từ Bloc sang GetX:

  1. Dependencies
dependencies:
  get: ^4.6.6  # Thay vì flutter_bloc
  1. Main App
// Cũ
MultiBlocProvider(...)

// Mới  
GetMaterialApp(
  initialRoute: AppPages.initial,
  getPages: AppPages.routes,
  initialBinding: AuthBinding(),
)
  1. State Management
// Cũ
BlocBuilder<AuthCubit, AuthStates>(...)

// Mới
Obx(() => Text(authController.isAuthenticated ? 'Logged In' : 'Not Logged In'))
  1. Navigation
// Cũ
Navigator.pushNamed(context, '/home')

// Mới
Get.toNamed('/home')

🎨 Components được giữ lại

Các component UI từ cấu trúc cũ vẫn được sử dụng:

  • MyButton
  • MyTextfield
  • AppleSignInButton
  • GoogleSignInButton

Import path đã được cập nhật trong các pages mới.

🚦 Route Flow

/loading (AuthBinding)
    ↓
/login (LoginMiddleware) ← unauthenticated
    ↓ (login success)
/home (AuthMiddleware) ← authenticated

📋 TODO: Clean up old files

Sau khi test thành công, có thể xóa:

  • lib/features/ (toàn bộ thư mục)
  • lib/core/ (nếu không dùng)
  • lib/main_getx.dart (thay bằng main_refactored.dart)

🎯 Kết luận

Cấu trúc mới mang lại:

  • Đơn giản: Ít folders, dễ navigate
  • Rõ ràng: Mỗi layer có responsibility riêng
  • Mở rộng: Dễ thêm features mới
  • Maintain: Code organized và clean

Perfect cho các dự án vừa và nhỏ! 🚀