前提
在做flutter ios 国际化的时候遇到长按文本框崩溃的问题,然后google到一堆写法是重写cupertinoLocalization的奇怪做法,然后还千篇一律都是这么改的,其实不用那么麻烦,一个代码就可以解决的问题
Flutter 中文网的例子
因为FLutter默认值支持美国英语的本地化,所以需要用到flutter_localizations的库,目前,软件包支持15中语言。
引入:flutter_localizations
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
使用:
import 'package:flutter_localizations/flutter_localizations.dart';
new MaterialApp(
localizationsDelegates: [
// ... app-specific localization delegate[s] here
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'), // English
const Locale('he', 'IL'), // Hebrew
// ... other locales the app supports
],
// ...
)
问题1:
按照Flutter中文网提供的这个例子来使用,在android中已经可以进行语言切换,实现对应的中英文切换了,但是在iphone中通过长按输入框,就会出错(get方法为空),提示为:
The getter 'pasteButtonLabel' was called on null.
Receiver: null
Tried calling: pasteButtonLabel
方案1:
这个错误,在天星银行里面也有,修复之后不会出现中文文案的提示。具体的修复代码如下:
class CupertinoLocalizationsDelegate extends LocalizationsDelegate {
const CupertinoLocalizationsDelegate();
@override
bool isSupported(Locale locale) => true;
@override
Future load(Locale locale) => DefaultCupertinoLocalizations.load(locale);
@override
bool shouldReload(CupertinoLocalizationsDelegate old) => false;
@override
String toString() => 'DefaultCupertinoLocalizations.delegate(zh_CH)';
}
--------------------------------------
localizationsDelegates: [
// ... app-specific localization delegate[s] here
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
CupertinoLocalizationsDelegate(),
],
添加如上代码之后,长按ios 会出现英文的cut、paste等文案,但是不会随着语言的切换本地化语言
原因是重写了LocalizationsDelegate,在isSupported返回true,不管当前是什么语言,我统统给你返回英文的默认文案(DefaultCupertinoLocalizations),改法显然是错误的。
方案2:
@override
Future load(L