build method

  1. @override
Widget build(
  1. BuildContext overlayContext
)
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 overlayContext) {
  final rootContext = context;

  void close() {
    QuranCtrl.instance.state.isShowMenu.value = false;
    onDismiss?.call();
  }

  final themed = AyahLongClickTheme.of(rootContext)?.style;
  final s = themed ??
      AyahMenuStyle.defaults(isDark: isDark, context: overlayContext);

  // الحصول على نمط الصوت / Get audio style
  final sAudio =
      AyahAudioStyle.defaults(isDark: isDark, context: overlayContext);

  // محاولة الحصول على نمط مدير التحميل من الثيم أولاً / Try to get download manager style from theme first
  final themedDownloadManager =
      AyahDownloadManagerTheme.of(rootContext)?.style;
  final sDownloadManager = themedDownloadManager ??
      AyahDownloadManagerStyle.defaults(
          isDark: isDark, context: overlayContext);

  final List<Widget> customMenuItems = s.customMenuItems ?? const [];

  // الحصول على أبعاد الشاشة والهوامش الآمنة / Get screen dimensions and safe area
  final screenSize = MediaQuery.of(overlayContext).size;
  final padding = MediaQuery.of(overlayContext).padding;

  // حساب عدد العناصر الفعلي بناءً على الأزرار المرئية / Count all visible items
  int itemsCount = customMenuItems.length;
  if (s.showPlayButton ?? true) itemsCount += 1;
  if ((s.showPlayAllButton ?? true) && !kIsWeb) itemsCount += 1;
  if (s.showTafsirButton ?? true) itemsCount += 1;
  if (s.showCopyButton ?? true) itemsCount += 1;
  if (s.showBookmarkButtons ?? true) {
    itemsCount += (s.bookmarkColorCodes?.length ?? 3);
  }
  final iconSz = s.iconSize ?? 24.0;
  final hPad = s.iconHorizontalPadding ?? 8.0;
  double dialogWidth = (itemsCount * (iconSz + hPad * 2)) +
      28; // أيقونة + تباعد + هوامش الحاوية
  // تأكد من عدم تجاوز عرض الشاشة / Ensure dialog doesn't exceed screen width
  final maxWidth = screenSize.width - padding.left - padding.right - 20;
  if (dialogWidth > maxWidth) dialogWidth = maxWidth;
  double dialogHeight = 80; // ارتفاع الحوار / Dialog height

  // حساب الموضع الأفقي مع التأكد من البقاء داخل الشاشة / Calculate horizontal position ensuring it stays within screen
  double left = position.dx - (dialogWidth / 2);

  // التحقق من الحواف اليسرى واليمنى / Check left and right edges
  if (left < padding.left + 10) {
    left = padding.left + 10; // هامش من الحافة اليسرى / Left margin
  } else if (left + dialogWidth > screenSize.width - padding.right - 10) {
    left = screenSize.width -
        padding.right -
        dialogWidth -
        10; // هامش من الحافة اليمنى / Right margin
  }

  // حساب الموضع العمودي مع التأكد من البقاء داخل الشاشة / Calculate vertical position ensuring it stays within screen
  double top = position.dy -
      dialogHeight +
      10; // زيادة المسافة إلى 10 بكسل / Increase distance to 10 pixels

  // التحقق من الحافة العلوية / Check top edge
  if (top < padding.top + 10) {
    top = position.dy +
        10; // إظهار الحوار تحت النقر مع مسافة أقل / Show dialog below tap with less distance
  }

  // التحقق من الحافة السفلية / Check bottom edge
  if (top + dialogHeight > screenSize.height - padding.bottom - 10) {
    top = screenSize.height -
        padding.bottom -
        dialogHeight -
        10; // هامش من الحافة السفلى / Bottom margin
  }
  return Obx(
    () => QuranCtrl.instance.state.isShowMenu.value
        ? Positioned(
            top: top,
            left: left,
            child: Container(
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(
                      Radius.circular(s.outerBorderRadius ?? 8.0)),
                  color: s.backgroundColor,
                  boxShadow: s.boxShadow),
              child: Container(
                padding: s.padding ??
                    const EdgeInsets.symmetric(
                        horizontal: 6.0, vertical: 2.0),
                margin: s.margin ?? const EdgeInsets.all(4.0),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(
                      Radius.circular(s.borderRadius ?? 6.0)),
                  border: Border.all(
                    width: s.borderWidth ?? 2.0,
                    color: s.borderColor ??
                        Theme.of(overlayContext)
                            .colorScheme
                            .primary
                            .withValues(alpha: 0.1),
                  ),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: () {
                    final List<Widget> widgets = [];
                    Widget divider() => overlayContext.verticalDivider(
                          height: s.dividerHeight,
                          color: s.dividerColor,
                        );

                    void addDividerIfNeeded() {
                      if (widgets.isNotEmpty) widgets.add(divider());
                    }

                    // عناصر إضافية مخصّصة (من الستايل ثم الاستدعاء)
                    if (customMenuItems.isNotEmpty) {
                      for (final item in customMenuItems) {
                        addDividerIfNeeded();
                        widgets.add(
                          GestureDetector(
                            behavior: HitTestBehavior.translucent,
                            onTap: () {
                              close();
                            },
                            child: item,
                          ),
                        );
                      }
                    }

                    // زر تشغيل جميع الآيات
                    if (s.showPlayButton ?? true) {
                      addDividerIfNeeded();
                      widgets.add(
                        GestureDetector(
                          onTap: () {
                            close();

                            AudioCtrl.instance.playAyah(
                              rootContext,
                              ayah!.ayahUQNumber,
                              playSingleAyah: true,
                              ayahAudioStyle: sAudio,
                              ayahDownloadManagerStyle: sDownloadManager,
                              isDarkMode: isDark,
                            );
                            log('Second Menu Child Tapped: ${ayah!.ayahUQNumber}');
                          },
                          child: Icon(
                            s.playIconData,
                            color: s.playIconColor,
                            size: s.iconSize,
                          ),
                        ),
                      );
                    }

                    // زر تشغيل جميع الآيات
                    if ((s.showPlayAllButton ?? true) && !kIsWeb) {
                      addDividerIfNeeded();
                      widgets.add(
                        GestureDetector(
                          onTap: () {
                            close();

                            AudioCtrl.instance.playAyah(
                              rootContext,
                              ayah!.ayahUQNumber,
                              playSingleAyah: false,
                              ayahAudioStyle: sAudio,
                              ayahDownloadManagerStyle: sDownloadManager,
                              isDarkMode: isDark,
                            );
                            log('Second Menu Child Tapped: ${ayah!.ayahUQNumber}');
                          },
                          child: Icon(
                            s.playAllIconData,
                            color: s.playAllIconColor,
                            size: s.iconSize,
                          ),
                        ),
                      );
                    }

                    // زر التفسير
                    if (s.showTafsirButton ?? true) {
                      addDividerIfNeeded();
                      widgets.add(
                        GestureDetector(
                          onTap: () {
                            close();
                            showTafsirOnTap(
                              context: rootContext,
                              isDark: isDark,
                              ayahNum: ayah!.ayahNumber,
                              pageIndex: pageIndex,
                              ayahUQNum: ayah!.ayahUQNumber,
                              ayahNumber: ayah!.ayahNumber,
                              externalTafsirStyle: externalTafsirStyle,
                            );
                          },
                          child: Icon(
                            s.tafsirIconData,
                            color: s.tafsirIconColor,
                            size: s.iconSize,
                          ),
                        ),
                      );
                    }

                    // زر النسخ
                    if (s.showCopyButton ?? true) {
                      addDividerIfNeeded();
                      widgets.add(
                        GestureDetector(
                          onTap: () {
                            Clipboard.setData(
                                ClipboardData(text: ayah!.text));
                            close();
                          },
                          child: Icon(
                            s.copyIconData,
                            color: s.copyIconColor,
                            size: s.iconSize,
                          ),
                        ),
                      );
                    }

                    // أزرار العلامات المرجعية
                    if (s.showBookmarkButtons ?? true) {
                      final colors = s.bookmarkColorCodes ??
                          const [
                            0xAAFFD354,
                            0xAAF36077,
                            0xAA00CD00,
                          ];
                      for (final colorCode in colors) {
                        widgets.add(
                          Padding(
                            padding: EdgeInsets.symmetric(
                              horizontal: s.iconHorizontalPadding ?? 8.0,
                            ),
                            child: GestureDetector(
                              onTap: () {
                                BookmarksCtrl.instance.saveBookmark(
                                  surahName: QuranCtrl.instance
                                      .getSurahDataByAyah(ayah!)
                                      .arabicName,
                                  ayahNumber: ayah!.ayahNumber,
                                  ayahId: ayah!.ayahUQNumber,
                                  page: ayah!.page,
                                  colorCode: colorCode,
                                );
                                close();
                              },
                              child: Icon(
                                s.bookmarkIconData,
                                color: Color(colorCode),
                                size: s.iconSize,
                              ),
                            ),
                          ),
                        );
                      }
                    }

                    return widgets;
                  }(),
                ),
              ),
            ),
          )
        : const SizedBox.shrink(),
  );
}