ym_open_file_plus 1.0.0
ym_open_file_plus: ^1.0.0 copied to clipboard
Open local files with the platform's default app on Android, iOS, Linux, macOS, Windows, and the web.
import 'package:flutter/material.dart';
import 'package:ym_open_file_plus/ym_open_file_plus.dart';
void main() {
runApp(const OpenFileExampleApp());
}
class OpenFileExampleApp extends StatelessWidget {
const OpenFileExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ym_open_file_plus example',
theme: ThemeData(colorSchemeSeed: Colors.indigo, useMaterial3: true),
home: const OpenFileExamplePage(),
);
}
}
class OpenFileExamplePage extends StatefulWidget {
const OpenFileExamplePage({super.key});
@override
State<OpenFileExamplePage> createState() => _OpenFileExamplePageState();
}
class _OpenFileExamplePageState extends State<OpenFileExamplePage> {
final _pathController = TextEditingController(
text: '/storage/emulated/0/Download/example.pdf',
);
OpenResult _result = const OpenResult(message: 'Unknown');
bool _opening = false;
@override
void dispose() {
_pathController.dispose();
super.dispose();
}
Future<void> _openFile() async {
if (_opening) return;
setState(() => _opening = true);
final result = await OpenFile.open(_pathController.text);
if (!mounted) return;
setState(() {
_result = result;
_opening = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('ym_open_file_plus')),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Enter a local path, content URI, or web URL.',
style: TextStyle(fontSize: 16),
),
const SizedBox(height: 16),
TextField(
controller: _pathController,
keyboardType: TextInputType.url,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'File path or URL',
),
),
const SizedBox(height: 16),
FilledButton.icon(
onPressed: _opening ? null : _openFile,
icon: const Icon(Icons.open_in_new),
label: Text(_opening ? 'Opening…' : 'Open file'),
),
const SizedBox(height: 24),
Text(
'Open result: type=${_result.type}, message=${_result.message}',
),
],
),
),
);
}
}