国际化和TextFiled 的 pasteButtonLabel 问题

刚好把(五)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 = "自定义 剪切 中文";
}

正确效果

系统英文:
在这里插入图片描述

系统中文:
在这里插入图片描述

代码

代码

参考资料

https://www.jianshu.com/p/e8e0f7b7eb14

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值