flutter 全选_Flutter ios 国际化(复制粘贴 中英文切换等问题)
前提
在做flutter ios 國(guó)際化的時(shí)候遇到長(zhǎng)按文本框崩潰的問題,然后google到一堆寫法是重寫cupertinoLocalization的奇怪做法,然后還千篇一律都是這么改的,其實(shí)不用那么麻煩,一個(gè)代碼就可以解決的問題
Flutter 中文網(wǎng)的例子
因?yàn)镕Lutter默認(rèn)值支持美國(guó)英語(yǔ)的本地化,所以需要用到flutter_localizations的庫(kù),目前,軟件包支持15中語(yǔ)言。
引入: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中文網(wǎng)提供的這個(gè)例子來使用,在android中已經(jīng)可以進(jìn)行語(yǔ)言切換,實(shí)現(xiàn)對(duì)應(yīng)的中英文切換了,但是在iphone中通過長(zhǎng)按輸入框,就會(huì)出錯(cuò)(get方法為空),提示為:
The getter 'pasteButtonLabel' was called on null.
Receiver: null
Tried calling: pasteButtonLabel
方案1:
這個(gè)錯(cuò)誤,在天星銀行里面也有,修復(fù)之后不會(huì)出現(xiàn)中文文案的提示。具體的修復(fù)代碼如下:
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(),
],
添加如上代碼之后,長(zhǎng)按ios 會(huì)出現(xiàn)英文的cut、paste等文案,但是不會(huì)隨著語(yǔ)言的切換本地化語(yǔ)言
原因是重寫了LocalizationsDelegate,在isSupported返回true,不管當(dāng)前是什么語(yǔ)言,我統(tǒng)統(tǒng)給你返回英文的默認(rèn)文案(DefaultCupertinoLocalizations),改法顯然是錯(cuò)誤的。
方案2:
@override
Future load(Locale locale) {
if (locale.languageCode == 'zh') {
print('locale.languageCode');
return ChineseCupertinoLocalizations.load(locale);
}
return DefaultCupertinoLocalizations.load(locale);
}
ChineseCupertinoLocalizations是 新寫的類 繼承 CupertinoLocalizations 實(shí)現(xiàn)所有的method
class ChineseCupertinoLocalizations implements CupertinoLocalizations {
.....
@override
String get cutButtonLabel => '剪切';
@override
String get copyButtonLabel => '復(fù)制';
@override
String get pasteButtonLabel => '粘貼';
@override
String get selectAllButtonLabel => '全選';
.....
}
經(jīng)過上述操作,問題是解決了,但是感覺不太對(duì):
1、flutter_localizations 本身支持十幾種語(yǔ)言,android 切換沒有問題,ios 卻有問題。
2、如果國(guó)際化更多的語(yǔ)言,難道要每個(gè)語(yǔ)言實(shí)現(xiàn)一遍CupertinoLocalizations,思路明顯有問題。
分析
MaterialApp 提供的參數(shù)localizationsDelegates列表中的元素是生本地集合的工廠,其中GlobalMaterialLocalizations.delegate為Material Components庫(kù)提供了本地化的字符串和其他值。GlobalWidgetsLocalizations.delegate定義widget默認(rèn)的文本方向,從左到右或從右到左(如沙特語(yǔ)言)。
LocalizationsDelegate是何物?我們看一下源碼
abstract class LocalizationsDelegate {
bool isSupported(Locale locale);
Future load(Locale locale);
bool shouldReload(covariant LocalizationsDelegate old);
Type get type => T;
@override
String toString() => '${objectRuntimeType(this, 'LocalizationsDelegate')}[$type]';
}
LocalizationsDelegate 提供了 三個(gè)關(guān)鍵函數(shù):分別是isSupported 、load 、 shouldReload
flutter為了減小包的大小,僅僅提供了英文的MaterialLocalizations和CupertinoLocalizations的實(shí)現(xiàn),分別是DefaultMaterialLocalizations 和 DefaultCupertinoLocalizations所以在語(yǔ)言是英文的情況下,即使不引入flutter_localizations庫(kù) 一些系統(tǒng)的文本是沒有問題的,但是本地語(yǔ)言是英文之外的語(yǔ)言,就需要這個(gè)庫(kù)了。
按照官方的寫法引入:
localizationsDelegates: [
// 本地化代理
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
Material Components庫(kù)是支持iOS、android、web 三段通用的風(fēng)格,所以正常提供GlobalMaterialLocalizations.delegate提供的本地化字符串和其他值,應(yīng)該是三段通用的。但是從現(xiàn)象上看是有問題的(下面解釋),查看GlobalMaterialLocalizations.delegate的實(shí)現(xiàn)中有一個(gè)函數(shù):
static const List> delegates = >[
GlobalCupertinoLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
];
delegates直接提供了GlobalCupertinoLocalizations和GlobalMaterialLocalizations ,說明flutter_localizations也是支持iOS的國(guó)際化設(shè)置的。
修改MaterialApp中的參數(shù)設(shè)置:
localizationsDelegates: [
// 本地化代理
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''), // 美國(guó)英語(yǔ)
const Locale('zh', 'HK'), // 中文
const Locale('ja', ''), // 日語(yǔ)
],
locale: const Locale('en', ''),
在加入GlobalCupertinoLocalizations之后就可以的支持iOS的國(guó)際化操作了。
繼續(xù)查看GlobalMaterialLocalizations中,查看繼承自LocalizationsDelegate的_MaterialLocalizationsDelegate
在isSupported中看到支持的語(yǔ)言:
final Set kMaterialSupportedLanguages = HashSet.from(const [
'af', // Afrikaans
'am', // Amharic
'ar', // Arabic
'as', // Assamese
'az', // Azerbaijani
'be', // Belarusian
'bg', // Bulgarian
'bn', // Bengali Bangla
'bs', // Bosnian
......//后面還有好多
]);
在load的的返回中:
return SynchronousFuture(getMaterialTranslation(
locale,
fullYearFormat,
compactDateFormat,
shortDateFormat,
mediumDateFormat,
longDateFormat,
yearMonthFormat,
shortMonthDayFormat,
decimalFormat,
twoDigitZeroPaddedFormat,
));
根據(jù)getMaterialTranslation 返回對(duì)應(yīng)語(yǔ)言場(chǎng)景下的繼承自GlobalMaterialLocalizations的具體實(shí)例。
GlobalMaterialLocalizations getMaterialTranslation(
Locale locale,
intl.DateFormat fullYearFormat,
intl.DateFormat compactDateFormat,
intl.DateFormat shortDateFormat,
intl.DateFormat mediumDateFormat,
intl.DateFormat longDateFormat,
intl.DateFormat yearMonthFormat,
intl.DateFormat shortMonthDayFormat,
intl.NumberFormat decimalFormat,
intl.NumberFormat twoDigitZeroPaddedFormat,
) {
switch (locale.languageCode) {
case 'af':
return MaterialLocalizationAf(fullYearFormat: fullYearFormat, compactDateFormat: compactDateFormat, shortDateFormat: shortDateFormat, mediumDateFormat: mediumDateFormat, longDateFormat: longDateFormat, yearMonthFormat: yearMonthFormat, shortMonthDayFormat: shortMonthDayFormat, decimalFormat: decimalFormat, twoDigitZeroPaddedFormat: twoDigitZeroPaddedFormat);
case 'am':
return MaterialLocalizationAm(fullYearFormat: fullYearFormat, compactDateFormat: compactDateFormat, shortDateFormat: shortDateFormat, mediumDateFormat: mediumDateFormat, longDateFormat: longDateFormat, yearMonthFormat: yearMonthFormat, shortMonthDayFormat: shortMonthDayFormat, decimalFormat: decimalFormat, twoDigitZeroPaddedFormat: twoDigitZeroPaddedFormat);
case 'ar':
return MaterialLocalizationAr(fullYearFormat: fullYearFormat, compactDateFormat: compactDateFormat, shortDateFormat: shortDateFormat, mediumDateFormat: mediumDateFormat, longDateFormat: longDateFormat, yearMonthFormat: yearMonthFormat, shortMonthDayFormat: shortMonthDayFormat, decimalFormat: decimalFormat, twoDigitZeroPaddedFormat: twoDigitZeroPaddedFormat);
......//很多語(yǔ)言
App通過啟動(dòng),或者切換語(yǔ)言的時(shí)候,替換MaterialApp中的'locale'對(duì)象來切換語(yǔ)言,同時(shí),切換的語(yǔ)言對(duì)象要在supportedLocales中,否則默認(rèn)返回supportedLocales中的第一個(gè)語(yǔ)言,如果一個(gè)都沒有,默認(rèn)返回英文
supportedLocales: [
const Locale('ja', ''), // 日語(yǔ)
const Locale('en', ''), // 美國(guó)英語(yǔ)
const Locale('zh', 'HK'), // 中文
//其它Locales
],
locale: const Locale('zh', ''),
問題:
既然GlobalMaterialLocalizations是iOS和android 通用的風(fēng)格,在Flutter中文網(wǎng) 介紹的接入國(guó)際化的時(shí)候也只提到了GlobalMaterialLocalizations感覺這明顯是有問題的。
在DefalutCupertinoLocalization中我們檢索cutButtonLabel屬性,我們看一下那些地方在使用:
內(nèi)容:
@override
String get cutButtonLabel => 'Cut';
@override
String get copyButtonLabel => 'Copy';
@override
String get pasteButtonLabel => 'Paste';
結(jié)果發(fā)現(xiàn)有兩處地方在使用:
flutter > lib > src > material > text_selection.dart
flutter > lib > scr > cupertino > text_selection.dart
在這兩個(gè)textSelection的build函數(shù)中,獲取的Localizations分別是material 和cupertino的:
@override
Widget build(BuildContext context) {
// Don't render the menu until the state of the clipboard is known.
if (widget.handlePaste != null
&& _clipboardStatus.value == ClipboardStatus.unknown) {
return const SizedBox(width: 0.0, height: 0.0);
}
print('text_selected---------------------------CupertinoLocalizations');
final List items = [];
final CupertinoLocalizations localizations = CupertinoLocalizations.of(context);
final EdgeInsets arrowPadding = widget.isArrowPointingDown
? EdgeInsets.only(bottom: _kToolbarArrowSize.height)
: EdgeInsets.only(top: _kToolbarArrowSize.height);
final Widget onePhysicalPixelVerticalDivider =
SizedBox(width: 1.0 / MediaQuery.of(context).devicePixelRatio);
@override
Widget build(BuildContext context) {
// Don't render the menu until the state of the clipboard is known.
if (widget.handlePaste != null
&& _clipboardStatus.value == ClipboardStatus.unknown) {
return const SizedBox(width: 0.0, height: 0.0);
}
print('text_selected---------------------------MaterialLocalizations');
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
final List<_itemdata> itemDatas = <_itemdata>[
if (widget.handleCut != null)
_ItemData(widget.handleCut, localizations.cutButtonLabel),
if (widget.handleCopy != null)
_ItemData(widget.handleCopy, localizations.copyButtonLabel),
if (widget.handlePaste != null
&& _clipboardStatus.value == ClipboardStatus.pasteable)
_ItemData(widget.handlePaste, localizations.pasteButtonLabel),
if (widget.handleSelectAll != null)
_ItemData(widget.handleSelectAll, localizations.selectAllButtonLabel),
];
加上print之后發(fā)現(xiàn),在iOS設(shè)備上加載的cupertino的text_selected,android上是material的text_selected ,
由此真相大白。
正常實(shí)現(xiàn)系統(tǒng)文本的國(guó)際化只需要在MaterialApp的初始化中加入:
localizationsDelegates: [
// 本地化代理
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
總結(jié)
以上是生活随笔為你收集整理的flutter 全选_Flutter ios 国际化(复制粘贴 中英文切换等问题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL刷题宝典-MySQL速通力扣困难题
- 下一篇: Chemdraw 打开分子后在手性中心边