rustdesk/lib/pages/file_manager_page.dart

326 lines
10 KiB
Dart
Raw Normal View History

2022-03-07 22:54:34 +08:00
import 'dart:async';
import 'package:flutter/material.dart';
2022-03-07 22:54:34 +08:00
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_hbb/models/file_model.dart';
import 'package:provider/provider.dart';
2022-03-09 17:07:24 +08:00
import 'package:flutter_breadcrumb/flutter_breadcrumb.dart';
2022-03-09 22:43:05 +08:00
import 'package:path/path.dart' as p;
2022-03-07 22:54:34 +08:00
import '../common.dart';
import '../models/model.dart';
import '../widgets/dialog.dart';
class FileManagerPage extends StatefulWidget {
FileManagerPage({Key? key, required this.id}) : super(key: key);
final String id;
@override
State<StatefulWidget> createState() => _FileManagerPageState();
}
class _FileManagerPageState extends State<FileManagerPage> {
2022-03-09 22:43:05 +08:00
final _fileModel = FFI.fileModel;
2022-03-07 22:54:34 +08:00
Timer? _interval;
Timer? _timer;
var _reconnects = 1;
2022-03-09 22:43:05 +08:00
2022-03-07 22:54:34 +08:00
var _isLocal = false;
2022-03-09 17:07:24 +08:00
var _selectMode = false;
2022-03-09 22:43:05 +08:00
final List<String> _selectedItems = []; // 换成entry对象数组
2022-03-09 17:07:24 +08:00
2022-03-07 22:54:34 +08:00
@override
void initState() {
super.initState();
showLoading(translate('Connecting...'));
FFI.connect(widget.id, isFileTransfer: true);
2022-03-09 22:43:05 +08:00
Future.delayed(Duration(seconds: 1), () {
final res = FFI.getByName("read_dir", FFI.getByName("get_home_dir"));
debugPrint("read_dir local :$res");
_fileModel.tryUpdateDir(res, true);
});
2022-03-07 22:54:34 +08:00
_interval = Timer.periodic(Duration(milliseconds: 30),
(timer) => FFI.ffiModel.update(widget.id, context, handleMsgBox));
}
@override
void dispose() {
2022-03-09 22:43:05 +08:00
_fileModel.clear();
2022-03-07 22:54:34 +08:00
_interval?.cancel();
FFI.close();
EasyLoading.dismiss();
super.dispose();
}
@override
Widget build(BuildContext context) {
2022-03-07 22:54:34 +08:00
return ChangeNotifierProvider.value(
2022-03-09 22:43:05 +08:00
value: _fileModel,
2022-03-07 22:54:34 +08:00
child: Scaffold(
2022-03-09 17:07:24 +08:00
backgroundColor: MyTheme.grayBg,
2022-03-07 22:54:34 +08:00
appBar: AppBar(
leading: Row(children: [
IconButton(icon: Icon(Icons.arrow_back), onPressed: goBack),
IconButton(icon: Icon(Icons.close), onPressed: clientClose),
]),
leadingWidth: 200,
centerTitle: true,
title: Text(translate(_isLocal ? "Local" : "Remote")),
actions: [
IconButton(
2022-03-09 22:43:05 +08:00
icon: Icon(Icons.change_circle),
onPressed: () => setState(() {
_isLocal = !_isLocal;
}),
)
2022-03-07 22:54:34 +08:00
],
),
2022-03-09 17:07:24 +08:00
body: body(),
bottomSheet: bottomSheet(),
2022-03-07 22:54:34 +08:00
));
}
2022-03-09 22:43:05 +08:00
Widget body() => Consumer<FileModel>(builder: (context, fileModel, _child) {
final fd =
_isLocal ? fileModel.currentLocalDir : fileModel.currentRemoteDir;
final entries = fd.entries;
return Column(children: [
headTools(),
Expanded(
child: ListView.builder(
itemCount: entries.length + 1,
itemBuilder: (context, index) {
if (index >= entries.length) {
// 添加尾部信息 文件统计信息等
// 添加快速返回上部
// 使用 bottomSheet 提示以选择的文件数量 点击后展开查看更多
return listTail();
}
final path = p.join(fd.path,entries[index].name);
var selected = false;
if (_selectMode) {
selected = _selectedItems.any((e) => e == path);
}
return Card(
child: ListTile(
leading: entries[index].isFile
? Icon(Icons.feed_outlined)
: Icon(Icons.folder),
title: Text(entries[index].name),
trailing: _selectMode
? Checkbox(
value: selected,
onChanged: (v) {
if (v == null) return;
if (v && !selected) {
setState(() {
_selectedItems.add(path);
});
} else if (!v && selected) {
setState(() {
_selectedItems.remove(path);
});
}
})
: null,
onTap: () {
if (entries[index].isDirectory) {
_fileModel.openDirectory(path,_isLocal);
} else {
// Perform file-related tasks.
}
},
onLongPress: () {
setState(() {
_selectedItems.clear();
_selectMode = !_selectMode;
});
},
),
);
},
))
]);
});
2022-03-09 17:07:24 +08:00
2022-03-07 22:54:34 +08:00
goBack() {
2022-03-09 22:43:05 +08:00
_fileModel.goToParentDirectory(_isLocal);
2022-03-07 22:54:34 +08:00
}
void handleMsgBox(Map<String, dynamic> evt, String id) {
var type = evt['type'];
var title = evt['title'];
var text = evt['text'];
if (type == 're-input-password') {
wrongPasswordDialog(id);
} else if (type == 'input-password') {
enterPasswordDialog(id);
} else {
var hasRetry = evt['hasRetry'] == 'true';
print(evt);
showMsgBox(type, title, text, hasRetry);
}
}
void showMsgBox(String type, String title, String text, bool hasRetry) {
msgBox(type, title, text);
if (hasRetry) {
_timer?.cancel();
_timer = Timer(Duration(seconds: _reconnects), () {
FFI.reconnect();
showLoading(translate('Connecting...'));
});
_reconnects *= 2;
} else {
_reconnects = 1;
}
}
2022-03-09 17:07:24 +08:00
Widget headTools() => Container(
child: Row(
children: [
Expanded(
child: BreadCrumb(
items: getPathBreadCrumbItems(() => debugPrint("pressed home"),
(e) => debugPrint("pressed url:$e")),
divider: Icon(Icons.chevron_right),
overflow: ScrollableOverflow(reverse: false), // TODO 计算容器宽度判断
)),
Row(
children: [
// IconButton(onPressed: () {}, icon: Icon(Icons.sort)),
PopupMenuButton<SortBy>(
2022-03-09 22:43:05 +08:00
icon: Icon(Icons.sort),
itemBuilder: (context) {
return SortBy.values
.map((e) => PopupMenuItem(
child:
Text(translate(e.toString().split(".").last)),
value: e,
))
.toList();
},
onSelected: _fileModel.changeSortStyle),
PopupMenuButton<String>(
icon: Icon(Icons.more_vert),
2022-03-09 17:07:24 +08:00
itemBuilder: (context) {
2022-03-09 22:43:05 +08:00
return [
PopupMenuItem(
child: Row(
children: [
Icon(Icons.refresh),
Text("刷新")
],
),
value: "refresh",
)
];
2022-03-09 17:07:24 +08:00
},
2022-03-09 22:43:05 +08:00
onSelected: (v){
if(v == "refresh"){
_fileModel.refresh(_isLocal);
}
}),
2022-03-09 17:07:24 +08:00
],
)
],
));
Widget emptyPage() {
return Column(
children: [
headTools(),
Expanded(child: Center(child: Text("Empty Directory")))
],
);
}
Widget listTail() {
return SizedBox(height: 100);
}
BottomSheet? bottomSheet() {
if (!_selectMode) return null;
return BottomSheet(
backgroundColor: MyTheme.grayBg,
enableDrag: false,
onClosing: () {
debugPrint("BottomSheet close");
},
builder: (context) {
return Container(
height: 65,
alignment: Alignment.centerLeft,
decoration: BoxDecoration(
color: MyTheme.accent50,
borderRadius: BorderRadius.vertical(top: Radius.circular(10))),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Icon(Icons.check),
SizedBox(width: 5),
Text(
"已选择 ${_selectedItems.length}",
style: TextStyle(fontSize: 18),
),
],
),
Row(
children: [
IconButton(
icon: Icon(Icons.paste),
2022-03-09 22:43:05 +08:00
onPressed: () {
debugPrint("paste");
_fileModel.sendFiles(_selectedItems.first, _fileModel.currentRemoteDir.path+'/'+_selectedItems.first.split('/').last, false, false);
},
2022-03-09 17:07:24 +08:00
),
IconButton(
icon: Icon(Icons.delete_forever),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.cancel_outlined),
onPressed: () {
setState(() {
_selectMode = false;
});
},
),
],
)
],
),
),
);
});
}
List<BreadCrumbItem> getPathBreadCrumbItems(
void Function() onHome, void Function(String) onPressed) {
final path = _isLocal
2022-03-09 22:43:05 +08:00
? _fileModel.currentLocalDir.path
: _fileModel.currentRemoteDir.path;
2022-03-09 17:07:24 +08:00
final list = path.trim().split('/');
list.remove("");
final breadCrumbList = [
BreadCrumbItem(
content: IconButton(
icon: Icon(Icons.home_filled),
onPressed: onHome,
))
];
breadCrumbList.addAll(list.map((e) => BreadCrumbItem(
content: TextButton(
child: Text(e),
style:
ButtonStyle(minimumSize: MaterialStateProperty.all(Size(0, 0))),
onPressed: () => onPressed(e)))));
return breadCrumbList;
}
}