flutter_deep_linker 0.0.2
flutter_deep_linker: ^0.0.2 copied to clipboard
Advanced deep linking with custom schemes and universal links for Flutter. Supports iOS, Android, Web, Windows, macOS, and Linux with WASM compatibility.
import 'package:flutter/material.dart';
import 'package:flutter_deep_linker/flutter_deep_linker.dart';
void main() {
runApp(const DeepLinkerExampleApp());
}
class DeepLinkerExampleApp extends StatefulWidget {
const DeepLinkerExampleApp({super.key});
@override
State<DeepLinkerExampleApp> createState() => _DeepLinkerExampleAppState();
}
class _DeepLinkerExampleAppState extends State<DeepLinkerExampleApp> {
late FlutterDeepLinker _deepLinker;
DeepLinkResult? _lastResult;
String _status = 'Initializing...';
@override
void initState() {
super.initState();
_initializeDeepLinking();
}
void _initializeDeepLinking() => _initializeDeepLinkingImpl();
void _initializeDeepLinkingImpl() {
try {
// Configure deep linking with multiple schemes and hosts
const config = DeepLinkConfig(
schemes: ['myapp', 'https', 'http'],
hosts: ['example.com', 'myapp.com'],
);
// Initialize the deep linker
_deepLinker = FlutterDeepLinker(config);
// Listen for incoming deep links
_deepLinker.onDeepLink.listen((result) {
setState(() {
_lastResult = result;
_status = 'Deep link received: ${result.url}';
});
});
// Start listening for deep links
_deepLinker.startListening();
setState(() {
_status = 'Deep linking initialized successfully';
});
} on Exception catch (e) {
setState(() {
_status = 'Error initializing deep linking: $e';
});
}
}
@override
void dispose() {
_deepLinker.stopListening();
super.dispose();
}
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Deep Linker Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Deep Linker Example'),
),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Status',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text(_status),
],
),
),
),
const SizedBox(height: 16),
const Card(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Configuration',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8),
Text('Supported Schemes: myapp, https, http'),
Text('Supported Hosts: example.com, myapp.com'),
],
),
),
),
const SizedBox(height: 16),
if (_lastResult != null) ...[
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Last Deep Link',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text('URL: ${_lastResult!.url}'),
Text('Scheme: ${_lastResult!.scheme}'),
Text('Host: ${_lastResult!.host}'),
if (_lastResult!.error != null)
Text('Error: ${_lastResult!.error}'),
],
),
),
),
const SizedBox(height: 16),
],
const Card(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Test Deep Links',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8),
Text(
'Use these URLs to test deep linking:',
),
SizedBox(height: 8),
Text('• myapp://example.com/test'),
Text('• https://example.com/deep-link'),
Text('• http://myapp.com/redirect'),
],
),
),
),
const SizedBox(height: 16),
const Card(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Platform Support',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8),
Text('✓ iOS (Custom schemes + Universal Links)'),
Text('✓ Android (Custom schemes + App Links)'),
Text('✓ Web (Browser-based deep linking + WASM)'),
Text('✓ Windows (Windows-specific deep linking)'),
Text('✓ macOS (macOS-specific deep linking + SPM)'),
Text('✓ Linux (Linux-specific deep linking)'),
Text('✓ WASM compatible'),
],
),
),
),
],
),
),
),
);
}
}