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 (appIconUrlForPlayAudioInBackground != null &&
      appIconUrlForPlayAudioInBackground!.isNotEmpty) {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      AudioCtrl.instance
          .updateAppIconUrl(appIconUrlForPlayAudioInBackground!);
    });
  }

  // if (isDark!) {
  //   QuranCtrl.instance.state.isTajweed.value = 1;
  //   GetStorage().write(StorageConstants().isTajweed, 1);
  // }
  return ScreenUtilInit(
    designSize: const Size(392.72727272727275, 800.7272727272727),
    minTextAdapt: true,
    splitScreenMode: true,
    // Use builder only if you need to use library outside ScreenUtilInit context
    builder: (_, __) => GetBuilder<QuranCtrl>(
      builder: (quranCtrl) {
        return Directionality(
          textDirection: TextDirection.rtl,
          child: Scaffold(
            backgroundColor:
                backgroundColor ?? AppColors.getBackgroundColor(isDark),
            body: SafeArea(
              child: Stack(
                alignment: Alignment.center,
                children: [
                  withPageView
                      ? NotificationListener<ScrollEndNotification>(
                          onNotification: (notification) {
                            // نلتقط نهاية السحب فقط لـ PageView عبر PageMetrics
                            final metrics = notification.metrics;
                            if (metrics is PageMetrics) {
                              final int current = (metrics.page ??
                                      (metrics.pixels /
                                          metrics.viewportDimension))
                                  .round();
                              if (quranCtrl.isDownloadFonts) {
                                // تنفيذ بعد انتهاء الإطار لتجنّب أي تجميد
                                Future.microtask(() => quranCtrl.prepareFonts(
                                    current,
                                    isFontsLocal: isFontsLocal!));
                              }
                            }
                            return false; // لا نستهلك الإشعار
                          },
                          child: PageView.builder(
                            itemCount: 604,
                            controller: quranCtrl.getPageController(context),
                            padEnds: false,
                            // شرح: اختيار نوع الفيزياء حسب إعداد التحسين
                            // Explanation: Choose physics type based on optimization setting
                            physics: const ClampingScrollPhysics(),
                            // شرح: إضافة allowImplicitScrolling لتحسين الأداء
                            // Explanation: Adding allowImplicitScrolling for better performance
                            allowImplicitScrolling: false,
                            // شرح: إضافة clipBehavior لتحسين الرسم
                            // Explanation: Adding clipBehavior for better rendering
                            clipBehavior: Clip.hardEdge,
                            // شرح: تحسين معالجة تغيير الصفحة لتقليل التقطيع
                            // Explanation: Optimized page change handling to reduce stuttering
                            onPageChanged: (pageIndex) {
                              // تشغيل العمليات في الخلفية لتجنب تجميد UI
                              // Run operations in background to avoid UI freeze
                              WidgetsBinding.instance
                                  .addPostFrameCallback((_) async {
                                if (onPageChanged != null) {
                                  onPageChanged!(pageIndex);
                                } else {
                                  quranCtrl.state.overlayEntry?.remove();
                                  quranCtrl.state.overlayEntry = null;
                                }
                                quranCtrl.state.currentPageNumber.value =
                                    pageIndex + 1;
                                quranCtrl.saveLastPage(pageIndex + 1);
                              });
                            },
                            pageSnapping: true,
                            itemBuilder: (ctx, index) {
                              // شرح: تحسين أداء itemBuilder لتقليل التقطيع
                              // Explanation: Optimize itemBuilder performance to reduce stuttering
                              return InkWell(
                                onTap: () {
                                  if (onPagePress != null) {
                                    onPagePress!();
                                  } else {
                                    quranCtrl.showControlToggle();
                                    quranCtrl.clearSelection();
                                    quranCtrl.state.overlayEntry?.remove();
                                    quranCtrl.state.overlayEntry = null;
                                  }
                                },
                                focusColor: Colors.transparent,
                                splashColor: Colors.transparent,
                                highlightColor: Colors.transparent,
                                child: RepaintBoundary(
                                  key: ValueKey('quran_page_$index'),
                                  child: PageViewBuild(
                                    circularProgressWidget:
                                        circularProgressWidget,
                                    languageCode: languageCode,
                                    juzName: juzName,
                                    sajdaName: sajdaName,
                                    topTitleChild: topTitleChild,
                                    bookmarkList: bookmarkList,
                                    ayahSelectedFontColor:
                                        ayahSelectedFontColor,
                                    textColor: textColor,
                                    ayahIconColor: ayahIconColor,
                                    showAyahBookmarkedIcon:
                                        showAyahBookmarkedIcon,
                                    onAyahLongPress: onAyahLongPress,
                                    bookmarksColor: bookmarksColor,
                                    surahInfoStyle: surahInfoStyle,
                                    surahNameStyle: surahNameStyle,
                                    bannerStyle: bannerStyle,
                                    basmalaStyle: basmalaStyle,
                                    onSurahBannerPress: onSurahBannerPress,
                                    surahNumber: surahNumber,
                                    ayahSelectedBackgroundColor:
                                        ayahSelectedBackgroundColor,
                                    onPagePress: onPagePress,
                                    isDark: isDark,
                                    fontsName: fontsName,
                                    ayahBookmarked: ayahBookmarked,
                                    anotherMenuChild: anotherMenuChild,
                                    anotherMenuChildOnTap:
                                        anotherMenuChildOnTap,
                                    secondMenuChild: secondMenuChild,
                                    secondMenuChildOnTap:
                                        secondMenuChildOnTap,
                                    userContext: parentContext,
                                    pageIndex: index,
                                    quranCtrl: quranCtrl,
                                    isFontsLocal: isFontsLocal!,
                                  ),
                                ),
                              );
                            },
                          ))
                      : PageViewBuild(
                          circularProgressWidget: circularProgressWidget,
                          languageCode: languageCode,
                          juzName: juzName,
                          sajdaName: sajdaName,
                          topTitleChild: topTitleChild,
                          bookmarkList: bookmarkList,
                          ayahSelectedFontColor: ayahSelectedFontColor,
                          textColor: textColor,
                          ayahIconColor: ayahIconColor,
                          showAyahBookmarkedIcon: showAyahBookmarkedIcon,
                          onAyahLongPress: onAyahLongPress,
                          bookmarksColor: bookmarksColor,
                          surahInfoStyle: surahInfoStyle,
                          surahNameStyle: surahNameStyle,
                          bannerStyle: bannerStyle,
                          basmalaStyle: basmalaStyle,
                          onSurahBannerPress: onSurahBannerPress,
                          surahNumber: surahNumber,
                          ayahSelectedBackgroundColor:
                              ayahSelectedBackgroundColor,
                          onPagePress: onPagePress,
                          isDark: isDark,
                          fontsName: fontsName,
                          ayahBookmarked: ayahBookmarked,
                          anotherMenuChild: anotherMenuChild,
                          anotherMenuChildOnTap: anotherMenuChildOnTap,
                          secondMenuChild: secondMenuChild,
                          secondMenuChildOnTap: secondMenuChildOnTap,
                          userContext: parentContext,
                          pageIndex: pageIndex,
                          quranCtrl: quranCtrl,
                          isFontsLocal: isFontsLocal!,
                        ),
                  GetBuilder<QuranCtrl>(
                    id: 'isShowControl',
                    builder: (quranCtrl) => Stack(
                      alignment: Alignment.center,
                      children: [
                        // السلايدر السفلي - يظهر من الأسفل للأعلى
                        // Bottom slider - appears from bottom to top
                        isShowAudioSlider!
                            ? BottomSlider(
                                isVisible:
                                    QuranCtrl.instance.isShowControl.value,
                                onClose: () {
                                  QuranCtrl.instance.isShowControl.value =
                                      false;
                                  SliderController.instance
                                      .hideBottomContent();
                                },
                                isDark: isDark,
                                sliderHeight: UiHelper.currentOrientation(
                                    0.0, 40.0, context),
                                style: ayahStyle ?? AyahAudioStyle(),
                                contentChild: SizedBox.shrink(),
                                child: Flexible(
                                  child: AyahsAudioWidget(
                                      style: ayahStyle ?? AyahAudioStyle()),
                                ),
                              )
                            : SizedBox.shrink(),
                        appBar == null &&
                                useDefaultAppBar &&
                                quranCtrl.isShowControl.value
                            ? _QuranTopBar(
                                languageCode ?? 'ar',
                                isDark,
                                style: surahStyle ?? SurahAudioStyle(),
                                backgroundColor: backgroundColor,
                                downloadFontsDialogStyle:
                                    downloadFontsDialogStyle,
                                isFontsLocal: isFontsLocal,
                                topBarStyle: topBarStyle ??
                                    QuranTopBarStyle.defaults(isDark: isDark),
                              )
                            : SizedBox.shrink(),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    ),
  );
}