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

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

相关推荐
Brilliantwxx几秒前
【C++】认识标准库STL(2)
开发语言·c++
恋猫de小郭几秒前
Flutter 凉了没?Flutter 2026 的未来行程和规划,一些有趣的变化
android·前端·flutter
故事还在继续吗5 分钟前
STL 容器算法手册
开发语言·c++·算法
天天向上10248 分钟前
openlayers 加载Shapefile文件
前端·javascript·html
techdashen12 分钟前
Cloudflare 用 Rust 实现 QUIC 协议:quiche 是怎么设计的
开发语言·后端·rust
wuxianda103013 分钟前
苹果App上架4.3a问题3天解决方案汇报总结
开发语言·javascript·uni-app·ecmascript·ios上架·苹果上架
無斜15 分钟前
【CAPL实用开发】--- CAPL调用 .NET DLL
开发语言·c#·capl·canoe
琹箐15 分钟前
今天吃什么干什么随机生成
javascript·css·css3
石榴树下的七彩鱼15 分钟前
OCR API价格对比2026:身份证/发票/医疗票据识别哪家性价比最高?含Python对接+成本公式
开发语言·人工智能·python·ocr·图像识别·文字识别·api接口