flutter_kit_file_open 0.0.2
flutter_kit_file_open: ^0.0.2 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';
import 'expandeble_info_item.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 = '空';
String _data1 = '空';
String _data2 = '空';
String _data3 = '空';
final _flutterFileOpenerPlugin = FlutterKitFileOpen();
// 关键:必须创建 ScrollController
final ScrollController _scrollController = ScrollController();
@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) async {
await result.readData();
setState(() {
_file1 = result.path;
_data1 = result.data;
});
print("方式1:Stream 收到文件路径: ${result.path}");
});
}
// ================= 方式2: 冷启动 / 主动拉取 =================
Future<void> fetchInitialFiles() async {
final result = await FlutterKitFileOpen.getFiles();
if (result != null) {
await result.readData();
setState(() {
_file2 = result.path;
_data2 = result.data;
});
print("方式2:拉取到文件路径: ${result.path}");
} else {
print("方式2:没有文件可拉取");
}
}
// ================= 方式3: 回调方式 =================
void receiveFile() {
FlutterKitFileOpen.receiveFileUri((OpenFileResult? result) async {
if (result != null) {
await result.readData();
setState(() {
_file3 = result.path;
_data3 = result.data;
});
print("方式3:回调收到文件路径: ${result.path}");
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: Scrollbar(
thickness: 30,
thumbVisibility: true, // 始终显示
interactive: true,
radius: Radius.circular(15),
controller: _scrollController,
child: SingleChildScrollView(
controller: _scrollController,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Center(child: Text('Running on: $_platformVersion')),
const SizedBox(height: 20),
ExpandableInfoItem(label: 'file1', value: _file1),
ExpandableInfoItem(label: 'data1', value: _data1, collapsedLines: 5),
ExpandableInfoItem(label: 'file2', value: _file2),
ExpandableInfoItem(label: 'data2', value: _data2, collapsedLines: 5),
ExpandableInfoItem(label: 'file3', value: _file3),
ExpandableInfoItem(label: 'data3', value: _data3, collapsedLines: 5),
],
),
),
),
),
),
);
}
}