Flutter文件选择插件simple_filepicker_windows的使用
Flutter文件选择插件simple_filepicker_windows的使用
simple_filepicker_windows
是一个用于 Windows 平台的简单文件选择插件,无需任何依赖(希望如此)。
使用方法
import 'package:simple_filepicker_windows/simple_filepicker_windows.dart';
void main() {
final files = SimpleFilepickerWindows.showFilePicker(["*.png", "*.pdf", "myfile.*", "mydocument.txt"], pickMultiple: false);
print(files); // 输出选中的文件路径
}
版本历史
- 1.0.0 - 初始发布
- 1.0.0+7 - 支持 null 安全
- 1.0.0+8 - 小修复
完整示例 Demo
以下是一个完整的 Flutter 应用示例,演示如何使用 simple_filepicker_windows
插件来选择文件:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:simple_filepicker_windows/simple_filepicker_windows.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<String>? _selectedFiles;
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
_selectedFiles = await SimpleFilepickerWindows.showFilePicker(['*.jpg', '*.png'], pickMultiple: true);
} on PlatformException {
if (kDebugMode) {
print('Failed to get platform version.');
}
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('文件选择插件示例'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () {
initPlatformState();
},
child: const Text('选择文件'),
),
if (_selectedFiles != null)
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('选中的文件:\n${_selectedFiles!.join('\n')}\n'),
),
],
),
),
),
);
}
}
更多关于Flutter文件选择插件simple_filepicker_windows的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复