A clean architecture template for Flutter projects using the GetX state management library.
This project serves as a template for building Flutter applications with a focus on clean architecture principles and utilizing the GetX package for state management. The goal is to provide a structured and maintainable foundation for Flutter developers to kickstart their projects.
- Clean Architecture: Separation of concerns with clear divisions between data, domain, and presentation layers.
- GetX State Management: Leverage the power of GetX for simple and efficient state management.
- Project Structure: Organized directory structure following clean architecture practices.
Follow these steps to set up and run the project locally.
Make sure you have the following installed:
-
Clone the repository:
git clone https://github.com/ssoad/flutter_getx_clean_architecture.git
-
Navigate to the project directory:
cd flutter_getx_clean_architecture
-
Install dependencies:
flutter pub get
-
Run the application:
flutter run
The project structure follows the principles of clean architecture to ensure maintainability and scalability. Here's an overview:
- core: Contains the base configurations.
- domain: Contains the business logic and domain entities.
- data: Manages data-related operations, including repositories and datasources.
- presentation: Handles the UI logic with GetX controllers, bindings, and pages.
This project includes support for internationalization. To add translations, follow these steps:
-
Create en.json and bn.json files in the
assets/lang
directory.assets/ |-- lang/ | |-- en.json | |-- es.json
-
Add the translations in the following format:
{ "key": "value" }
-
Update pubspec.yaml with the new translations:
flutter: ... assets: - assets/lang/en.json - assets/lang/bn.json
Run the following command to get the assets:
flutter pub get
-
Load JSON from assets Create a class to load and manage translations from assets. This class will be responsible for reading JSON files and providing translations to GetX:
// core/presentation/translations_service.dart import 'dart:convert'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; class TranslationsService extends Translations { static const fallbackLocale = Locale('en'); static const supportedLocales = [Locale('en'), Locale('es')]; Map<String, String>? _translations; Future<void> loadTranslations(Locale locale) async { final jsonString = await rootBundle.loadString('assets/lang/${locale.languageCode}.json'); final jsonMap = json.decode(jsonString); _translations = jsonMap.cast<String, String>(); } @override Map<String, Map<String, String>> get keys => { for (var locale in supportedLocales) locale.languageCode: _translations ?? {}, }; }
-
Initialize GetX with translations Update your main.dart to initialize translations using the TranslationsService:
// main.dart import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'core/presentation/translations_service.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await initServices(); // Initialize translation service runApp(MyApp()); } Future<void> initServices() async { await Get.putAsync(() => TranslationsService().loadTranslations(Get.deviceLocale ?? TranslationsService.fallbackLocale)); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return GetMaterialApp( translations: TranslationsService(), locale: Get.deviceLocale ?? TranslationsService.fallbackLocale, fallbackLocale: TranslationsService.fallbackLocale, supportedLocales: TranslationsService.supportedLocales, home: MyHomePage(), ); } }
-
Now, you can use translations in your widgets as before:
// presentation/pages/home_page.dart import 'package:flutter/material.dart'; import 'package:get/get.dart'; class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('home'.tr), ), body: Center( child: Text('hello'.tr), ), ); } }
We welcome contributions! Please follow these guidelines when contributing to the project. Check the Contribution Guidelines for more details.
Explain how to run tests and provide examples for different types of tests.
This project is licensed under the MIT License.
For questions or feedback, feel free to reach out: