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

如果是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');
        }
      },
    );
  }

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

三、小结

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

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

猜你喜欢

转载自blog.csdn.net/gloryFlow/article/details/132966717