showBottomSheetProfilesCheckBox function Null safety

Future<List<ModernFormBottomSheetProfileModel>?> showBottomSheetProfilesCheckBox(
  1. dynamic context,
  2. {List<ModernFormBottomSheetProfileModel>? list,
  3. List<ModernFormBottomSheetProfileModel>? initialValue,
  4. String? title,
  5. TextStyle? titleStyle,
  6. bool? forceMenu = false}
)

Implementation

Future<List<ModernFormBottomSheetProfileModel>?>
    showBottomSheetProfilesCheckBox(
  context, {
  List<ModernFormBottomSheetProfileModel>? list,
  List<ModernFormBottomSheetProfileModel>? initialValue,
  String? title,
  TextStyle? titleStyle,
  bool? forceMenu = false,
}) async {
  List<ModernFormBottomSheetProfileModel> selected = [];
  if (initialValue != null) {
    selected = List.from(initialValue);
  }

  if (forceMenu == null) {
    forceMenu = false;
  }

  if (MediaQuery.of(context).size.width > ModernFormUtils.webBreakdown ||
      forceMenu) {
    List<menu.PopupMenuEntry<dynamic>> items = [];

    list?.forEach((element) {
      items.add(menu.PopupMenuItem(
        value: element.returnValue,
        onTapEnabled: false,
        child: _ProfileItenCheckBox(
          image: element.imageUrl,
          imageWidth: 50,
          title: element.name,
          isSelected: selected.contains(element),
          onChanged: (value) {
            if (selected.contains(element)) {
              selected.remove(element);
            } else {
              selected.add(element);
            }
          },
        ),
      ));
    });
    if (items.isNotEmpty) {
      await menu.showMenu(
        context: context,
        position: menu.modernFormPopupMenuPosition(
            context, menu.ModernFormPopupMenuLocation.BottomLeft),
        items: items,
        maxHeight: MediaQuery.of(context).size.height * .3,
      );
    }
  } else {
    List<Widget> widgets = <Widget>[];

    list?.forEach(
      (element) => widgets.add(
        _ProfileItenCheckBox(
          image: element.imageUrl,
          title: element.name,
          isSelected: selected.contains(element),
          onChanged: (value) {
            if (selected.contains(element)) {
              selected.remove(element);
            } else {
              selected.add(element);
            }
          },
        ),
      ),
    );
    await showModalBottomSheet(
      isScrollControlled: true,
      context: context,
      builder: (context) {
        return ConstrainedBox(
          constraints: BoxConstraints(
              maxHeight: MediaQuery.of(context).size.height * .6),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              ModernFormBottonSheetLabel(
                label: title,
                labelStyle: titleStyle,
              ),
              Expanded(
                child: ListView(
                  padding:
                      EdgeInsets.symmetric(vertical: ScreenUtil().setSp(15)),
                  shrinkWrap: true,
                  children: widgets,
                ),
              ),
            ],
          ),
        );
      },
    );
  }
  return selected;
}