flutter开发实战-长按TextField输入框cut、copy设置为中文复制、粘贴

flutter开发实战-长按TextField输入框cut、copy设置为中文复制、粘贴

在开发过程中,需要长按TextField输入框cut、copy设置为中文"复制、粘贴",这里记录一下设置的代码。

一、pubspec.yaml设置flutter_localizations

在pubspec.yaml中设置flutter_localizations

dart 复制代码
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

如果是Android studio,可以使用Flutter intl插件。如图所示

二、设置main.dart的入口locale

在MaterialApp设置locale、supportedLocales

dart 复制代码
import 'package:flutter_localizations/flutter_localizations.dart';
MaterialApp(
     home: _buildGlobalGesture(context),
     onGenerateRoute: DevRouterManager.generateRoute,
     initialRoute: RouterName.splash,
     locale: Locale('zh'),
     localizationsDelegates: [
       GlobalMaterialLocalizations.delegate,
       GlobalWidgetsLocalizations.delegate,
       GlobalCupertinoLocalizations.delegate,
     ],
     supportedLocales: [
       //此处设置
       const Locale('zh', 'CH'),
       const Locale('en', 'US'),
     ],
 )

  Widget _buildGlobalGesture(BuildContext context) {
    return GestureDetector(
      onTap: () {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus &&
            currentFocus.focusedChild != null) {
          FocusManager.instance.primaryFocus?.unfocus();
          // 也可以使用如下方式隐藏键盘:
          // SystemChannels.textInput.invokeMethod('TextInput.hide');
        }
      },
    );
  }

至此,可以全局设置语言环境为中文。

三、小结

flutter开发实战-长按TextField输入框cut、copy设置为中文复制、粘贴。

学习记录,每天不停进步。

相关推荐
DKPT16 小时前
JVM新生代和老生代比例如何设置?
java·开发语言·jvm·笔记·学习
知彼解己16 小时前
JVM 运行时数据区域
java·开发语言·jvm
坠金16 小时前
CentOS安装或升级protoc
linux·c语言·开发语言
江团1io016 小时前
一篇文章带你彻底搞懂 JVM 垃圾收集器
java·开发语言·jvm
yuehua_zhang16 小时前
uni app 的app端 写入运行日志到指定文件夹。
前端·javascript·uni-app
会飞的胖达喵16 小时前
Qt Model/View/Delegate 架构详解
开发语言·qt·架构
edjxj16 小时前
Qt添加图标资源
开发语言·数据库·qt
鲸落落丶17 小时前
finalize() 方法介绍
开发语言·python
q5673152317 小时前
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
开发语言·爬虫·python·自动化·scala
hqyjzsb17 小时前
2025职场进阶:B端产品经理必备的计算机专业技能精要
大数据·开发语言·人工智能·产品经理·编程语言·caie