build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  final quranCtrl = QuranCtrl.instance;

  // تهيئة كنترولر الصوت
  // Initialize audio controller
  AudioCtrl.instance;

  // تحديث رابط أيقونة التطبيق إذا تم تمريره
  // Update app icon URL if provided
  if (appIconUrlForPlayAudioInBackground != null &&
      appIconUrlForPlayAudioInBackground!.isNotEmpty) {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (context.mounted) {
        AudioCtrl.instance
            .updateAppIconUrl(appIconUrlForPlayAudioInBackground!);
      }
    });
  }

  // تفعيل تحديد الكلمات
  // Enable word selection
  WordInfoCtrl.instance.isWordSelectionEnabled = enableWordSelection;

  final String deviceLocale = Localizations.localeOf(context).languageCode;
  final String languageCode = appLanguageCode ?? deviceLocale;

  return PopScope(
    onPopInvokedWithResult: (b, _) async {
      QuranCtrl.instance.state.isShowMenu.value = false;
    },
    child: ScaleKitBuilder(
      designWidth: 375,
      designHeight: 812,
      designType: DeviceType.mobile,
      child: QuranLibraryTheme(
        snackBarStyle: snackBarStyle ??
            SnackBarStyle.defaults(isDark: isDark, context: context),
        ayahLongClickStyle: ayahMenuStyle ??
            AyahMenuStyle.defaults(isDark: isDark, context: context),
        indexTabStyle: indexTabStyle ??
            IndexTabStyle.defaults(isDark: isDark, context: context),
        topBarStyle: topBarStyle ??
            QuranTopBarStyle.defaults(isDark: isDark, context: context),
        tajweedMenuStyle: tajweedMenuStyle ??
            TajweedMenuStyle.defaults(isDark: isDark, context: context),
        searchTabStyle: searchTabStyle ??
            SearchTabStyle.defaults(isDark: isDark, context: context),
        surahInfoStyle: surahInfoStyle ??
            SurahInfoStyle.defaults(isDark: isDark, context: context),
        tafsirStyle: tafsirStyle ??
            TafsirStyle.defaults(isDark: isDark, context: context),
        bookmarksTabStyle: bookmarksTabStyle ??
            BookmarksTabStyle.defaults(isDark: isDark, context: context),
        topBottomQuranStyle: topBottomQuranStyle ??
            TopBottomQuranStyle.defaults(isDark: isDark, context: context),
        ayahDownloadManagerStyle: ayahDownloadManagerStyle ??
            AyahDownloadManagerStyle.defaults(
                isDark: isDark, context: context),
        child: GetBuilder<SurahCtrl>(
          init: SurahCtrl.instance,
          initState: (state) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              if (!context.mounted) return;

              // على الويب: لا تسرق التركيز من حقول الكتابة
              // On web: don't steal focus from text fields
              if (kIsWeb) {
                final pf = FocusManager.instance.primaryFocus;
                final isTextFieldFocused =
                    pf?.context?.widget is EditableText;
                if (!isTextFieldFocused) {
                  FocusScope.of(context)
                      .requestFocus(quranCtrl.state.quranPageRLFocusNode);
                }
              }

              final ctrl = state.controller!;
              // تحميل السورة عند التهيئة أو إعادة التحميل إذا تغيّر الرقم أو كانت الصفحات فارغة
              // Load surah on init or reload if number changed or pages are empty
              if (ctrl.surahNumber != surahNumber ||
                  ctrl.surahPages.isEmpty) {
                ctrl.loadSurah(surahNumber).then((_) {
                  // تحضير خطوط QPC v4 لصفحات السورة بعد التحميل
                  // Prewarm QPC v4 fonts for surah pages after loading
                  if (ctrl.surahPages.isNotEmpty) {
                    final firstRealPage =
                        ctrl.surahPages.first.pageNumber - 1;
                    quranCtrl.prewarmQpcV4Pages(firstRealPage);
                  }
                });
              }
            });
          },
          builder: (surahCtrl) {
            return Directionality(
              textDirection: TextDirection.rtl,
              child: Scaffold(
                resizeToAvoidBottomInset: false,
                backgroundColor:
                    backgroundColor ?? AppColors.getBackgroundColor(isDark),
                body: SafeArea(
                  child: Stack(
                    alignment: Alignment.center,
                    children: [
                      // محتوى السورة مع دعم التكبير/التصغير
                      // Surah content with pinch-to-zoom support
                      GestureDetector(
                        onScaleStart: (details) => quranCtrl
                            .state
                            .baseScaleFactor
                            .value = quranCtrl.state.scaleFactor.value,
                        onScaleUpdate: (details) =>
                            _onScaleUpdate(details, quranCtrl),
                        onScaleEnd: (_) {
                          if (quranCtrl.state.isScaling.value) {
                            quranCtrl.state.isScaling.value = false;
                            quranCtrl.update();
                          }
                        },
                        child: InkWell(
                          onTap: () {
                            if (onPagePress != null) {
                              onPagePress!();
                            } else {
                              quranCtrl.showControlToggle();
                              quranCtrl.state.isShowMenu.value = false;
                            }
                          },
                          focusColor: Colors.transparent,
                          splashColor: Colors.transparent,
                          highlightColor: Colors.transparent,
                          child: _buildSurahBody(
                              parentContext, surahCtrl, quranCtrl),
                        ),
                      ),
                      // طبقة عناصر التحكم (شريط الأعلى + الصوت)
                      // Controls overlay (top bar + audio)
                      _SurahControlWidget(
                        isShowAudioSlider: isShowAudioSlider,
                        ayahStyle: ayahStyle,
                        isDark: isDark,
                        languageCode: languageCode,
                        ayahDownloadManagerStyle: ayahDownloadManagerStyle,
                        backgroundColor: backgroundColor,
                        textColor: textColor,
                        appBar: appBar,
                        useDefaultAppBar: useDefaultAppBar,
                        surahStyle: surahStyle,
                        downloadFontsDialogStyle: downloadFontsDialogStyle,
                        isFontsLocal: isFontsLocal,
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
        ),
      ),
    ),
  );
}