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


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

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

    一、pubspec.yaml设置flutter_localizations

    在pubspec.yaml中设置flutter_localizations

    dependencies:
      flutter:
        sdk: flutter
      flutter_localizations:
        sdk: flutter
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    在这里插入图片描述

    二、设置main.dart的入口locale

    在MaterialApp设置locale、supportedLocales

    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');
            }
          },
        );
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

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

    三、小结

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

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

  • 相关阅读:
    github上的nemo_go项目
    什么是埃及COC认证?埃及COC认证是什么意思?
    固态硬盘SSD
    Android ConstraintLayout app:layout_constraintHorizontal_weight
    JC/T 482-2022 聚氨酯建筑密封胶检测
    扬帆际海—为什么要做shopee跨境本土店
    Java内卷,仅凭这份10w字Java面试合集我已拿下15个offer
    Dart语言入门
    IDEA的快捷键大全
    Vue单文件组件
  • 原文地址:https://blog.csdn.net/gloryFlow/article/details/132966717