注册

其他都是错的,只有这一篇正确解决:Flutter Textfield长按报错修复:NosuchMethodError: The getter ‘pasterBu

正确解决:Flutter Textfield长按报错修复:NosuchMethodError: The getter 'pasterButtonLabel' was ca ?????????


为什么叫正确解决??
关于这个问题,我在百度上看过很多人的答案,基本无一例外都是,说:“Cupertino缺少了对应的非英文版本的支持”。
大家真的看过源码吗?真的是缺少Cupertino么?我是真不相信的,flutter出了这么多年,连个中文都不支持?然后我就查阅了源码:
我发现了这个类 GlobalCupertinoLocalizations
有木有很眼熟,他和
GlobalMaterialLocalizations & GlobalWidgetsLocalizations 没啥区别


class _GlobalCupertinoLocalizationsDelegate extends LocalizationsDelegate {
const _GlobalCupertinoLocalizationsDelegate();

@override
bool isSupported(Locale locale) => kCupertinoSupportedLanguages.contains(locale.languageCode);

static final Map> _loadedTranslations = >{};

@override
Future load(Locale locale) {
assert(isSupported(locale));
return _loadedTranslations.putIfAbsent(locale, () {
util.loadDateIntlDataIfNotLoaded();

final String localeName = intl.Intl.canonicalizedLocale(locale.toString());
assert(
locale.toString() == localeName,
'Flutter does not support the non-standard locale form $locale (which '
'might be $localeName',
);

late intl.DateFormat fullYearFormat;
late intl.DateFormat dayFormat;
late intl.DateFormat mediumDateFormat;
// We don't want any additional decoration here. The am/pm is handled in
// the date picker. We just want an hour number localized.
late intl.DateFormat singleDigitHourFormat;
late intl.DateFormat singleDigitMinuteFormat;
late intl.DateFormat doubleDigitMinuteFormat;
late intl.DateFormat singleDigitSecondFormat;
late intl.NumberFormat decimalFormat;

void loadFormats(String? locale) {
fullYearFormat = intl.DateFormat.y(locale);
dayFormat = intl.DateFormat.d(locale);
mediumDateFormat = intl.DateFormat.MMMEd(locale);
// TODO(xster): fix when is resolved.
singleDigitHourFormat = intl.DateFormat('HH', locale);
singleDigitMinuteFormat = intl.DateFormat.m(locale);
doubleDigitMinuteFormat = intl.DateFormat('mm', locale);
singleDigitSecondFormat = intl.DateFormat.s(locale);
decimalFormat = intl.NumberFormat.decimalPattern(locale);
}

if (intl.DateFormat.localeExists(localeName)) {
loadFormats(localeName);
} else if (intl.DateFormat.localeExists(locale.languageCode)) {
loadFormats(locale.languageCode);
} else {
loadFormats(null);
}

return SynchronousFuture(getCupertinoTranslation(
locale,
fullYearFormat,
dayFormat,
mediumDateFormat,
singleDigitHourFormat,
singleDigitMinuteFormat,
doubleDigitMinuteFormat,
singleDigitSecondFormat,
decimalFormat,
)!);
});
}

@override
bool shouldReload(_GlobalCupertinoLocalizationsDelegate old) => false;

@override
String toString() => 'GlobalCupertinoLocalizations.delegate(${kCupertinoSupportedLanguages.length} locales)';
}
,>,>

源码中加载语言也没说不支持中文啊!!
还有网上很多配置本地化时候都是这么写的:


          GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,

仔细看了源码,我想说:
这么写不香么??
GlobalMaterialLocalizations.delegates



/// A value for [MaterialApp.localizationsDelegates] that's typically used by
/// internationalized apps.
///
/// ## Sample code
///
/// To include the localizations provided by this class and by
/// [GlobalWidgetsLocalizations] in a [MaterialApp],
/// use [GlobalMaterialLocalizations.delegates] as the value of
/// [MaterialApp.localizationsDelegates], and specify the locales your
/// app supports with [MaterialApp.supportedLocales]:
///
/// ```dart
/// new MaterialApp(
/// localizationsDelegates: GlobalMaterialLocalizations.delegates,
/// supportedLocales: [
/// const Locale('en', 'US'), // English
/// const Locale('he', 'IL'), // Hebrew
/// ],
/// // ...
/// )
/// ```
static const List> delegates = >[
GlobalCupertinoLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
];
}

仅此一篇文章,我希望大家认真阅读源码,提升水平

0 个评论

要回复文章请先登录注册