Flutter文件浏览器插件samba_browser的使用
Flutter文件浏览器插件samba_browser的使用
该插件能够使用SMB2规范对共享进行多种读操作。目前,支持以下实现:
- Android(使用JCIFS库)
- iOS(使用AMSMB2库)
免责声明! 该插件主要用于实验目的。因此当前提供的功能有限。 然而,我计划在未来扩展该插件。因此,如果您有任何建议或错误报告,我会非常感谢。您可以在我发布的GitHub Issue Board上提供。
示例
SambaBrowser.getShareList('smb://192.168.0.1/', 'domain.net', 'admin', 'password')
.then((shares) => print('Shares found: ${shares.cast<String>()}'));
SambaBrowser.saveFile('./local/', 'downloaded.pdf', 'smb://192.168.0.1/example.pdf', 'domain.net', 'admin', 'password')
.then((path) => print('File downloaded to: $path'));
完整示例代码
以下是完整的示例代码,展示了如何在Flutter应用中使用samba_browser
插件来列出和下载共享文件。
import 'package:flutter/material.dart';
import 'package:samba_browser/samba_browser.dart';
void main() {
runApp(const SambaApp());
}
class SambaApp extends StatefulWidget {
const SambaApp({Key? key}) : super(key: key);
[@override](/user/override)
State<SambaApp> createState() => _SambaAppState();
}
class _SambaAppState extends State<SambaApp> {
String shareUrl = '';
String domain = '';
String username = '';
String password = '';
Future<List>? shareFuture;
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('SAMBA插件示例应用'),
),
body: SingleChildScrollView(
child: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
TextFormField(
onChanged: (text) => shareUrl = text,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'SAMBA共享URL',
),
),
TextFormField(
onChanged: (text) => domain = text,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: '域',
),
),
Row(
children: [
Flexible(
child: TextFormField(
onChanged: (text) => username = text,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: '用户名',
),
),
),
const SizedBox(width: 15.0),
Flexible(
child: TextFormField(
onChanged: (text) => password = text,
obscureText: true,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: '密码',
),
),
),
],
),
TextButton(
onPressed: () => setState(() {
shareFuture = SambaBrowser.getShareList(shareUrl, domain, username, password);
}),
child: const Text("列出可用共享"),
),
const SizedBox(height: 30.0),
if (shareFuture != null) FutureBuilder(future: shareFuture, builder: (context, snapshot) {
if (snapshot.hasError) {
return Column(
children: [
const Text('发生错误。'),
Text(snapshot.error.toString())
]);
}
if (!snapshot.hasData) return const CircularProgressIndicator();
List<String> shares = (snapshot.data as List).cast<String>();
return Column(children: shares.map((e) => Text(e)).toList());
})
],
),
),
),
),
),
);
}
}
更多关于Flutter文件浏览器插件samba_browser的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复