A Flutter application that calculates the absolute difference between a number and its reverse, built with Clean Architecture principles and Riverpod for state management.
- Features
- Tech Stack
- Architecture
- Project Structure
- Getting Started
- Running the App
- Testing
- Calculation Logic
- β Calculate absolute difference between a number and its reverse
- β Handle large numbers using BigInt
- β Clean UI with Material Design 3
- β State management with Riverpod
- β Reusable widget components
- β Comprehensive unit tests
- Flutter:
3.41.4(Channel Stable) - Dart:
3.11.1 - State Management:
flutter_riverpod^2.4.9 - Architecture: Clean Architecture + MVVM
This project follows Clean Architecture principles with clear separation of concerns:
- Architecture & Clean Code: Inspired by ResoCoder's Flutter Clean Architectureβimplementing a strict separation of concerns, repository patterns, and robust dependency management.
- Presentation: UI logic dan widgets (Flutter).
- View Model: State management menggunakan Riverpod (Notifier & Providers).
- Data: Data models (JSON mapping) dan repository implementation.
- Core: Utility universal seperti
AppTheme,AppColors, danCalculationLogic.
- Separation of Concerns - Each layer has a single responsibility
- Dependency Rule - Dependencies only point inward
- Testability - Each layer can be tested independently
- Reusability - Components can be reused across features
lib/
βββ core/ # Shared resources
β βββ constants/
β β βββ app_colors.dart # App-wide color palette
β βββ theme/
β β βββ app_theme.dart # Material Design 3 theme
β βββ utils/
β βββ reverse_calculator.dart # Business logic utilities
β
βββ feature/
βββ reverse_subtract/ # Feature module
βββ data/ # Data layer
β βββ models/
β β βββ reverse_result_model.dart
β βββ repositories/
β βββ reverse_repository.dart
β
βββ presentation/ # Presentation layer
β βββ screens/
β β βββ reverse_subtract_screen.dart
β βββ widgets/
β βββ app_background_gradient.dart
β βββ app_back_button.dart
β βββ app_logo.dart
β βββ app_primary_button.dart
β βββ app_text_field.dart
β βββ result_display.dart
β βββ screen_header.dart
β
βββ view_model/ # View Model layer
βββ reverse_state.dart # State class
βββ reverse_notifier.dart # StateNotifier
βββ reverse_providers.dart # Riverpod providers
βββ reverse_view_model.dart # Barrel export
test/
βββ feature/
βββ reverse_subtract/
βββ view_model/
βββ reverse_notifier_test.dart # Unit tests
- Flutter SDK 3.0.0 or higher
- Clone the repository:
git clone <repository-url>
cd reversesubtest- Install dependencies:
flutter pub get- Check Flutter installation:
flutter doctorRun the app in debug mode:
flutter runBuild for release:
# Android APK
flutter build apk --release
# Android App Bundle
flutter build appbundle --release
# iOS
flutter build ios --release
# Web
flutter build web --releaseflutter testThe calculation uses BigInt to handle very large numbers efficiently:
static String calculateReverseDifference(String input) {
String cleanInput = input.replaceAll(RegExp(r'[^0-9]'), '');
if (cleanInput.isEmpty) return "-";
try {
BigInt original = BigInt.parse(cleanInput);
BigInt temp = original;
BigInt reversed = BigInt.zero;
BigInt ten = BigInt.from(10);
while (temp > BigInt.zero) {
reversed = (reversed * ten) + (temp % ten);
temp = temp ~/ ten;
}
return (original - reversed).abs().toString();
} catch (e) {
return "-";
}
}BigInt is used instead of regular integers because:
- β Handles arbitrarily large numbers
- β No overflow issues
- β Precise calculations
- β Suitable for mathematical operations
| Input | Reversed | Difference |
|---|---|---|
| 21 | 12 | 9 |
| 123 | 321 | 198 |
| 123456789 | 987654321 | 864197532 |
| 121 | 121 | 0 |
| 0 | 0 | 0 |
The calculation logic is thoroughly tested with unit tests covering:
- β Normal cases (21 β 9)
- β Edge cases (empty, zero, single digit)
- β Large numbers (123456789 β 864197532)
- β Special cases (palindromes, same digits)
See reverse_notifier_test.dart for complete test coverage.
See pubspec.yaml for complete dependency list:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.8
flutter_riverpod: ^2.4.9
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^5.0.0All widgets are reusable and customizable:
- AppBackgroundGradient - Decorative gradient background
- AppBackButton - Styled back button with shadow
- AppLogo - Customizable logo widget
- AppTextField - Styled text input with validation
- AppPrimaryButton - Primary button with loading state
- ResultDisplay - Result display widget
- ScreenHeader - Combined logo and header
