jjDropdown<T extends IMaster> function

Widget jjDropdown<T extends IMaster>({
  1. required String title,
  2. required List<T> items,
  3. required ValueChanged<T?> onChanged,
  4. T? selected,
  5. VoidCallback? onAdd,
  6. VoidCallback? onError,
})

Implementation

Widget jjDropdown<T extends IMaster>({
  required String title,
  required List<T> items,
  required ValueChanged<T?> onChanged,
  T? selected,
  VoidCallback? onAdd,
  VoidCallback? onError,
}) {
  try {
    return Row(
      children: [
        Expanded(
          child: Stack(
            children: [
              // Text(
              //   title,
              //   style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
              // ),
              // SizedBox(height: 4),
              Padding(
                padding: title.isNotEmpty
                    ? const EdgeInsets.only(top: 16)
                    : const EdgeInsets.all(0),
                child: DropdownButtonFormField<String>(
                  initialValue: selected?.uuid,
                  decoration: InputDecoration(
                    isDense: true,
                    // contentPadding: EdgeInsets.zero,
                    contentPadding: const EdgeInsets.only(
                      top: 12,
                      left: 12,
                      right: 12,
                      bottom: 4,
                    ),
                    // border: OutlineInputBorder(
                    //   borderRadius: BorderRadius.circular(3),
                    //   borderSide: const BorderSide(
                    //     color: JColors.colorEntryStroke,
                    //   ), // اللون الافتراضي
                    // ),
                    enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(3),
                      borderSide: const BorderSide(
                        color: JColors.colorEntryStroke,
                      ),
                    ),

                    focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(3),
                      borderSide: const BorderSide(
                        color: JColors.yellow,
                        width: 2,
                      ), // اللون عند الفتح/التركيز
                    ),
                  ),
                  hint: Text('اختر $title'),
                  // isExpanded: true,
                  borderRadius: BorderRadius.circular(5),
                  alignment: AlignmentGeometry.bottomRight,
                  dropdownColor: JColors.jBlue3,
                  selectedItemBuilder: (context) {
                    return items.map((item) {
                      return Padding(
                        padding: const EdgeInsets.only(top: 4),
                        child: Container(
                          alignment: Alignment.bottomCenter,
                          child: Text(
                            item.toString(),
                            // style: TextStyle(
                            //   // إذا كان هذا العنصر هو المختار → لونه أزرق
                            //   color: value == item ? Colors.blue : Colors.black,
                            // ),
                          ),
                        ),
                      );
                    }).toList();
                  },
                  style: GoogleFonts.tajawal(
                    color: Colors.black,
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                  items: items.map((item) {
                    return DropdownMenuItem(
                      value: item.uuid,
                      alignment: Alignment.center,
                      child: Text(
                        item.toString(),
                        style: TextStyle(
                          // إذا كان هذا العنصر هو المختار → لونه أزرق
                          color: selected == item
                              ? JColors.yellow
                              : Colors.white,
                        ),
                      ),
                    );
                  }).toList(),
                  onChanged: (value) {
                    final item = items.firstWhere((x) => x.uuid == value);
                    onChanged(item);
                  },
                ),
              ),
              if (title.isNotEmpty)
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  child: Container(
                    decoration: BoxDecoration(
                      color: JColors.colorEntryBackground,
                      border: Border.all(color: JColors.colorEntryStroke),
                      borderRadius: BorderRadius.circular(3),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.only(
                        left: 8.0,
                        top: 2,
                        right: 8,
                      ),
                      child: Text(
                        title,
                        style: JColors.styleEntryLabel.copyWith(
                          color: JColors.colorEntryStroke,
                        ),
                      ),
                    ),
                  ),
                ),
            ],
          ),
        ),
        if (onAdd != null) ...[
          SizedBox(width: 8),
          Padding(
            padding: const EdgeInsets.only(top: 18.0),
            child: SizedBox(
              height: 48,
              child: OutlinedButton(
                onPressed: () {
                  try {
                    onAdd();
                  } catch (e) {
                    jLog.e('Error in onAdd callback: $e');
                  }
                },
                style: OutlinedButton.styleFrom(
                  foregroundColor: JColors.green,
                  // backgroundColor: JColors.yellow,
                  side: BorderSide(width: 0, color: JColors.green),
                  padding: const EdgeInsets.symmetric(horizontal: 8),
                  minimumSize: const Size(48, 48),
                ),
                child: const Icon(Icons.add, size: 20),
              ),
            ),
          ),
        ],
      ],
    );
  } catch (e) {
    jLog.e('Error logging dropdown build: $e');
    if (selected == null) {
      return Text(
        'Error logging dropdown build: $title',
        overflow: TextOverflow.ellipsis,
        style: TextStyle(color: JColors.jRed),
        textAlign: TextAlign.center,
      );
    }
    selected = null;
    onError?.call();
    return jjDropdown<T>(
      title: title,
      selected: selected,
      items: items,
      onChanged: onChanged,
      onAdd: onAdd,
    );
  }
}