customBottomSheet method

Future<T?> customBottomSheet({
  1. required BuildContext buildContext,
  2. required String headerName,
  3. TextStyle? headerNameTextStyle,
  4. Color? headerNameBackgroundColor,
  5. required List<CustomSelectItem<T>> dropdownItems,
  6. T? initialSelection,
  7. Color? selectedItemColor,
  8. TextStyle? selectedItemTextStyle,
  9. bool isBarrierDismissible = true,
  10. String cancelText = "Cancel",
  11. Color? separatorColor,
  12. double separatorHeight = 1,
  13. Color? cancelBackgroundColor,
  14. Color? dropdownItemBackgroundColor,
  15. TextStyle? cancelTextStyle,
  16. TextStyle? itemTextStyle,
})

Implementation

Future<T?> customBottomSheet(
    {required BuildContext buildContext,
    required String headerName,
    TextStyle? headerNameTextStyle,
    Color? headerNameBackgroundColor,
    required List<CustomSelectItem<T>> dropdownItems,
    T? initialSelection,
    Color? selectedItemColor,
    TextStyle? selectedItemTextStyle,
    bool isBarrierDismissible = true,
    String cancelText = "Cancel",
    Color? separatorColor,
    double separatorHeight = 1,
    Color? cancelBackgroundColor,
    Color? dropdownItemBackgroundColor,
    TextStyle? cancelTextStyle,
    TextStyle? itemTextStyle}) async {
  T? selectedItem = initialSelection;
  List<CustomSelectItem<T>> searchedItems = <CustomSelectItem<T>>[];

  if (selectedItem != null) {
    CustomSelectItem<T> value = dropdownItems
        .firstWhere((element) => element.buttonObjectValue == selectedItem);
    value.selected = true;
  }

  await showModalBottomSheet(
      isDismissible: isBarrierDismissible,
      context: buildContext,
      backgroundColor: Colors.transparent,
      enableDrag: true,
      builder: (BuildContext bc) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Container(
              clipBehavior: Clip.antiAlias,
              margin: EdgeInsets.symmetric(
                  horizontal: MediaQuery.of(bc).size.width * 0.05),
              decoration: BoxDecoration(
                color: dropdownItemBackgroundColor ?? Colors.grey.shade200,
                borderRadius: BorderRadius.circular(10),
              ),
              child: StatefulBuilder(builder: (_, setState) {
                return Column(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Container(
                      padding: const EdgeInsets.symmetric(vertical: 10),
                      color:
                          headerNameBackgroundColor ?? Colors.grey.shade200,
                      width: double.infinity,
                      alignment: Alignment.center,
                      child: Text(
                        headerName,
                        style: headerNameTextStyle ??
                            const TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                              color: Colors.black,
                            ),
                      ),
                    ),
                    ListView(
                      shrinkWrap: true,
                      children: [
                        for (CustomSelectItem<T> item
                            in searchedItems.isNotEmpty
                                ? searchedItems
                                : dropdownItems)
                          Column(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Container(
                                height: item == dropdownItems.first
                                    ? separatorHeight
                                    : (separatorHeight / 2),
                                width: double.infinity,
                                color: separatorColor ?? Colors.grey,
                              ),
                              CustomBottomSheetButton(
                                onPressed: () {
                                  selectedItem = item.buttonObjectValue;
                                  Navigator.pop(
                                    buildContext,
                                  );
                                },
                                buttonTextStyle: itemTextStyle?.copyWith(
                                        color: item.selected
                                            ? selectedItemColor
                                            : null) ??
                                    defaultTextStyle(
                                        color: item.selected
                                            ? (selectedItemColor ??
                                                Colors.black)
                                            : Colors.black),
                                // buttonTextColor: Colors.black,
                                buttonText: item.buttonText,
                              ),
                              Container(
                                height: item == dropdownItems.last
                                    ? separatorHeight
                                    : (separatorHeight / 2),
                                width: double.infinity,
                                color: separatorColor ?? Colors.grey,
                              ),
                            ],
                          )
                      ],
                    ),
                  ],
                );
              }),
            ),
            Container(
              width: MediaQuery.of(bc).size.width * 0.9,
              margin: const EdgeInsets.symmetric(vertical: 10),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
              ),
              child: MaterialButton(
                onPressed: () {
                  Navigator.pop(buildContext);
                },
                color: cancelBackgroundColor ?? Colors.grey.shade200,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0)),
                minWidth: MediaQuery.of(bc).size.width - 40,
                child: Container(
                  padding: const EdgeInsets.symmetric(
                      horizontal: 20, vertical: 10),
                  child: Center(
                    child: Text(
                      cancelText,
                      style: cancelTextStyle ??
                          const TextStyle(
                            fontSize: 20,
                          ),
                    ),
                  ),
                ),
              ),
            )
          ],
        );
      });
  return selectedItem;
}