This project is a Flutter application showcasing the various features of the GetX state management and utility framework, helping developers learn and understand the core features of GetX.
lib/
├── app/
│ ├── controllers/ # Controllers (state management)
│ ├── modules/ # UI pages for each feature module
│ ├── routes/ # Route management
│ ├── translations/ # Internationalization
│ └── bindings/ # Dependency injection bindings
└── main.dart # Application entry point
This project demonstrates the six core features of GetX:
graph TD
A[GetX Features] --> B[State Management]
A --> C[Dependency Injection]
A --> D[Route Management]
A --> E[Internationalization]
A --> F[Theme Management]
A --> G[Data Persistence]
B --> B1[Simple State Management]
B --> B2[Reactive State Management]
B1 --> B1a[GetBuilder]
B2 --> B2a[.obs & Obx]
B2 --> B2b[Workers]
GetX provides two state management approaches: simple state management and reactive state management.
// Controller definition - using GetBuilder to update UI
class CounterController extends GetxController {
int counter = 0;
void increment() {
counter++;
update(); // Notify listeners to update
}
}
// Usage in UI
GetBuilder<CounterController>(
builder: (controller) {
return Text('${controller.counter}');
},
)
Features:
- Lightweight with less memory usage
- Suitable for states that don't change frequently
- Requires manual call to
update()
method to refresh UI
// Controller definition - using .obs to create reactive variables
class ReactiveCounterController extends GetxController {
RxInt count = 0.obs;
void increment() {
count.value++;
}
}
// Usage in UI
Obx(() => Text('${controller.count.value}'))
Features:
- Automatically responds to changes without manual update calls
- Supports various Workers to monitor changes
- Can monitor changes to individual variables
- Suitable for frequently changing states
GetX provides a simple dependency injection mechanism without complex setup:
// Register dependency
Get.put<ThemeController>(ThemeController());
// Lazy loading registration
Get.lazyPut<CounterController>(() => CounterController());
// Get controller instance
final controller = Get.find<CounterController>();
In this project, we use Bindings classes to manage dependencies:
class HomeBinding extends Bindings {
@override
void dependencies() {
Get.put<ThemeController>(ThemeController(), permanent: true);
Get.lazyPut<CounterController>(() => CounterController(), fenix: true);
}
}
GetX provides an advanced routing system that doesn't require context for navigation:
// Define route
GetPage(
name: Routes.SIMPLE_COUNTER,
page: () => SimpleCounterPage(),
binding: SimpleCounterBinding(),
transition: Transition.rightToLeft,
),
// Navigate to named route
Get.toNamed(Routes.SIMPLE_COUNTER);
// Go back
Get.back();
// Show dialog
Get.dialog(AlertDialog(...));
// Show Snackbar
Get.snackbar('Title', 'Message');
GetX provides a simple internationalization solution:
// Define translations
class AppTranslations extends Translations {
@override
Map<String, Map<String, String>> get keys => {
'en_US': {
'hello': 'Hello',
},
'zh_CN': {
'hello': '你好',
},
};
}
// Use translation
Text('hello'.tr)
// Translation with parameters
Text('greeting'.trParams({'name': 'John'}))
// Switch language
Get.updateLocale(Locale('zh', 'CN'));
GetX provides convenient theme management features:
// Switch theme mode
Get.changeThemeMode(ThemeMode.dark);
// Apply custom theme
Get.changeTheme(ThemeData.dark());
Implement simple data persistence with GetStorage:
// Initialize
await GetStorage.init();
// Store data
final box = GetStorage();
box.write('key', value);
// Read data
final value = box.read('key');
- Get logger instance:
final logger = Get.find<AppLogger>(); logger.d('Debug log'); logger.i('Info log'); logger.e('Error log');
- No manual initialization needed, already globally registered in
AppBinding
. - All log outputs have replaced print, making maintenance and extension easier.
flutter pub get
flutter run
- To customize log formats, output to files, etc., you can extend functionality in
lib/app/core/logger.dart
. - The project draws inspiration from Jetpack architecture principles (such as dependency injection, modularization, single responsibility), with unified code style for easy maintenance.
If you have any questions, feel free to open an issue or reach out!