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

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

相关推荐
G_dou_5 小时前
Flutter三方库适配OpenHarmony【countdown_timer】倒计时器项目完整实战
flutter·harmonyos
fqbqrr5 小时前
2606C++,C++构的多态
开发语言·c++
biter down5 小时前
从 0 到 1 搭建 Python 接口自动化测试框架(博客系统实战)
开发语言·python
threelab7 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
武器大师727 小时前
lv_binding_js 代码解读
开发语言·javascript·ecmascript
不知名的老吴7 小时前
线程的生命周期之线程“插队“
java·开发语言·python
kaikaile19958 小时前
数字全息图处理系统(C# 实现)
开发语言·c#
Patrick_Wilson8 小时前
router.replace 之后紧跟 reload,页面为什么无限刷新?
javascript·react.js·浏览器
秋99 小时前
Go语言(Golang)开发工程师全景解析:岗位职责·语言优势与使用场景·各城市薪资·发展前景·高考志愿填报(2026版)
开发语言·golang·高考