getPageController method

PageController getPageController(
  1. BuildContext context
)

Implementation

PageController getPageController(BuildContext context) {
  final Orientation orientation = MediaQuery.of(context).orientation;

  // احسب قيمة الـ viewportFraction الهدف بناءً على حجم/اتجاه الشاشة
  final double targetFraction =
      (Responsive.isDesktop(context) && orientation == Orientation.landscape)
          ? 0.5
          : 1.0;

  // إذا لم يكن لدينا عملاء (أول إنشاء) أو تغيّرت القيمة، أعد إنشاء المتحكم
  final bool needsNewController = !quranPagesController.hasClients ||
      (quranPagesController.viewportFraction != targetFraction);

  if (needsNewController) {
    // حافظ على الفهرس الحالي للصفحة
    int currentIndex = state.currentPageNumber.value - 1;
    if (quranPagesController.hasClients) {
      final double? p = quranPagesController.page;
      if (p != null) currentIndex = p.round();
    }
    currentIndex = currentIndex.clamp(0, 603);

    final oldController = quranPagesController;
    quranPagesController = PageController(
      initialPage: currentIndex,
      keepPage: kIsWeb || GetPlatform.isDesktop,
      viewportFraction: targetFraction,
    );

    // تخلّص من المتحكم القديم بعد الإطار لتجنّب تعارضات التثبيت
    if (oldController != quranPagesController) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        try {
          oldController.dispose();
        } catch (_) {
          // تجاهل أي أخطاء تصريف إن كان قد صُرّف سابقًا
        }
      });
    }
  }

  // إضافة مستمع تمرير لمرة واحدة
  // if (!_scrollListenerAttached) {
  //   _scrollListenerAttached = true;
  //   quranPagesController.addListener(() {
  //     final metrics = quranPagesController.positions.isNotEmpty
  //         ? quranPagesController.position
  //         : null;
  //     if (metrics == null) return;
  //     final viewport = metrics.viewportDimension;
  //     if (viewport == 0) return;

  //     final page = metrics.pixels / viewport;

  //     // تحديد الصفحة الحالية والاتجاه
  //     final currentIndex = page.floor().clamp(0, 603);
  //     final delta = page - currentIndex;

  //     // اقتراب من الحافة اليمنى/اليسرى لبدء تحميل مبكر
  //     const threshold = 0.82; // بدء التهيئة قبل السنيب
  //     int? targetNeighbor;
  //     if (delta > threshold && currentIndex + 1 < 604) {
  //       targetNeighbor = currentIndex + 1; // يسار -> الصفحة التالية (RTL)
  //     } else if (delta < (1 - threshold) && currentIndex - 1 >= 0) {
  //       // عند بداية الصفحة، حضّر السابقة
  //       targetNeighbor = currentIndex - 1;
  //     }

  //     if (targetNeighbor != null &&
  //         targetNeighbor != _lastPrefetchedForPage) {
  //       _lastPrefetchedForPage = targetNeighbor;
  //       // اختيار عدد الجيران بحسب الجهاز: هواتف ±1، غيرها ±2
  //       final isWeak =
  //           Platform.isAndroid || Platform.isIOS || Platform.isFuchsia;
  //       final neighborOffsets =
  //           isWeak ? const [0, 1, -1] : const [0, 1, -1, 2, -2];
  //       final targets = neighborOffsets
  //           .map((o) => targetNeighbor! + o)
  //           .where((i) => i >= 0 && i < 604)
  //           .toList();

  //       // جدولة بوقت الخمول لضمان عدم حجب UI
  //       SchedulerBinding.instance.scheduleTask(() async {
  //         if (!isDownloadFonts) return;
  //         for (final t in targets) {
  //           try {
  //             await prepareFonts(t, isFontsLocal: false);
  //           } catch (_) {
  //             // تجاهل أخطاء التهيئة المسبقة
  //           }
  //         }
  //       }, Priority.idle);
  //     }
  //   });
  // }

  return quranPagesController;
}