Ansa Flutter SDK

Ansa Flutter SDK - A Flutter SDK for integrating Ansa Core and Ansa UI functionalities into your Flutter applications.

Overview

This package wraps two core functionalities:

  • Ansa Core: A headless core SDK that allows you to manage your UI while leveraging Ansa's powerful backend.
  • Ansa UI: Pre-built UI components that use Ansa Core under the hood.

The Ansa Flutter SDK is compatible with both Android and iOS platforms. This guide will help you integrate the SDK into your Flutter application using best practices.


Installation

Add the Ansa Flutter SDK to your pubspec.yaml:

dependencies:
  ansa_flutter_sdk: ^0.0.1

Then, run the following command:

flutter pub get

Setup

To integrate the SDK into your Flutter application:

1. Enable Swift Package Manager

Flutter's Swift Package Manager (SPM) support is disabled by default. To enable it, follow these steps:

  1. Switch to Flutter's main channel:

    flutter channel main --no-cache-artifacts
    
  2. Upgrade to the latest Flutter SDK:

    flutter upgrade
    
  3. Enable the Swift Package Manager feature:

    flutter config --enable-swift-package-manager
    

Once enabled, running your Flutter app will automatically migrate the iOS project to support SPM integration. For more details, refer to Flutter's Swift Package Manager Documentation.


2. Initialize the SDK

You’ll need your Merchant Publishable Key from the Ansa Portal. For details on obtaining API keys and setting up customers, refer to the Ansa Documentation.

Use the following code to initialize the SDK in your main.dart:

import 'package:ansa_flutter_sdk/ansa_flutter_sdk.dart';
import 'package:ansa_flutter_sdk/ansa_logger.dart';

final _ansaFlutterSdk = AnsaFlutterSdk();

Future<void> _initializeSdk() async {
  await _ansaFlutterSdk.initializeSdk(
    publishableKey: 'your-publishable-key',
    clientSecretProvider: (customerId) async {
      // Provide the client secret dynamically
      return 'your-client-secret-key';
    },
    logLevel: LogLevel.info,
  );
}

Important: Securely fetch the client secret key from your backend server in production.

3. Add Pre-Built UI Components

You can integrate pre-built UI components like the ManagedAnsaBalanceScreen for balance management. Supports custom branding and a callback for navigation handling:

import 'package:ansa_flutter_sdk/ansa_flutter_sdk.dart';
import 'package:flutter/material.dart';

class BalanceScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ManagedAnsaBalanceScreen(
      customerId: 'your-customer-id',
      merchantId: 'your-merchant-id',
      onGoBack: () {
        // Handle navigation or cleanup when the user navigates back
        print('User navigated back');
      },
      brandColors: BrandColors(
        primary: '#FF5733', // Primary color for the UI
        secondary: '#33FF57', // Secondary color for accents
      ),
    );
  }
}

Features

Core API Methods

  • Customer Management: Fetch and manage customer data.
  • Merchant Management: Retrieve merchant details.
  • Payment Methods: Add, fetch, and delete payment methods.
  • Balance Management: Add and use balances programmatically.
  • Auto Reload: Configure and manage auto-reload settings.
  • Transactions: Fetch transaction details.

Pre-Built UI Components

  • ManagedAnsaBalanceScreen: Displays the balance widget, allowing users to view and manage their balance. Supports custom brand colors and a callback for handling back navigation.

Example Usage

Here’s a simple example app integrating various features of the SDK:

import 'package:ansa_flutter_sdk/ansa_flutter_sdk.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final _ansaFlutterSdk = AnsaFlutterSdk();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Ansa Flutter SDK Example')),
        body: Column(
          children: [
            ElevatedButton(
              onPressed: () async {
                await _ansaFlutterSdk.initializeSdk(
                  publishableKey: 'your-publishable-key',
                  clientSecretProvider: (customerId) async {
                    // Provide the client secret dynamically
                    return 'your-client-secret-key';
                  },
                  logLevel: LogLevel.info,
                );
              },
              child: const Text('Initialize SDK'),
            ),
            ElevatedButton(
              onPressed: () async {
                final customer = await _ansaFlutterSdk.customer
                    .getCustomer(customerId: 'your-customer-id');
                print(customer);
              },
              child: const Text('Fetch Customer'),
            ),
          ],
        ),
      ),
    );
  }
}

Logging

You can configure logging during SDK initialization:

_ansaFlutterSdk.initializeSdk(
  publishableKey: 'your-publishable-key',
  clientSecretProvider: (customerId) async {
    // Provide the client secret dynamically
    return 'your-client-secret-key';
  },
  logLevel: LogLevel.debug, // Set your log level
);

Supported Platforms

  • iOS: Requires iOS 16.0+
  • Android: Requires Android API Level 26+

Resources

For any questions, feel free to reach out to your implementation manager or open an issue on GitHub.