Skip to content

NadeemIqbal/Android_Flutter_Sdk_intg

Repository files navigation

White-Label Native Android + Flutter SDK Integration

Production-grade architecture for embedding a Flutter SDK in a native Android application with dynamic white-label theming support.

Architecture Overview

+---------------------------+     +---------------------------+
|    Native Android App     |     |      Flutter SDK          |
+---------------------------+     +---------------------------+
|                           |     |                           |
|  WhiteLabelThemeManager   |<--->|  ThemeController          |
|  (StateFlow)              |     |  (ChangeNotifier)         |
|                           |     |                           |
|  FlutterSdkIntegration    |<--->|  SdkPlatformChannel       |
|  (Facade)                 |     |  (MethodChannel Bridge)   |
|                           |     |                           |
|  WhiteLabelComposeTheme   |     |  WhiteLabelThemeProvider  |
|  (Compose Theme)          |     |  (InheritedWidget)        |
|                           |     |                           |
+---------------------------+     +---------------------------+
            |                                 |
            v                                 v
+---------------------------+     +---------------------------+
|   Native Compose UI       |     |   Flutter SDK UI          |
|   (Same theme tokens)     |     |   (Same theme tokens)     |
+---------------------------+     +---------------------------+

Key Features

  • Single Source of Truth: Both native and Flutter UI consume the same WhiteLabelTheme model
  • Hot-Swappable Theme: Theme can be changed at runtime without app restart
  • Dark Mode Support: Automatic dark mode handling with separate color tokens
  • Native-Driven Navigation: Flutter navigation controlled via MethodChannel
  • Deep Link Support: Deep links forwarded from native to Flutter
  • Event Bridge: Bidirectional event communication between native and Flutter
  • iOS Ready: Architecture designed for cross-platform compatibility

Project Structure

Android_Flutter_Sdk_intg/
├── app/                                    # Native Android App
│   └── src/main/java/com/example/android_flutter_sdk_intg/
│       ├── domain/
│       │   ├── model/
│       │   │   ├── WhiteLabelTheme.kt     # Theme domain model
│       │   │   └── NavigationCommand.kt   # Navigation commands
│       │   └── theme/
│       │       ├── ThemeExtractor.kt      # Extract theme from Android
│       │       └── WhiteLabelThemeManager.kt # Theme state management
│       ├── flutter/
│       │   ├── FlutterEngineManager.kt    # Engine lifecycle
│       │   ├── FlutterSdkBridge.kt        # MethodChannel bridge
│       │   └── FlutterSdkIntegration.kt   # High-level SDK API
│       ├── ui/
│       │   ├── screens/
│       │   │   └── NativeHomeScreen.kt    # Sample native screen
│       │   └── theme/
│       │       └── WhiteLabelComposeTheme.kt # Compose theme wrapper
│       ├── MainActivity.kt                 # Main entry point
│       ├── FlutterSdkActivity.kt          # Flutter host activity
│       └── SdkApplication.kt              # Application class
│
├── flutter_sdk_module/                     # Flutter SDK Module
│   └── lib/
│       ├── main.dart                       # Entry point
│       ├── flutter_sdk_module.dart         # Public API exports
│       └── src/
│           ├── sdk.dart                    # SDK facade
│           ├── core/
│           │   ├── theme/
│           │   │   └── theme_controller.dart
│           │   └── navigation/
│           │       └── navigation_controller.dart
│           ├── domain/
│           │   └── models/
│           │       ├── white_label_theme.dart
│           │       └── navigation_command.dart
│           ├── platform/
│           │   └── sdk_platform_channel.dart
│           └── presentation/
│               ├── widgets/
│               │   └── sdk_root_widget.dart
│               └── screens/
│                   ├── sdk_home_screen.dart
│                   ├── sdk_details_screen.dart
│                   └── sdk_settings_screen.dart
│
└── scripts/
    ├── build_flutter_aar.ps1              # Windows build script
    └── build_flutter_aar.sh               # Unix build script

Setup Instructions

Prerequisites

  • Android Studio Arctic Fox or later
  • Flutter SDK 3.9.0 or later
  • Kotlin 2.0+
  • Gradle 8.7+

Step 1: Build Flutter Module AAR

# Navigate to Flutter module
cd flutter_sdk_module

# Get dependencies
flutter pub get

# Build AAR (creates debug, profile, and release variants)
flutter build aar --no-tree-shake-icons

Step 2: Configure Android Project

The Gradle configuration is already set up. After building the AAR:

  1. The AAR artifacts will be in flutter_sdk_module/build/host/outputs/repo/
  2. The settings.gradle.kts already includes the Maven repository
  3. The app/build.gradle.kts already includes the dependencies

Step 3: Run the App

# From project root
./gradlew :app:installDebug

Usage

Updating Theme from Native

// Get the integration instance
val integration = FlutterSdkIntegration.getInstance()

// Create a custom theme
val theme = ThemeExtractor.createEnterpriseTheme(
    clientId = "client_abc",
    themeName = "Client ABC Brand",
    primaryColor = 0xFF1976D2,
    secondaryColor = 0xFF26A69A,
    isDarkMode = false
)

// Update theme (automatically propagates to Flutter)
integration.updateTheme(theme)

Navigating to Flutter Routes

// Open Flutter SDK at home
startActivity(FlutterSdkActivity.createIntent(this))

// Navigate to specific route
integration.navigateTo("/details", mapOf("id" to "123"))
startActivity(FlutterSdkActivity.createIntent(this))

Handling Deep Links

// In MainActivity.onCreate or onNewIntent
val uri = intent.data
if (uri != null) {
    integration.handleDeepLink(
        uri = uri.toString(),
        parameters = mapOf("source" to "deep_link"),
        source = "external"
    )
    startActivity(FlutterSdkActivity.createIntent(this))
}

Forwarding Notifications

// When notification is received
integration.handleNotification(mapOf(
    "type" to "promo",
    "title" to "Special Offer",
    "route" to "/details"
))

Theme Contract (Version 1.0.0)

The theme contract defines the structure for cross-platform theming:

data class WhiteLabelTheme(
    val version: String,           // Contract version
    val clientId: String,          // Client identifier
    val themeName: String,         // Display name
    val isDarkMode: Boolean,       // Dark mode flag
    val lightColors: ColorTokens,  // Light mode colors
    val darkColors: ColorTokens,   // Dark mode colors
    val typography: TypographyTokens,
    val spacing: SpacingTokens,
    val borderRadius: BorderRadiusTokens
)

Color Tokens

All colors use ARGB Long format (e.g., 0xFF1976D2) for cross-platform compatibility.

Token Description
primary Primary brand color
onPrimary Text/icon color on primary
primaryContainer Container using primary
secondary Secondary brand color
tertiary Tertiary accent color
error Error state color
surface Surface/card background
background Screen background
outline Border/divider color

Typography Tokens

Material Design 3 typography scale:

  • displayLarge, displayMedium, displaySmall
  • headlineLarge, headlineMedium, headlineSmall
  • titleLarge, titleMedium, titleSmall
  • bodyLarge, bodyMedium, bodySmall
  • labelLarge, labelMedium, labelSmall

Spacing Tokens

Token Default Value
xxs 2dp
xs 4dp
sm 8dp
md 16dp
lg 24dp
xl 32dp
xxl 48dp

MethodChannel Contract

Theme Channel (com.example.flutter_sdk/theme/v1)

Method Direction Description
updateTheme Native -> Flutter Push new theme
setDarkMode Native -> Flutter Set dark mode
getTheme Native <- Flutter Get current theme
requestTheme Flutter -> Native Request theme from native

Navigation Channel (com.example.flutter_sdk/navigation/v1)

Method Direction Description
navigate Native -> Flutter Execute navigation command
handleDeepLink Native -> Flutter Handle deep link
getCurrentRoute Native <- Flutter Get current route

Events Channel (com.example.flutter_sdk/events/v1)

Method Direction Description
onSdkEvent Flutter -> Native SDK event (analytics, errors)
onNavigationStateChanged Flutter -> Native Navigation state update

Lifecycle Channel (com.example.flutter_sdk/lifecycle/v1)

Method Direction Description
onNotificationReceived Native -> Flutter Forward notification
onCustomEvent Native -> Flutter Custom event
onAppStateChanged Native -> Flutter App lifecycle state

iOS Compatibility

The architecture is designed for iOS compatibility:

  1. Platform-Agnostic Models: All theme models use primitive types (Long for colors, Double for dimensions)
  2. Versioned Contracts: MethodChannel names include version for backward compatibility
  3. No Android-Specific Types: Domain models don't use Android Color, Drawable, etc.
  4. Single Entry Point: Flutter module has one main entry point

To implement iOS:

  1. Create Swift equivalents of WhiteLabelTheme and related models
  2. Implement FlutterSdkBridge using FlutterMethodChannel
  3. Create FlutterSdkIntegration facade
  4. Use same MethodChannel names and contract

Best Practices

Theme Management

  • Always use theme tokens, never hardcode colors
  • Access colors via context.colors extension in Flutter
  • Use WhiteLabelComposeTheme wrapper in Compose
  • Test both light and dark modes

Navigation

  • Define all routes in _getRoutes() in main.dart
  • Use SdkScreenMixin for automatic analytics tracking
  • Handle back navigation via navigationController.pop()

Error Handling

  • Use Sdk.instance.trackError() for error reporting
  • Handle MethodChannel errors gracefully
  • Provide fallback themes for initialization failures

License

Proprietary - Internal Use Only

About

A production ready reference implementation for embedding a Flutter SDK inside a white label Native Android app. Demonstrates dynamic branding, theming, navigation, deep links, and runtime events while keeping Flutter fully decoupled and future ready for iOS parity and SDK scale.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages