日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

flutter 全选_Flutter ios 国际化(复制粘贴 中英文切换等问题)

發布時間:2024/1/1 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 flutter 全选_Flutter ios 国际化(复制粘贴 中英文切换等问题) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前提

在做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(Locale locale) {

if (locale.languageCode == 'zh') {

print('locale.languageCode');

return ChineseCupertinoLocalizations.load(locale);

}

return DefaultCupertinoLocalizations.load(locale);

}

ChineseCupertinoLocalizations是 新寫的類 繼承 CupertinoLocalizations 實現所有的method

class ChineseCupertinoLocalizations implements CupertinoLocalizations {

.....

@override

String get cutButtonLabel => '剪切';

@override

String get copyButtonLabel => '復制';

@override

String get pasteButtonLabel => '粘貼';

@override

String get selectAllButtonLabel => '全選';

.....

}

經過上述操作,問題是解決了,但是感覺不太對:

1、flutter_localizations 本身支持十幾種語言,android 切換沒有問題,ios 卻有問題。

2、如果國際化更多的語言,難道要每個語言實現一遍CupertinoLocalizations,思路明顯有問題。

分析

MaterialApp 提供的參數localizationsDelegates列表中的元素是生本地集合的工廠,其中GlobalMaterialLocalizations.delegate為Material Components庫提供了本地化的字符串和其他值。GlobalWidgetsLocalizations.delegate定義widget默認的文本方向,從左到右或從右到左(如沙特語言)。

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 提供了 三個關鍵函數:分別是isSupported 、load 、 shouldReload

flutter為了減小包的大小,僅僅提供了英文的MaterialLocalizations和CupertinoLocalizations的實現,分別是DefaultMaterialLocalizations 和 DefaultCupertinoLocalizations所以在語言是英文的情況下,即使不引入flutter_localizations庫 一些系統的文本是沒有問題的,但是本地語言是英文之外的語言,就需要這個庫了。

按照官方的寫法引入:

localizationsDelegates: [

// 本地化代理

GlobalMaterialLocalizations.delegate,

GlobalWidgetsLocalizations.delegate,

],

Material Components庫是支持iOS、android、web 三段通用的風格,所以正常提供GlobalMaterialLocalizations.delegate提供的本地化字符串和其他值,應該是三段通用的。但是從現象上看是有問題的(下面解釋),查看GlobalMaterialLocalizations.delegate的實現中有一個函數:

static const List> delegates = >[

GlobalCupertinoLocalizations.delegate,

GlobalMaterialLocalizations.delegate,

GlobalWidgetsLocalizations.delegate,

];

delegates直接提供了GlobalCupertinoLocalizations和GlobalMaterialLocalizations ,說明flutter_localizations也是支持iOS的國際化設置的。

修改MaterialApp中的參數設置:

localizationsDelegates: [

// 本地化代理

GlobalMaterialLocalizations.delegate,

GlobalWidgetsLocalizations.delegate,

GlobalCupertinoLocalizations.delegate,

],

supportedLocales: [

const Locale('en', ''), // 美國英語

const Locale('zh', 'HK'), // 中文

const Locale('ja', ''), // 日語

],

locale: const Locale('en', ''),

在加入GlobalCupertinoLocalizations之后就可以的支持iOS的國際化操作了。

繼續查看GlobalMaterialLocalizations中,查看繼承自LocalizationsDelegate的_MaterialLocalizationsDelegate

在isSupported中看到支持的語言:

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,

));

根據getMaterialTranslation 返回對應語言場景下的繼承自GlobalMaterialLocalizations的具體實例。

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);

......//很多語言

App通過啟動,或者切換語言的時候,替換MaterialApp中的'locale'對象來切換語言,同時,切換的語言對象要在supportedLocales中,否則默認返回supportedLocales中的第一個語言,如果一個都沒有,默認返回英文

supportedLocales: [

const Locale('ja', ''), // 日語

const Locale('en', ''), // 美國英語

const Locale('zh', 'HK'), // 中文

//其它Locales

],

locale: const Locale('zh', ''),

問題:

既然GlobalMaterialLocalizations是iOS和android 通用的風格,在Flutter中文網 介紹的接入國際化的時候也只提到了GlobalMaterialLocalizations感覺這明顯是有問題的。

在DefalutCupertinoLocalization中我們檢索cutButtonLabel屬性,我們看一下那些地方在使用:

內容:

@override

String get cutButtonLabel => 'Cut';

@override

String get copyButtonLabel => 'Copy';

@override

String get pasteButtonLabel => 'Paste';

結果發現有兩處地方在使用:

flutter > lib > src > material > text_selection.dart

flutter > lib > scr > cupertino > text_selection.dart

在這兩個textSelection的build函數中,獲取的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之后發現,在iOS設備上加載的cupertino的text_selected,android上是material的text_selected ,

由此真相大白。

正常實現系統文本的國際化只需要在MaterialApp的初始化中加入:

localizationsDelegates: [

// 本地化代理

GlobalMaterialLocalizations.delegate,

GlobalWidgetsLocalizations.delegate,

GlobalCupertinoLocalizations.delegate,

],

總結

以上是生活随笔為你收集整理的flutter 全选_Flutter ios 国际化(复制粘贴 中英文切换等问题)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。