anylink_flutter 0.0.2
anylink_flutter: ^0.0.2 copied to clipboard
A Flutter plugin for integrating AnyLink deep-linking, deferred deep-linking, and session management.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:anylink/anylink.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const AnyLinkExampleApp());
}
class AnyLinkExampleApp extends StatefulWidget {
const AnyLinkExampleApp({super.key});
@override
State<AnyLinkExampleApp> createState() => _AnyLinkExampleAppState();
}
class _AnyLinkExampleAppState extends State<AnyLinkExampleApp> {
AnyLinkDeepLink? _lastReceivedDeepLink;
String _status = 'Initializing SDK...';
final List<String> _logs = [];
@override
void initState() {
super.initState();
_initAnyLinkSdk();
}
Future<void> _initAnyLinkSdk() async {
// Listen for deep link events from the SDK
AnyLinkSdk.onDeepLink((deepLink) {
_addLog('Deep Link Received: ${deepLink.url} (ID: ${deepLink.linkId})');
setState(() {
_lastReceivedDeepLink = deepLink;
});
// Host app parses opaque deepLink.url and performs app-specific navigation
final uri = Uri.tryParse(deepLink.url);
if (uri != null) {
_addLog('Navigating to scheme: ${uri.scheme}, path: ${uri.path}');
}
});
try {
await AnyLinkSdk.initialize(
appId: 'SHOP_APP_DEMO_UUID',
devKey: 'ak_demo_dev_key',
);
setState(() {
_status = 'SDK Initialized (App ID: SHOP_APP_DEMO_UUID)';
});
_addLog('AnyLink SDK successfully initialized');
} catch (e) {
setState(() {
_status = 'SDK Initialization Failed';
});
_addLog('Initialization Error: $e');
}
}
void _addLog(String msg) {
setState(() {
_logs.insert(0, '[${DateTime.now().toIso8601String().substring(11, 19)}] $msg');
});
}
@override
Widget build(BuildContext context) {
final deepLink = _lastReceivedDeepLink;
return MaterialApp(
theme: ThemeData(
useMaterial3: true,
colorSchemeSeed: Colors.indigo,
),
home: Scaffold(
appBar: AppBar(
title: const Text('AnyLink SDK Example'),
elevation: 2,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
color: Colors.indigo.shade50,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'SDK Status',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
const SizedBox(height: 4),
Text(_status),
if (AnyLinkSdk.config != null) ...[
const SizedBox(height: 8),
Text(
'Base URL: ${AnyLinkSdk.config?.publicLinkBaseUrl} | App: ${AnyLinkSdk.config?.appIdentifier}',
style: const TextStyle(fontSize: 12, color: Colors.grey),
),
]
],
),
),
),
const SizedBox(height: 16),
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Last Resolved Deep Link',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
const SizedBox(height: 8),
if (deepLink == null)
const Text(
'No deep link received yet.',
style: TextStyle(color: Colors.grey),
)
else ...[
Text('URL: ${deepLink.url}', style: const TextStyle(fontWeight: FontWeight.w600)),
Text('LinkId: ${deepLink.linkId}', style: const TextStyle(fontSize: 12, color: Colors.grey)),
],
],
),
),
),
const SizedBox(height: 16),
const Text(
'Activity Log',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
const SizedBox(height: 8),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.black87,
borderRadius: BorderRadius.circular(8),
),
padding: const EdgeInsets.all(12),
child: ListView.builder(
itemCount: _logs.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 2.0),
child: Text(
_logs[index],
style: const TextStyle(
color: Colors.greenAccent,
fontFamily: 'monospace',
fontSize: 13,
),
),
);
},
),
),
),
],
),
),
),
);
}
}