flag method

Widget? flag({
  1. FlagNotFoundFallBack? fallBack = FlagNotFoundFallBack.emojiThenFull,
})

Search for a flag for the given locale

See FlagNotFoundFallBack for fallbacks.

Implementation

Widget? flag(
    {FlagNotFoundFallBack? fallBack = FlagNotFoundFallBack.emojiThenFull}) {
  final str = toString();

  // check full
  var flag = findFlagFor(language: str);
  if (flag != null) return flag;

  final localeList = str.split('_');
  // create fallback
  Widget? fb;
  switch (fallBack) {
    case null:
    // nothing
    case FlagNotFoundFallBack.full:
      fb = Text(str);
    case FlagNotFoundFallBack.emojiThenFull:
      final em = emoji;
      if (em != languageCode && em != countryCode) {
        fb = Text(em);
      } else {
        fb = Text(str);
      }
    case FlagNotFoundFallBack.countryCodeThenFull:
      if (localeList.length > 1) {
        fb = Text(localeList.last);
      } else {
        fb = Text(str);
      }
    case FlagNotFoundFallBack.emojiThenCountryCodeThenFull:
      final em = emoji;
      if (em != languageCode && em != countryCode) {
        fb = Text(em);
      } else {
        if (localeList.length > 1) {
          fb = Text(localeList.last);
        } else {
          fb = Text(str);
        }
      }
    case FlagNotFoundFallBack.countryCodeThenNull:
      if (localeList.length > 1) {
        fb = Text(localeList.last);
      }
  }

  if (str.length > 2) {
    fb = findFlagFor(language: localeList.first) ?? fb;
  }

  switch (localeList.length) {
    case 1:
    // already checked by findFlagFor(str)
    case 2:
      if (localeList.last.length != 4) {
        // second is not scriptCode
        return findFlagFor(country: localeList.last) ?? fb;
      } else {
        return fb;
      }
    case 3:
      return findFlagFor(country: localeList.last) ?? fb;
    default:
      return fb;
  }
}