刚好把(五)Flutter Redux 中的国际化 重新理一遍,顺便把之前遇到的一个TextFiled长按问题给处理下。该问题是在国际化的前提下,长按、双击文本框的时候弹出 selectAllButtonLabel、pasteButtonLabel、copyButtonLabel、cutButtonLabel 这几个方法没实现的错误。这边的代码主要是在(五)Flutter Redux 中的国际化 这里面的无Redux,App语言随系统语言变化的代码上进行修改。
错误效果
flutter: The getter 'pasteButtonLabel' was called on null.
flutter: Another exception was thrown: NoSuchMethodError: The getter 'pasteButtonLabel' was called on null.
解决方法
我们在国际化中讲了国际化主要是实现2个类,Delegate类和Localization类,这次的修改主要也是在这2个类中修改。
修改一:localization_delegate.dart
之前的FZLocalizationDelegate类是继承LocalizationsDelegate,并提供一个MoreLocalization范型,这次将范型改成CupertinoLocalizations,对应的实现方法load上的范型也要调整。
import 'package:flutter/cupertino.dart';
class FZLocalizationDelegate extends LocalizationsDelegate<CupertinoLocalizations> {
FZLocalizationDelegate();
///是否支持某个Local
///支持中文和英语
@override
bool isSupported(Locale locale) {
return ['zh', 'en'].contains(locale.languageCode);
}
///shouldReload的返回值决定当Localizations Widget重新build时,是否调用load方法重新加载Locale资源
@override
bool shouldReload(LocalizationsDelegate<CupertinoLocalizations> old) {
return false;
}
///根据locale,创建一个对象用于提供当前locale下的文本显示
///Flutter会调用此类加载相应的Locale资源类
@override
Future<CupertinoLocalizations> load(Locale locale) {
return SynchronousFuture<CupertinoLocalizations>(
MoreLocalization(locale)
);
}
static FZLocalizationDelegate delegate = FZLocalizationDelegate();
}
修改二:more_localization.dart
MoreLocalization实现CupertinoLocalizations抽象类,实现CupertinoLocalizations的10几个方法,这边主要拿selectAllButtonLabel、pasteButtonLabel、copyButtonLabel、cutButtonLabel出来说。
这里面还有个重要的修改点,实现CupertinoLocalizations抽象类后,之前的
of
方法里的 Localizations.of(context, MoreLocalization)
会报错,取不到对象,得调整为 CupertinoLocalizations 的方法 CupertinoLocalizations.of(context)
import 'package:flutter/cupertino.dart';
class MoreLocalization implements CupertinoLocalizations{
final Locale locale;
MoreLocalization(this.locale);
/// 基于Map,根据当前语言的 languageCode: en或zh来获取对应的文案
static Map<String, BaseLanguage> _localValue = {
'en' : EnLanguage(),
'zh' : ChLanguage()
};
/// 返回当前的内容维护类
BaseLanguage get currentLocalized {
return _localValue[locale.languageCode];
}
///通过 Localizations.of(context,type) 加载当前的 FZLocalizations
static MoreLocalization of(BuildContext context) {
return CupertinoLocalizations.of(context);
/// 实现CupertinoLocalizations抽象类后,取不到对象,得换成CupertinoLocalizations.of(context);
// return Localizations.of(context, MoreLocalization);
}
@override
String get selectAllButtonLabel {
return currentLocalized.selectAllButtonLabel;
}
@override
String get pasteButtonLabel {
return currentLocalized.pasteButtonLabel;
}
@override
String get copyButtonLabel {
return currentLocalized.copyButtonLabel;
}
@override
String get cutButtonLabel {
return currentLocalized.cutButtonLabel;
}
}
/// 这个抽象类和它的实现类可以拉出去新建类
/// 中文和英语 语言内容维护
abstract class BaseLanguage {
String name;
String selectAllButtonLabel;
String pasteButtonLabel;
String copyButtonLabel;
String cutButtonLabel;
}
class EnLanguage implements BaseLanguage {
@override
String name = "This is English";
@override
String selectAllButtonLabel = "自定义 全选 英语";
@override
String pasteButtonLabel = "自定义 粘贴 英语";
@override
String copyButtonLabel = "自定义 复制 英语";
@override
String cutButtonLabel = "自定义 剪切 英语";
}
class ChLanguage implements BaseLanguage {
@override
String name = "这是中文";
@override
String selectAllButtonLabel = "自定义 全选 中文";
@override
String pasteButtonLabel = "自定义 粘贴 中文";
@override
String copyButtonLabel = "自定义 复制 中文";
@override
String cutButtonLabel = "自定义 剪切 中文";
}
正确效果
系统英文:
系统中文: