flutter如何让filepicker显示中文

我在使用Flutter的file_picker插件时,发现文件选择器的界面默认是英文的。请问如何让它显示中文界面?需要配置什么参数还是需要引入额外的语言包?

2 回复

FilePickerFileType设置中,添加allowedExtensions并指定中文文件类型,如['.txt', '.pdf']。同时确保系统语言为中文,或使用MaterialLocalizations本地化支持。

更多关于flutter如何让filepicker显示中文的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中让FilePicker显示中文,可以通过以下方法实现:

1. 配置Android中文支持(android/app/src/main/AndroidManifest.xml):

<application
    android:label="文件选择器"
    android:name="io.flutter.app.FlutterApplication">
    
    <activity
        android:name="com.mr.flutter.plugin.filepicker.FilePickerPlugin"
        android:theme="@style/FilePickerTheme">
        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />
            <category android:name="android.intent.category.OPENABLE" />
            <data android:mimeType="*/*" />
        </intent-filter>
    </activity>
</application>

2. 配置iOS中文支持(ios/Runner/Info.plist):

<key>CFBundleDisplayName</key>
<string>文件选择器</string>
<key>CFBundleName</key>
<string>文件选择器</string>

3. 使用FilePicker时的参数设置:

FilePickerResult? result = await FilePicker.platform.pickFiles(
  type: FileType.custom,
  allowedExtensions: ['jpg', 'pdf', 'doc'],
  dialogTitle: '选择文件', // 设置对话框标题
  allowMultiple: false,
);

4. 完整示例代码:

import 'package:file_picker/file_picker.dart';

void pickFile() async {
  FilePickerResult? result = await FilePicker.platform.pickFiles(
    type: FileType.custom,
    allowedExtensions: ['jpg', 'pdf', 'txt'],
    dialogTitle: '请选择文件',
    allowMultiple: false,
  );

  if (result != null) {
    PlatformFile file = result.files.first;
    print('文件名称: ${file.name}');
    print('文件大小: ${file.size}');
  } else {
    // 用户取消了选择
  }
}

注意事项:

  • 确保应用支持中文语言包
  • 某些系统语言设置可能会影响显示效果
  • 不同平台(Android/iOS)的显示效果可能略有差异

这样配置后,FilePicker的界面元素就会显示为中文了。

回到顶部