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

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

相关推荐
宋恩淇要努力33 分钟前
C++继承
开发语言·c++
沿着路走到底2 小时前
python 基础
开发语言·python
記億揺晃着的那天2 小时前
Vue + Element UI 表格自适应高度如何做?
javascript·vue.js·ui
沐知全栈开发2 小时前
C# 委托(Delegate)
开发语言
GISer_Jing3 小时前
ByteDance——jy真题
前端·javascript·面试
真的想不出名儿3 小时前
Vue 中 props 传递数据的坑
前端·javascript·vue.js
任子菲阳3 小时前
学Java第三十四天-----抽象类和抽象方法
java·开发语言
阳光阴郁大boy3 小时前
星座运势网站技术解析:从零打造现代化Web应用
前端·javascript
sorryhc3 小时前
如何设计一个架构良好的前端请求库?
前端·javascript·架构
Queen_sy4 小时前
vue3 el-date-picker 日期选择器校验规则-选择日期范围不能超过七天
javascript·vue.js·elementui