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) {
  // الحصول على أبعاد الشاشة والهوامش الآمنة / Get screen dimensions and safe area
  final screenSize = MediaQuery.of(context).size;
  final padding = MediaQuery.of(context).padding;

  // حساب العرض الفعلي للحوار بناءً على المحتوى / Calculate actual dialog width based on content
  int itemsCount =
      3; // عدد الأيقونات الأساسية (3 ألوان + نسخ + تفسير) / Basic icons count
  if (anotherMenuChild != null) {
    itemsCount += 1; // إضافة عنصر إضافي / Additional item
  }
  if (secondMenuChild != null) {
    itemsCount += 1; // إضافة عنصر إضافي / Additional item
  }
  double dialogWidth = (itemsCount * 40) +
      (itemsCount * 16) +
      40; // عرض كل أيقونة + التباعد + الهوامش / Icon width + spacing + margins
  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 Positioned(
    top: top,
    left: left,
    child: Container(
      decoration: BoxDecoration(
          borderRadius: const BorderRadius.all(Radius.circular(8.0)),
          color: AppColors.getBackgroundColor(isDark),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withValues(alpha: 0.3),
              blurRadius: 10,
              spreadRadius: 5,
              offset: const Offset(0, 5),
            )
          ]),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 6.0, vertical: 2.0),
        margin: const EdgeInsets.all(4.0),
        decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(6.0)),
            border: Border.all(width: 2, color: Colors.teal.shade100)),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ...[
              0xAAFFD354,
              0xAAF36077,
              0xAA00CD00
            ].map((colorCode) => Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 8.0),
                  child: GestureDetector(
                    onTap: () {
                      if (QuranCtrl.instance.state.fontsSelected.value == 1 ||
                          QuranCtrl.instance.state.fontsSelected.value == 2 ||
                          QuranCtrl.instance.state.scaleFactor.value > 1.3) {
                        BookmarksCtrl.instance.saveBookmark(
                          surahName: QuranCtrl.instance
                              .getSurahDataByAyah(ayah!)
                              .arabicName,
                          ayahNumber: ayah!.ayahNumber,
                          ayahId: ayah!.ayahUQNumber,
                          page: ayah!.page,
                          colorCode: colorCode,
                        );
                      } else {
                        BookmarksCtrl.instance.saveBookmark(
                          surahName: ayah!.arabicName!,
                          ayahNumber: ayah!.ayahNumber,
                          ayahId: ayah!.ayahUQNumber,
                          page: ayah!.page,
                          colorCode: colorCode,
                        );
                      }
                      QuranCtrl.instance.state.overlayEntry?.remove();
                      QuranCtrl.instance.state.overlayEntry = null;
                    },
                    child: Icon(
                      Icons.bookmark,
                      color: Color(colorCode),
                    ),
                  ),
                )),
            context.verticalDivider(height: 30, color: Colors.teal.shade100),
            GestureDetector(
              onTap: () {
                if (QuranCtrl.instance.state.fontsSelected.value == 1) {
                  Clipboard.setData(ClipboardData(text: ayah!.text));
                  ToastUtils().showToast(context, "تم النسخ الى الحافظة");
                } else {
                  Clipboard.setData(ClipboardData(
                      text: QuranCtrl
                          .instance.staticPages[ayah!.page - 1].ayahs
                          .firstWhere((element) =>
                              element.ayahUQNumber == ayah!.ayahUQNumber)
                          .text));
                  ToastUtils().showToast(context, "تم النسخ الى الحافظة");
                }
                QuranCtrl.instance.state.overlayEntry?.remove();
                QuranCtrl.instance.state.overlayEntry = null;
              },
              child: const Icon(
                Icons.copy_rounded,
                color: Colors.teal,
              ),
            ),
            context.verticalDivider(height: 30, color: Colors.teal.shade100),
            GestureDetector(
              onTap: () {
                showTafsirOnTap(
                  context: context,
                  isDark: isDark,
                  ayahNum: (QuranCtrl.instance.state.fontsSelected.value ==
                              1 ||
                          QuranCtrl.instance.state.fontsSelected.value == 2 ||
                          QuranCtrl.instance.state.scaleFactor.value > 1.3)
                      ? ayah!.ayahNumber
                      : ayah!.ayahNumber,
                  pageIndex: pageIndex,
                  ayahUQNum: (QuranCtrl.instance.state.fontsSelected.value ==
                              1 ||
                          QuranCtrl.instance.state.fontsSelected.value == 2 ||
                          QuranCtrl.instance.state.scaleFactor.value > 1.3)
                      ? ayah!.ayahUQNumber
                      : ayah!.ayahUQNumber,
                  ayahNumber: (QuranCtrl.instance.state.fontsSelected.value ==
                              1 ||
                          QuranCtrl.instance.state.fontsSelected.value == 2 ||
                          QuranCtrl.instance.state.scaleFactor.value > 1.3)
                      ? ayah!.ayahNumber
                      : ayah!.ayahNumber,
                  tafsirStyle: TafsirStyle(
                    backgroundColor: AppColors.getBackgroundColor(isDark),
                    tafsirNameWidget: Text(
                      'التفسير',
                      style: QuranLibrary().cairoStyle.copyWith(
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                            color: isDark ? Colors.white : Colors.black,
                          ),
                    ),
                    fontSizeWidget: fontSizeDropDown(
                      height: 30.0,
                      isDark: isDark,
                    ),
                  ),
                );
                QuranCtrl.instance.state.overlayEntry?.remove();
                QuranCtrl.instance.state.overlayEntry = null;
              },
              child: const Icon(
                Icons.text_snippet_rounded,
                color: Colors.teal,
              ),
            ),
            anotherMenuChild != null
                ? context.verticalDivider(
                    height: 30, color: Colors.teal.shade100)
                : const SizedBox(),
            anotherMenuChild != null
                ? GestureDetector(
                    onTap: () {
                      if (anotherMenuChildOnTap != null) {
                        anotherMenuChildOnTap!(ayah!);
                      }
                      QuranCtrl.instance.state.overlayEntry?.remove();
                      QuranCtrl.instance.state.overlayEntry = null;
                    },
                    child: anotherMenuChild ?? const SizedBox(),
                  )
                : const SizedBox(),
            secondMenuChild != null
                ? context.verticalDivider(
                    height: 30, color: Colors.teal.shade100)
                : const SizedBox(),
            secondMenuChild != null
                ? GestureDetector(
                    onTap: () {
                      if (secondMenuChildOnTap != null) {
                        secondMenuChildOnTap!(ayah!);
                      }
                      QuranCtrl.instance.state.overlayEntry?.remove();
                      QuranCtrl.instance.state.overlayEntry = null;
                    },
                    child: secondMenuChild ?? const SizedBox(),
                  )
                : const SizedBox(),
          ],
        ),
      ),
    ),
  );
}