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:
-
Switch to Flutter's
mainchannel:flutter channel main --no-cache-artifacts -
Upgrade to the latest Flutter SDK:
flutter upgrade -
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> initializeAnsaSdk() async {
await _ansaFlutterSdk.initializeSdk(
publishableKey: 'your-publishable-key',
logLevel: LogLevel.info,
);
_ansaFlutterSdk.setupClientSecretProvider((String customerId) async {
// Fetch the client secret from your backend securely
return 'your-client-secret-key';
});
}
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:
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',
);
}
}
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.
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',
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',
logLevel: LogLevel.debug,
);
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.
Libraries
- ansa_flutter_sdk
- ansa_flutter_sdk_method_channel
- ansa_flutter_sdk_platform_interface
- ansa_logger
- customer_api
- merchant_api
- models/add_balance_request
- models/add_payment_method_type
- models/auto_reload_config
- models/auto_reload_configuration_request
- models/balance
- models/balance_update
- models/billing_details
- models/card_info
- models/customer
- models/merchant
- models/payment_method
- models/transaction_result
- models/use_balance_request
- models/user_reload_configuration