flutter_kit_file_open 0.0.1
flutter_kit_file_open: ^0.0.1 copied to clipboard
Private plugins / packages
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_kit_file_open/flutter_kit_file_open.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _file1 = '空';
String _file2 = '空';
String _file3 = '空';
final _flutterFileOpenerPlugin = FlutterKitFileOpen();
@override
void initState() {
super.initState();
initPlatformState();
listen();
fetchInitialFiles();
receiveFile();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = await FlutterKitFileOpen.getPlatformVersion() ?? 'Unknown platform version';
} on PlatformException {
platformVersion = '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(() {
_platformVersion = platformVersion;
});
}
// ================= 方式1: 运行中实时接收文件(Stream) =================
void listen() {
FlutterKitFileOpen.fileStream.listen((OpenFileResult result) {
setState(() {
_file1 = result.paths.map(($0) => $0).join(' |');
});
print("方式1:Stream 收到文件路径: ${result.paths}");
});
}
// ================= 方式2: 冷启动 / 主动拉取 =================
Future<void> fetchInitialFiles() async {
final result = await FlutterKitFileOpen.getFiles();
if (result != null) {
setState(() {
_file2 = result.paths.map(($0) => $0).join(' |');
});
print("方式2:拉取到文件路径: ${result.paths}");
} else {
print("方式2:没有文件可拉取");
}
}
// ================= 方式3: 回调方式 =================
void receiveFile() {
FlutterKitFileOpen.receiveFileUri((dynamic result) {
if (result != null) {
setState(() {
_file3 = result.paths.map(($0) => $0).join(' |');
});
print("方式3:回调收到文件路径: ${result.paths}");
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: Column(
spacing: 10,
children: [
Center(child: Text('Running on: $_platformVersion\n')),
Center(child: Text('file1 $_file1\n')),
Center(child: Text('file2 $_file2\n')),
Center(child: Text('file3 $_file3\n')),
],
),
),
);
}
}