showPicker method

Future<CountryCode?> showPicker({
  1. required BuildContext context,
  2. bool fullScreen = false,
  3. ShapeBorder shape = kShape,
  4. double pickerMinHeight = 150,
  5. double pickerMaxHeight = 770,
  6. String? initialSelectedLocale,
  7. bool scrollToDeviceLocale = false,
  8. Color barrierColor = kBarrierColor,
  9. Clip? clipBehavior = Clip.hardEdge,
  10. Color backgroundColor = kBackgroundColor,
})

Shows the CountryCodePickerModal modal.

If scrollToDeviceLocale was set to true, it will override the value from initialSelectedLocale parameter.

Returns the selected CountryCode.

Implementation

Future<CountryCode?> showPicker({
  required BuildContext context,
  bool fullScreen = false,
  ShapeBorder shape = kShape,
  double pickerMinHeight = 150,
  double pickerMaxHeight = 770,
  String? initialSelectedLocale,
  bool scrollToDeviceLocale = false,
  Color barrierColor = kBarrierColor,
  Clip? clipBehavior = Clip.hardEdge,
  Color backgroundColor = kBackgroundColor,
}) async {
  final fullScreenHeight = MediaQuery.of(context).size.height;
  final allowance = MediaQuery.of(context).padding.top -
      MediaQuery.of(context).padding.bottom;

  // Dynamic modal height computation.
  final maxHeight =
      fullScreen ? fullScreenHeight - allowance : pickerMaxHeight;

  final constraints = BoxConstraints(
    maxHeight: maxHeight,
    minHeight: pickerMinHeight,
  );

  // For automatic scrolling.
  final deviceLocale = ui.PlatformDispatcher.instance.locale.countryCode;

  String? focusedCountry;
  if (scrollToDeviceLocale) {
    if (_codeIsSupported(deviceLocale)) {
      focusedCountry = deviceLocale;
    }
  } else {
    if (_codeIsSupported(initialSelectedLocale)) {
      focusedCountry = initialSelectedLocale;
    }
  }

  final country = showModalBottomSheet<CountryCode?>(
    elevation: 0,
    shape: shape,
    context: context,
    useSafeArea: true,
    constraints: constraints,
    clipBehavior: clipBehavior,
    barrierColor: barrierColor,
    backgroundColor: backgroundColor,
    isScrollControlled: true,
    builder: (_) => CountryCodePickerModal(
      title: title,
      defaultAppbarBackgroundColor: defaultAppbarBackgroundColor,
      defaultAppbarForegroundColor: defaultAppbarForegroundColor,
      defaultAppbarCloseIconBackgroundColor:
          defaultAppbarCloseIconBackgroundColor,
      defaultAppbarText: defaultAppbarText,
      defaultAppbarCloseIcon: defaultAppbarCloseIcon,
      localize: localize,
      favorites: favorites,
      showDialCode: showDialCode,
      favoritesIcon: favoritesIcon,
      horizontalTitleGap: horizontalTitleGap,
      showSearchBar: showSearchBar,
      filteredCountries: filteredCountries,
      searchBarDecoration: searchBarDecoration,
      focusedCountry: focusedCountry,
      countryTextStyle: countryTextStyle,
      dialCodeTextStyle: dialCodeTextStyle,
      searchBarTextStyle: searchBarTextStyle,
    ),
  );

  return country;
}