1.插件
flutter_localizations:
sdk: flutter
3.main.dart
import 'package:flutter_localizations/flutter_localizations.dart';
import 'config/CupertinoLocalizationsDelegate.dart'; // 引入ios需要的创建的文件
MaterialApp(
localizationsDelegates: <LocalizationsDelegate<dynamic>>[
ChineseCupertinoLocalizations.delegate, // 中文
DefaultCupertinoLocalizations.delegate, // 英文
// 下面两个是Material widgets的delegate, 包含中文
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [ // 识别本地环境的语言
const Locale('en', 'US'), // English
const Locale('zh', 'Hans'), // China
const Locale('zh', ''), // China
// ... other locales the app supports
],
);
但是
Flutter Textfield长按报错修复:NosuchMethodError: The getter 'pasterButtonLabel' was called on null.
1.创建文件CupertinoLocalizationsDelegate.dart
import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; class ChineseCupertinoLocalizations implements CupertinoLocalizations { final materialDelegate = GlobalMaterialLocalizations.delegate; final widgetsDelegate = GlobalWidgetsLocalizations.delegate; final local = const Locale('zh'); MaterialLocalizations ml; Future init() async { ml = await materialDelegate.load(local); print(ml.pasteButtonLabel); } @override String get alertDialogLabel => ml.alertDialogLabel; @override String get anteMeridiemAbbreviation => ml.anteMeridiemAbbreviation; @override String get copyButtonLabel => ml.copyButtonLabel; @override String get cutButtonLabel => ml.cutButtonLabel; @override DatePickerDateOrder get datePickerDateOrder => DatePickerDateOrder.mdy; @override DatePickerDateTimeOrder get datePickerDateTimeOrder => DatePickerDateTimeOrder.date_time_dayPeriod; @override String datePickerDayOfMonth(int dayIndex) { return dayIndex.toString(); } @override String datePickerHour(int hour) { return hour.toString().padLeft(2, "0"); } @override String datePickerHourSemanticsLabel(int hour) { return "$hour" + "时"; } @override String datePickerMediumDate(DateTime date) { return ml.formatMediumDate(date); } @override String datePickerMinute(int minute) { return minute.toString().padLeft(2, '0'); } @override String datePickerMinuteSemanticsLabel(int minute) { return "$minute" + "分"; } @override String datePickerMonth(int monthIndex) { return "$monthIndex"; } @override String datePickerYear(int yearIndex) { return yearIndex.toString(); } @override String get pasteButtonLabel => ml.pasteButtonLabel; @override String get postMeridiemAbbreviation => ml.postMeridiemAbbreviation; @override String get selectAllButtonLabel => ml.selectAllButtonLabel; @override String timerPickerHour(int hour) { return hour.toString().padLeft(2, "0"); } @override String timerPickerHourLabel(int hour) { return "$hour".toString().padLeft(2, "0") + "时"; } @override String timerPickerMinute(int minute) { return minute.toString().padLeft(2, "0"); } @override String timerPickerMinuteLabel(int minute) { return minute.toString().padLeft(2, "0") + "分"; } @override String timerPickerSecond(int second) { return second.toString().padLeft(2, "0"); } @override String timerPickerSecondLabel(int second) { return second.toString().padLeft(2, "0") + "秒"; } static const LocalizationsDelegate<CupertinoLocalizations> delegate = _ChineseDelegate(); static Future<CupertinoLocalizations> load(Locale locale) async { var localizaltions = ChineseCupertinoLocalizations(); await localizaltions.init(); return SynchronousFuture<CupertinoLocalizations>(localizaltions); } @override // TODO: implement modalBarrierDismissLabel String get modalBarrierDismissLabel => throw UnimplementedError(); @override String tabSemanticsLabel({int tabIndex, int tabCount}) { // TODO: implement tabSemanticsLabel throw UnimplementedError(); } @override // TODO: implement todayLabel String get todayLabel => throw UnimplementedError(); } class _ChineseDelegate extends LocalizationsDelegate<CupertinoLocalizations> { const _ChineseDelegate(); @override bool isSupported(Locale locale) { return locale.languageCode == 'zh'; } @override Future<CupertinoLocalizations> load(Locale locale) { return ChineseCupertinoLocalizations.load(locale); } @override bool shouldReload(LocalizationsDelegate<CupertinoLocalizations> old) { return false; } }
但是textinput的光标需要做以下修改,需要将onChange改成onSubmit就可以了
TextField( // textInputAction: TextInputAction.go, // textAlign: TextAlign.center, controller: new TextEditingController(text: kvs[index]['title']), onSubmitted: (val) { // 原先是onChange setState((){ kvs[index]['title'] = val; }); }, // style: TextStyle(textBaseline: TextBaseline.alphabetic), decoration: InputDecoration( hintText: "参数名", border: InputBorder.none ), ),