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

  // if (isDark!) {
  //   QuranCtrl.instance.state.isTajweed.value = 1;
  //   GetStorage().write(StorageConstants().isTajweed, 1);
  // }
  final String deviceLocale = Localizations.localeOf(context).languageCode;
  final String languageCode = appLanguageCode ?? deviceLocale;
  // ضبط الصفحة فقط إذا مُرِّر pageIndex غير صفري — لتجنّب إعادة تعيين الصفحة المحفوظة
  if (pageIndex > 0) {
    QuranCtrl.instance.state.currentPageNumber.value = pageIndex + 1;
  }
  WordInfoCtrl.instance.isWordSelectionEnabled = enableWordSelection;
  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),
          displayModeBarStyle: displayModeBarStyle ??
              DisplayModeBarStyle.defaults(isDark: isDark, context: context),
          ayahTafsirInlineStyle: ayahTafsirInlineStyle ??
              AyahTafsirInlineStyle.defaults(
                  isDark: isDark, context: context),
          quranTafsirSideStyle: quranTafsirSideStyle ??
              QuranTafsirSideStyle.defaults(isDark: isDark, context: context),
          child: GetBuilder<QuranCtrl>(
            builder: (quranCtrl) {
              // تهيئة خاملة لخطوط الصفحات المجاورة حول الصفحة الحالية بعد أول إطار
              WidgetsBinding.instance.addPostFrameCallback((_) {
                if (!context.mounted) return;
                // على الويب: لا تسرق التركيز من حقول الكتابة
                if (kIsWeb) {
                  final pf = FocusManager.instance.primaryFocus;
                  final isTextFieldFocused =
                      pf?.context?.widget is EditableText;
                  if (!isTextFieldFocused) {
                    FocusScope.of(context)
                        .requestFocus(quranCtrl.state.quranPageRLFocusNode);
                  }
                }

                // عند تعطيل PageView، لن يتم استدعاء onPageChanged، وبالتالي لن يتم
                // تحضير الخطوط/بيانات QPC v4 تلقائياً. نُطلق التحضير هنا مرة واحدة
                // بعد أول إطار لضمان عدم بقاء الصفحة على مؤشر التحميل.
                if (!withPageView) {
                  Future(() async {
                    await quranCtrl.prewarmQpcV4Pages(pageIndex);
                  });
                }
              });
              return Scaffold(
                backgroundColor:
                    backgroundColor ?? AppColors.getBackgroundColor(isDark),
                body: SafeArea(
                  child: Stack(
                    alignment: Alignment.center,
                    children: [
                      Directionality(
                        textDirection: TextDirection.rtl,
                        child: GestureDetector(
                          onScaleStart: (details) => quranCtrl
                              .state
                              .baseScaleFactor
                              .value = quranCtrl.state.scaleFactor.value,
                          onScaleUpdate: (ScaleUpdateDetails details) {
                            // عند وجود إصبعين أو أكثر نعتبرها عملية تكبير/تصغير ونوقف سكرول الصفحات
                            _onScaleUpdate(details, quranCtrl);
                          },
                          onScaleEnd: (_) {
                            if (quranCtrl.state.isScaling.value) {
                              quranCtrl.state.isScaling.value = false;
                              quranCtrl.update();
                            }
                          },
                          child: Obx(() {
                            final mode = quranCtrl.state.displayMode.value;
                            switch (mode) {
                              case QuranDisplayMode.singleScrollable:
                                return SingleScrollablePage(
                                  quranCtrl: quranCtrl,
                                  isDark: isDark,
                                  languageCode: languageCode,
                                  onPageChanged: (idx) =>
                                      _onPageChange(context, idx, quranCtrl),
                                  onPagePress: onPagePress,
                                  circularProgressWidget:
                                      circularProgressWidget,
                                  bookmarkList: bookmarkList,
                                  ayahSelectedFontColor:
                                      ayahSelectedFontColor,
                                  textColor: textColor,
                                  ayahIconColor: ayahIconColor,
                                  showAyahBookmarkedIcon:
                                      showAyahBookmarkedIcon,
                                  onAyahLongPress: onAyahLongPress,
                                  bookmarksColor: bookmarksColor,
                                  surahNameStyle: surahNameStyle,
                                  bannerStyle: bannerStyle,
                                  basmalaStyle: basmalaStyle,
                                  onSurahBannerPress: onSurahBannerPress,
                                  surahNumber: surahNumber,
                                  ayahSelectedBackgroundColor:
                                      ayahSelectedBackgroundColor,
                                  fontsName: fontsName,
                                  ayahBookmarked: ayahBookmarked,
                                  isAyahBookmarked: isAyahBookmarked,
                                  parentContext: parentContext,
                                  isFontsLocal: isFontsLocal,
                                );
                              case QuranDisplayMode.dualPage:
                                return DualPageView(
                                  quranCtrl: quranCtrl,
                                  isDark: isDark,
                                  languageCode: languageCode,
                                  onPageChanged: (idx) =>
                                      _onPageChange(context, idx, quranCtrl),
                                  onPagePress: onPagePress,
                                  circularProgressWidget:
                                      circularProgressWidget,
                                  bookmarkList: bookmarkList,
                                  ayahSelectedFontColor:
                                      ayahSelectedFontColor,
                                  textColor: textColor,
                                  ayahIconColor: ayahIconColor,
                                  showAyahBookmarkedIcon:
                                      showAyahBookmarkedIcon,
                                  onAyahLongPress: onAyahLongPress,
                                  bookmarksColor: bookmarksColor,
                                  surahNameStyle: surahNameStyle,
                                  bannerStyle: bannerStyle,
                                  basmalaStyle: basmalaStyle,
                                  onSurahBannerPress: onSurahBannerPress,
                                  surahNumber: surahNumber,
                                  ayahSelectedBackgroundColor:
                                      ayahSelectedBackgroundColor,
                                  fontsName: fontsName,
                                  ayahBookmarked: ayahBookmarked,
                                  isAyahBookmarked: isAyahBookmarked,
                                  parentContext: parentContext,
                                  isFontsLocal: isFontsLocal,
                                );
                              case QuranDisplayMode.quranWithTafsirSide:
                                return QuranWithTafsirSide(
                                  quranCtrl: quranCtrl,
                                  isDark: isDark,
                                  languageCode: languageCode,
                                  onPageChanged: (idx) =>
                                      _onPageChange(context, idx, quranCtrl),
                                  onPagePress: onPagePress,
                                  circularProgressWidget:
                                      circularProgressWidget,
                                  bookmarkList: bookmarkList,
                                  ayahSelectedFontColor:
                                      ayahSelectedFontColor,
                                  textColor: textColor,
                                  ayahIconColor: ayahIconColor,
                                  showAyahBookmarkedIcon:
                                      showAyahBookmarkedIcon,
                                  onAyahLongPress: onAyahLongPress,
                                  bookmarksColor: bookmarksColor,
                                  surahNameStyle: surahNameStyle,
                                  bannerStyle: bannerStyle,
                                  basmalaStyle: basmalaStyle,
                                  onSurahBannerPress: onSurahBannerPress,
                                  surahNumber: surahNumber,
                                  ayahSelectedBackgroundColor:
                                      ayahSelectedBackgroundColor,
                                  fontsName: fontsName,
                                  ayahBookmarked: ayahBookmarked,
                                  isAyahBookmarked: isAyahBookmarked,
                                  parentContext: parentContext,
                                  isFontsLocal: isFontsLocal,
                                );
                              case QuranDisplayMode.ayahWithTafsirInline:
                                return AyahWithTafsirInline(
                                  quranCtrl: quranCtrl,
                                  isDark: isDark,
                                  languageCode: languageCode,
                                  onPageChanged: (idx) =>
                                      _onPageChange(context, idx, quranCtrl),
                                  onPagePress: onPagePress,
                                  parentContext: parentContext,
                                  bannerStyle: bannerStyle,
                                  surahNameStyle: surahNameStyle,
                                  onSurahBannerPress: onSurahBannerPress,
                                  basmalaStyle: basmalaStyle,
                                  ayahBookmarked: ayahBookmarked,
                                  isAyahBookmarked: isAyahBookmarked,
                                  showAyahBookmarkedIcon:
                                      showAyahBookmarkedIcon,
                                  bookmarksColor: bookmarksColor,
                                  style: ayahTafsirInlineStyle ??
                                      AyahTafsirInlineStyle.defaults(
                                        isDark: isDark,
                                        context: context,
                                      ),
                                  audioStyle: ayahStyle ??
                                      AyahAudioStyle.defaults(
                                        isDark: isDark,
                                        context: context,
                                      ),
                                  ayahDownloadManagerStyle:
                                      ayahDownloadManagerStyle ??
                                          AyahDownloadManagerStyle.defaults(
                                            isDark: isDark,
                                            context: context,
                                          ),
                                );
                              case QuranDisplayMode.defaultMode:
                                return _buildDefaultPageView(
                                    context, quranCtrl, languageCode);
                            }
                          }),
                        ),
                      ),
                      _ControlWidget(
                        isShowAudioSlider: isShowAudioSlider,
                        ayahStyle: ayahStyle,
                        isDark: isDark,
                        languageCode: languageCode,
                        ayahDownloadManagerStyle: ayahDownloadManagerStyle,
                        backgroundColor: backgroundColor,
                        textColor: textColor,
                        appBar: appBar,
                        useDefaultAppBar: useDefaultAppBar,
                        surahStyle: surahStyle,
                        downloadFontsDialogStyle: downloadFontsDialogStyle,
                        isFontsLocal: isFontsLocal,
                        isShowTabBar: isShowTabBar,
                        topBarStyle: topBarStyle,
                      ),
                    ],
                  ),
                ),
              );
            },
          )),
    ),
  );
}