showMRECountryPicker function Phone

Future<MRECountry?> showMRECountryPicker(
  1. BuildContext context, {
  2. required List<MRECountry> countries,
  3. MRECountry? selected,
  4. List<MRECountry> favorites = const [],
  5. String? title,
  6. String? searchHint,
  7. String? emptyText,
  8. String nameBuilder(
    1. MRECountry country
    )?,
  9. bool showFlags = true,
})

Opens the country picker and returns the country the user chose, or null when it is dismissed.

The picker adapts to the window: a bottom sheet you can drag on narrow windows, a centered dialog with a maximum width on wide ones. The width is classified with MREFieldsTheme.windowSizeFor.

final country = showMRECountryPicker(
  context,
  countries: MRECountries.all,
  favorites: [MRECountries.byIsoCode('EG')!],
);

Implementation

Future<MRECountry?> showMRECountryPicker(
  BuildContext context, {
  required List<MRECountry> countries,
  MRECountry? selected,
  List<MRECountry> favorites = const [],
  String? title,
  String? searchHint,
  String? emptyText,
  String Function(MRECountry country)? nameBuilder,
  bool showFlags = true,
}) {
  final size = MREFieldsTheme.of(
    context,
  ).windowSizeFor(MediaQuery.sizeOf(context).width);

  MRECountryPickerBody body(
    BuildContext context, {
    ScrollController? controller,
  }) {
    return MRECountryPickerBody(
      countries: countries,
      selected: selected,
      favorites: favorites,
      title: title,
      searchHint: searchHint,
      emptyText: emptyText,
      nameBuilder: nameBuilder,
      showFlags: showFlags,
      scrollController: controller,
      autofocusSearch: size == MREWindowSize.expanded,
      onSelected: (country) => Navigator.of(context).pop(country),
    );
  }

  if (size == MREWindowSize.expanded) {
    return showDialog<MRECountry>(
      context: context,
      builder: (context) => Dialog(
        child: ConstrainedBox(
          constraints: BoxConstraints(
            maxWidth: _dialogMaxWidth,
            maxHeight: math.min(
              _dialogMaxHeight,
              MediaQuery.sizeOf(context).height * _dialogHeightShare,
            ),
          ),
          child: body(context),
        ),
      ),
    );
  }

  return showModalBottomSheet<MRECountry>(
    context: context,
    isScrollControlled: true,
    useSafeArea: true,
    showDragHandle: true,
    builder: (context) => Padding(
      padding: EdgeInsets.only(bottom: MediaQuery.viewInsetsOf(context).bottom),
      child: DraggableScrollableSheet(
        expand: false,
        initialChildSize: 0.9,
        minChildSize: 0.5,
        builder: (context, controller) => body(context, controller: controller),
      ),
    ),
  );
}