legato_centralize_deep_link 0.0.4 legato_centralize_deep_link: ^0.0.4 copied to clipboard
Centralize deep link
import 'dart:developer';
import 'package:deep_link_example/src/modules/app/app_router.dart';
import 'package:deep_link_example/src/modules/common/constants/constants.dart';
import 'package:deep_link_example/src/modules/common/utils/getit_utils.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'handle_deep_links.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
configureDependencies();
return runApp(MainBody());
}
class MainBody extends StatelessWidget {
final router = getIt<AppRouter>();
MainBody({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
// navigatorKey: navigatorKey,
title: 'Centralize DeepLinks Example',
// initialRoute: '/',
// routes: <String, WidgetBuilder>{
// '/': (context) => const MainScreen(),
// '/location': (context) => _DynamicLinkScreen(),
// },
routerConfig: router.config(deepLinkTransformer: _transformDeepLink),
);
}
Future<Uri> _transformDeepLink(Uri uri) async {
log('_transformDeepLink: $uri');
if (Constants.host == uri.host) {
log('_transformDeepLink onDeeplink: $uri');
final segments = uri.pathSegments;
if (segments.isNotEmpty && segments.last.isNotEmpty) {
final token = segments.last;
final metadata = await HandleDeepLinks.instance.extraMetadata(token);
final referralCode = metadata[DynamicLinkKey.referralKey];
if (referralCode != null &&
referralCode is String &&
referralCode.isNotEmpty) {
final transform = uri.replace(
path: '/location',
queryParameters: {DynamicLinkKey.referralKey: referralCode});
log('_transformDeepLink onDeeplink transform: $transform');
return SynchronousFuture(transform);
}
}
}
return SynchronousFuture(uri);
}
}