25-09-22/1
This commit is contained in:
63
lib/controller/home/deep_link_controller.dart
Normal file
63
lib/controller/home/deep_link_controller.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'dart:async';
|
||||
import 'package:app_links/app_links.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
|
||||
class DeepLinkController extends GetxController {
|
||||
// استخدم AppLinks للتعامل مع الروابط
|
||||
final _appLinks = AppLinks();
|
||||
StreamSubscription<Uri>? _linkSubscription;
|
||||
|
||||
// متغير لتخزين الإحداثيات القادمة من الرابط
|
||||
final Rx<LatLng?> deepLinkLatLng = Rx<LatLng?>(null);
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
// ابدأ بالاستماع للروابط عند تشغيل التطبيق
|
||||
initDeepLinks();
|
||||
}
|
||||
|
||||
Future<void> initDeepLinks() async {
|
||||
// الاستماع إلى الروابط القادمة
|
||||
_linkSubscription = _appLinks.uriLinkStream.listen((uri) {
|
||||
print('Received deep link: $uri');
|
||||
_handleLink(uri);
|
||||
});
|
||||
|
||||
// جلب الرابط الأولي الذي قد يكون فتح التطبيق
|
||||
final initialUri = await _appLinks.getInitialLink();
|
||||
if (initialUri != null) {
|
||||
print('Received initial deep link: $initialUri');
|
||||
_handleLink(initialUri);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleLink(Uri uri) {
|
||||
// تحقق من أن الرابط يتبع النمط المتوقع (مثال: intaleq://route?lat=xx&lng=yy)
|
||||
if (uri.scheme == 'intaleq' && uri.host == 'route') {
|
||||
// استخراج خطوط الطول والعرض من الرابط
|
||||
final latString = uri.queryParameters['lat'];
|
||||
final lngString = uri.queryParameters['lng'];
|
||||
|
||||
if (latString != null && lngString != null) {
|
||||
final double? lat = double.tryParse(latString);
|
||||
final double? lng = double.tryParse(lngString);
|
||||
|
||||
if (lat != null && lng != null) {
|
||||
// إذا كانت الإحداثيات صالحة، قم بتحديث المتغير
|
||||
// ستستمع وحدة التحكم في الخريطة لهذا التغيير
|
||||
deepLinkLatLng.value = LatLng(lat, lng);
|
||||
print('Parsed LatLng from deep link: ${deepLinkLatLng.value}');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
// تأكد من إلغاء الاشتراك عند إغلاق وحدة التحكم
|
||||
_linkSubscription?.cancel();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user