Skip to content

joshflutterwork/reverseandsubstruct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Reverse & Subtract - Clean Architecture Flutter App

A Flutter application that calculates the absolute difference between a number and its reverse, built with Clean Architecture principles and Riverpod for state management.

πŸ“‹ Table of Contents

✨ Features

  • βœ… 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

πŸ›  Tech Stack

  • Flutter: 3.41.4 (Channel Stable)
  • Dart: 3.11.1
  • State Management: flutter_riverpod ^2.4.9
  • Architecture: Clean Architecture + MVVM

πŸ— Architecture

This project follows Clean Architecture principles with clear separation of concerns:

Layers:

  • 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, dan CalculationLogic.

Key Principles

  1. Separation of Concerns - Each layer has a single responsibility
  2. Dependency Rule - Dependencies only point inward
  3. Testability - Each layer can be tested independently
  4. Reusability - Components can be reused across features

πŸ“ Project Structure

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

πŸš€ Getting Started

Prerequisites

  • Flutter SDK 3.0.0 or higher

Installation

  1. Clone the repository:
git clone <repository-url>
cd reversesubtest
  1. Install dependencies:
flutter pub get
  1. Check Flutter installation:
flutter doctor

πŸƒ Running the App

Development Mode

Run the app in debug mode:

flutter run

Release Mode

Build for release:

# Android APK
flutter build apk --release

# Android App Bundle
flutter build appbundle --release

# iOS
flutter build ios --release

# Web
flutter build web --release

πŸ§ͺ Testing

Run All Tests

flutter test

πŸ”’ Calculation Logic

Algorithm

The 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 "-";
    }
  }

Why BigInt?

BigInt is used instead of regular integers because:

  • βœ… Handles arbitrarily large numbers
  • βœ… No overflow issues
  • βœ… Precise calculations
  • βœ… Suitable for mathematical operations

Examples

Input Reversed Difference
21 12 9
123 321 198
123456789 987654321 864197532
121 121 0
0 0 0

Unit Testing

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.

πŸ“¦ Dependencies

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.0

🎨 UI Components

Reusable Widgets

All 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

πŸ“Έ Demo

App Demo

About

A Flutter application that calculates the absolute difference between a number and its reverse, built with Clean Architecture principles and Riverpod for state management.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors