com_tapp_so 1.0.0
com_tapp_so: ^1.0.0 copied to clipboard
Flutter plugin for Tapp.so affiliate marketing SDK. Track attributions, handle deferred deep links, and fire conversion events on iOS and Android.
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:com_tapp_so/com_tapp_so.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _sdk = ComTappSo();
// Input Controllers
final _shouldProcessController = TextEditingController(text: 'https://example.com');
final _influencerController = TextEditingController(text: 'influencer_flutter');
final _fetchLinkController = TextEditingController(text: 'https://example.com');
// —— Core SDK state
String _should = '—';
String _url = '—';
String _tappEvt = '—';
String _link = '—';
String _orig = '—';
String _testMsg = '—';
// —— Deferred‐link events
String _deferred = '—';
String _fail = '—';
@override
void initState() {
super.initState();
_initializeSdk();
}
@override
void dispose() {
// Controller disposal
_shouldProcessController.dispose();
_influencerController.dispose();
_fetchLinkController.dispose();
super.dispose();
}
// Replace with your actual tokens from tapp.so dashboard
static const _authToken = String.fromEnvironment('TAPP_AUTH_TOKEN', defaultValue: 'YOUR_AUTH_TOKEN');
static const _tappToken = String.fromEnvironment('TAPP_TOKEN', defaultValue: 'YOUR_TAPP_TOKEN');
Future<void> _initializeSdk() async {
try {
await _sdk.start(
authToken: _authToken,
env: EnvironmentType.SANDBOX,
tappToken: _tappToken,
onDeferredDeepLink: (data) {
if (!mounted) return;
setState(() {
_deferred = 'Tapp URL: ${data.tappUrl}\n'
'Influencer: ${data.influencer}\n'
'First Session: ${data.isFirstSession}\n'
'Data: ${data.data}';
});
},
onFailResolvingUrl: (err) {
if (!mounted) return;
setState(() {
_fail = 'URL: ${err.url}\nError: ${err.error}';
});
},
onTestListener: (msg) {
if (!mounted) return;
setState(() => _testMsg = msg ?? 'null');
},
);
} on PlatformException {
// ignore
}
await _checkShouldProcess();
}
Future<void> _checkShouldProcess() async {
final ok = await _sdk.shouldProcess(_shouldProcessController.text);
if (!mounted) return;
setState(() => _should = ok ? 'Yes' : 'No');
}
Future<void> _generateUrl() async {
final u = await _sdk.generateUrl(
influencer: _influencerController.text,
adGroup: 'group',
creative: 'creative',
data: {'foo': 'bar'},
);
if (!mounted) return;
setState(() => _url = u ?? 'Error generating URL');
}
Future<void> _handleTappEvent() async {
await _sdk.handleTappEvent(
eventAction: EventAction.custom,
customValue: 'tapp_purchase',
metadata: {
'orderId': '123456',
'value': 99.99,
'currency': 'USD',
},
);
if (!mounted) return;
setState(() => _tappEvt = 'Event sent successfully');
}
Future<void> _fetchLinkData() async {
final link = await _sdk.fetchLinkData(_fetchLinkController.text);
if (!mounted) return;
setState(() => _link = link != null ? link.toString() : 'No data found');
}
Future<void> _fetchOriginLinkData() async {
final link = await _sdk.fetchOriginLinkData();
if (!mounted) return;
setState(() => _orig = link != null ? link.toString() : 'No origin data');
}
Future<void> _simulateTestEvent() async {
await _sdk.simulateTestEvent();
}
Widget _buildCard({required String title, required IconData icon, required List<Widget> children}) {
return Card(
elevation: 2,
margin: const EdgeInsets.only(bottom: 16),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Icon(icon, color: Theme.of(context).colorScheme.primary),
const SizedBox(width: 12),
Text(
title,
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
],
),
const Divider(height: 24),
...children,
],
),
),
);
}
Widget _buildActionItem(
String label,
String value,
VoidCallback onTap,
{String? buttonLabel, TextEditingController? controller}
) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(label, style: const TextStyle(fontWeight: FontWeight.w600)),
),
FilledButton.tonal(
onPressed: onTap,
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 16),
),
child: Text(buttonLabel ?? 'Execute'),
),
],
),
if (controller != null) ...[
const SizedBox(height: 8),
TextField(
controller: controller,
decoration: InputDecoration(
isDense: true,
border: const OutlineInputBorder(),
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
labelStyle: TextStyle(fontSize: 12, color: Theme.of(context).colorScheme.secondary),
),
style: const TextStyle(fontSize: 13),
),
],
const SizedBox(height: 8),
Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHighest.withOpacity(0.5),
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Theme.of(context).colorScheme.outlineVariant),
),
child: Text(
value,
style: TextStyle(
fontFamily: 'monospace',
fontSize: 12,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
),
const SizedBox(height: 16),
],
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
brightness: Brightness.light,
),
scaffoldBackgroundColor: const Color(0xFFFDF8FD),
appBarTheme: const AppBarTheme(
centerTitle: true,
elevation: 0,
scrolledUnderElevation: 2,
backgroundColor: Color(0xFFFDF8FD),
),
),
darkTheme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
brightness: Brightness.dark,
),
),
themeMode: ThemeMode.system,
home: Scaffold(
appBar: AppBar(
title: const Text('Tapp SDK'),
actions: [
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () {
setState(() {
// Refresh UI state
});
},
),
],
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildCard(
title: 'Core Functions',
icon: Icons.api,
children: [
_buildActionItem(
'Should Process URL',
_should,
_checkShouldProcess,
buttonLabel: 'Check',
controller: _shouldProcessController
),
_buildActionItem(
'Generate URL',
_url,
_generateUrl,
buttonLabel: 'Generate',
controller: _influencerController // Using this input for the influencer arg
),
_buildActionItem('Track Event', _tappEvt, _handleTappEvent, buttonLabel: 'Track'),
_buildActionItem(
'Fetch Link Data',
_link,
_fetchLinkData,
buttonLabel: 'Fetch',
controller: _fetchLinkController
),
_buildActionItem('Fetch Origin Data', _orig, _fetchOriginLinkData, buttonLabel: 'Fetch'),
],
),
_buildCard(
title: 'Deferred Links',
icon: Icons.link,
children: [
_buildActionItem('Deferred Link', _deferred, () {}, buttonLabel: 'Waiting...'),
_buildActionItem('Resolution Error', _fail, () {}, buttonLabel: 'Waiting...'),
],
),
_buildCard(
title: 'Testing',
icon: Icons.bug_report,
children: [
_buildActionItem('Simulate Event', _testMsg, _simulateTestEvent, buttonLabel: 'Simulate'),
],
),
],
),
),
),
);
}
}