Flutter设置中文报错解决办法

TextField长按后报错 The getter 'pasteButtonLabel' was called on null.

因为设置中文后TextField长按调用的cupertino语言配置,而flutter的cupertino_*.arb(flutter/packages/flutter_localizations/lib/src/l10n)文件只对英文进行了支持所以导致的报错。

解决办法自定义一个LocalizationsDelegate类来对cupertino做中文支持,直接上源码,如下:

///设置语言中文
locale = const Locale('zh', 'CN'),
localizationsDelegates = const [
             GlobalMaterialLocalizations.delegate,
             GlobalWidgetsLocalizations.delegate,
             ///自定义代理,见下段代码
             CommonLocalizationsDelegate(),
           ],
supportedLocales = [
         const Locale('en', 'US'),
         const Locale('zh', 'CN'),
       ],
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';

///语言(主要解决cupertino控件不能显示中文的问题)
class CommonLocalizationsDelegate
    extends LocalizationsDelegate {
  const CommonLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) =>
      ['zh', 'CN'].contains(locale.languageCode);

  @override
  SynchronousFuture<_DefaultCupertinoLocalizations> load(Locale locale) {
    return SynchronousFuture<_DefaultCupertinoLocalizations>(
        _DefaultCupertinoLocalizations(locale.languageCode));
  }

  @override
  bool shouldReload(CommonLocalizationsDelegate old) => false;
}

class _DefaultCupertinoLocalizations extends CupertinoLocalizations {
  _DefaultCupertinoLocalizations(this._languageCode)
      : assert(_languageCode != null);
  final String _languageCode;

  static const List _shortWeekdays = [
    '周一',
    '周二',
    '周三',
    '周四',
    '周五',
    '周六',
    '周日',
  ];

  static const List _shortMonths = [
    '一月',
    '二月',
    '三月',
    '四月',
    '五月',
    '六月',
    '七月',
    '八月',
    '九月',
    '十月',
    '十一月',
    '十二月',
  ];

  static const List _months = [
    '一月',
    '二月',
    '三月',
    '四月',
    '五月',
    '六月',
    '七月',
    '八月',
    '九月',
    '十月',
    '十一月',
    '十二月',
  ];

  @override
  String get alertDialogLabel => '提醒';

  @override
  String get anteMeridiemAbbreviation => "上午";

  @override
  String get postMeridiemAbbreviation => "下午";

  @override
  String get copyButtonLabel => "复制";

  @override
  String get cutButtonLabel => "剪切";

  @override
  String get pasteButtonLabel => "粘贴";

  @override
  String get selectAllButtonLabel => "全选";

  @override
  DatePickerDateOrder get datePickerDateOrder => DatePickerDateOrder.ymd;

  @override
  DatePickerDateTimeOrder get datePickerDateTimeOrder =>
      DatePickerDateTimeOrder.date_time_dayPeriod;

  @override
  String datePickerDayOfMonth(int dayIndex) => dayIndex.toString();

  @override
  String datePickerHour(int hour) => hour.toString();

  @override
  String datePickerHourSemanticsLabel(int hour) => hour.toString();

  @override
  String datePickerMediumDate(DateTime date) {
    return '${_shortWeekdays[date.weekday - DateTime.monday]} '
        '${_shortMonths[date.month - DateTime.january]} '
        '${date.day.toString().padRight(2)}';
  }

  @override
  String datePickerMinute(int minute) => minute.toString().padLeft(2, '0');

  @override
  String datePickerMinuteSemanticsLabel(int minute) {
    if (minute == 1) return '1 分钟';
    return minute.toString() + ' 分钟';
  }

  @override
  String datePickerMonth(int monthIndex) => _months[monthIndex - 1];

  @override
  String datePickerYear(int yearIndex) => yearIndex.toString();

  @override
  String timerPickerHour(int hour) => hour.toString();

  @override
  String timerPickerHourLabel(int hour) => '时';

  @override
  String timerPickerMinute(int minute) => minute.toString();

  @override
  String timerPickerMinuteLabel(int minute) => '分';

  @override
  String timerPickerSecond(int second) => second.toString();

  @override
  String timerPickerSecondLabel(int second) => '秒';
}

愿一切顺利,大家多多留言点赞。

你可能感兴趣的:(Flutter设置中文报错解决办法)