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设置为中文复制、粘贴。

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

相关推荐
同学小张2 小时前
【端侧AI 与 C++】1. llama.cpp源码编译与本地运行
开发语言·c++·aigc·llama·agi·ai-native
踢球的打工仔3 小时前
PHP面向对象(7)
android·开发语言·php
汤姆yu5 小时前
基于python的外卖配送及数据分析系统
开发语言·python·外卖分析
Yue丶越5 小时前
【C语言】字符函数和字符串函数
c语言·开发语言·算法
翔云 OCR API6 小时前
人脸识别API开发者对接代码示例
开发语言·人工智能·python·计算机视觉·ocr
V***u4536 小时前
MS SQL Server partition by 函数实战二 编排考场人员
java·服务器·开发语言
这是程序猿6 小时前
基于java的ssm框架旅游在线平台
java·开发语言·spring boot·spring·旅游·旅游在线平台
芳草萋萋鹦鹉洲哦6 小时前
【elemen/js】阻塞UI线程导致的开关卡顿如何优化
开发语言·javascript·ui
爱学习的小邓同学6 小时前
C++ --- 多态
开发语言·c++
颜*鸣&空7 小时前
QT实现串口通信+VSPD+串口调试工具
开发语言·qt