Operation.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:fluttertoast/fluttertoast.dart';
  5. import 'package:path/path.dart' as p;
  6. import '../Item/Common.dart';
  7. import '../Item/Mode.dart';
  8. class Operation {
  9. List<FileSystemEntity> leftFiles = []; //左列的文件
  10. List<FileSystemEntity> rightFiles = []; //右列的文件
  11. BuildContext context;
  12. ValueNotifier<bool> uiShouldChange;
  13. Mode mode;
  14. Operation(this.leftFiles, this.rightFiles, this.context,
  15. {this.uiShouldChange, this.mode});
  16. //添加到favorite
  17. void addToFavorite(FileSystemEntity file, int type) {
  18. bool flag = isInFavorite(file.path);
  19. if (!flag) {
  20. try {
  21. File favoriteTxt = File(Common().favoriteDir + '/favorite.txt');
  22. favoriteTxt.copy(Common().sDCardDir + '/1/test.txt');
  23. if (Common().favoriteAll.length > 0) {
  24. Common().favoriteAll =
  25. Common().favoriteAll + '\n' + file.path.toString();
  26. } else {
  27. Common().favoriteAll = Common().favoriteAll + file.path.toString();
  28. }
  29. Common().favoriteFileList.add(file.path.toString());
  30. favoriteTxt.writeAsStringSync(Common().favoriteAll);
  31. print('收藏' + file.path);
  32. } catch (err) {
  33. print('错误信息' + err.toString());
  34. }
  35. }
  36. showCupertinoDialog(
  37. context: context,
  38. builder: (BuildContext context) {
  39. return CupertinoAlertDialog(
  40. title: Text(flag ? '条目已存在' : '添加成功'),
  41. actions: <Widget>[
  42. CupertinoDialogAction(
  43. child: Text('确定'),
  44. onPressed: () {
  45. Navigator.pop(context);
  46. },
  47. )
  48. ],
  49. );
  50. });
  51. }
  52. //重命名文件
  53. void renameFile(FileSystemEntity file, int type) {
  54. TextEditingController _controller = TextEditingController();
  55. if (context == null) {
  56. print('context为null2');
  57. }
  58. showCupertinoDialog(
  59. context: context,
  60. builder: (BuildContext context) {
  61. return Scaffold(
  62. backgroundColor: Colors.transparent,
  63. body: Center(
  64. child: CupertinoAlertDialog(
  65. title: Text('重命名'),
  66. content: Padding(
  67. padding: EdgeInsets.only(top: 10),
  68. child: TextField(
  69. controller: _controller,
  70. decoration: InputDecoration(
  71. hintText: '请输入新名称',
  72. border: OutlineInputBorder(
  73. borderRadius: BorderRadius.circular(10.0),
  74. borderSide: BorderSide(width: 0.3)),
  75. focusedBorder: OutlineInputBorder(
  76. borderRadius: BorderRadius.circular(10.0),
  77. borderSide: BorderSide(width: 0.5)),
  78. contentPadding: EdgeInsets.all(8.0),
  79. ),
  80. ),
  81. ),
  82. actions: <Widget>[
  83. CupertinoButton(
  84. child: Text(
  85. '取消',
  86. style: TextStyle(color: Colors.blue),
  87. ),
  88. onPressed: () {
  89. Navigator.pop(context);
  90. },
  91. ),
  92. CupertinoButton(
  93. child: Text(
  94. '确认',
  95. style: TextStyle(color: Colors.blue),
  96. ),
  97. onPressed: () async {
  98. String newName = _controller.text;
  99. if (newName.trim().length == 0) {
  100. Fluttertoast.showToast(
  101. msg: '名字不能为空', gravity: ToastGravity.BOTTOM);
  102. return;
  103. }
  104. String newPath = "";
  105. if (file.statSync().type == FileSystemEntityType.file) {
  106. newPath = file.parent.path +
  107. '/' +
  108. newName +
  109. p.extension(file.path);
  110. } else {
  111. newPath = file.parent.path + '/' + newName;
  112. }
  113. file.renameSync(newPath);
  114. if (type == -1) {
  115. initPathFiles(newPath, -3);
  116. } else {
  117. initPathFiles(file.parent.path, type);
  118. }
  119. Navigator.pop(context);
  120. },
  121. )
  122. ],
  123. ),
  124. ),
  125. );
  126. });
  127. }
  128. //用传来的路径刷新该目录下的文件.文件夹
  129. void initPathFiles(String path, int type) {
  130. try {
  131. //可变的Widget可以使用setState()函数--重新绘制试图--调用build方法--绘制不一样的地方
  132. //-1: 右列-->父目录下 (点了左边列表,刷新右边列表,parentDir时左边的)
  133. // 1: 右列-->父目录下 (点了右列的文件夹的情况,parenDir换成了右边的)
  134. // 左列-->父目录同级
  135. //-2: 左列-->当前父目录下 (初始化时,用sdcard刷线左列)
  136. // 2: 右列-->父目录同级 (按了返回键)
  137. // 左列-->父目录的父目录同级
  138. //-3: 左列-->父目录同级 (原地刷新)
  139. // 右列-->父目录下
  140. mode.parentDir = Directory(path);
  141. print('parentDir 更改为' + mode.parentDir.path);
  142. if (type == -1) {
  143. //点击左列
  144. //刷新右列表
  145. sortFile(mode.parentDir, -1);
  146. } else if (type == 1) {
  147. //点击右列
  148. //往右走 先刷右列表 再刷左列表
  149. sortFile(mode.parentDir, -1);
  150. sortFile(mode.parentDir.parent, 1);
  151. } else if (type == -2) {
  152. //初始化
  153. sortFile(mode.parentDir, 1);
  154. } else if (type == 2) {
  155. //返回时
  156. sortFile(mode.parentDir.parent, -1);
  157. sortFile(mode.parentDir.parent.parent, 1);
  158. mode.parentDir = mode.parentDir.parent;
  159. } else if (type == -3) {
  160. //左列重命名文件夹
  161. sortFile(mode.parentDir.parent, 1);
  162. sortFile(mode.parentDir, -1);
  163. }
  164. } catch (e) {
  165. print(e);
  166. print("Directory does not exist");
  167. }
  168. changeUI();
  169. }
  170. void changeUI() {
  171. if (uiShouldChange == null) {
  172. print('uiShouldChange is null');
  173. }
  174. if (uiShouldChange.value) {
  175. uiShouldChange.value = false;
  176. } else {
  177. uiShouldChange.value = true;
  178. }
  179. }
  180. //目录排序
  181. void sortFile(Directory parentDir, int type) {
  182. List<FileSystemEntity> _Files = [];
  183. List<FileSystemEntity> _Folders = [];
  184. //parenDire--路径
  185. //Directory.listSync 会列出当前目录下所有的文件和文件夹
  186. for (var v in parentDir.listSync()) {
  187. //去除.开头的文件/文件夹
  188. if (p.basename(v.path).substring(0, 1) == '.') {
  189. continue;
  190. }
  191. if (FileSystemEntity.isFileSync(v.path))
  192. _Files.add(v);
  193. else
  194. _Folders.add(v);
  195. }
  196. _Files.sort((a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
  197. _Folders.sort(
  198. (a, b) => a.path.toLowerCase().compareTo(b.path.toLowerCase()));
  199. if (type == -1) {
  200. //刷新右列
  201. rightFiles.clear();
  202. rightFiles.addAll(_Folders);
  203. rightFiles.addAll(_Files);
  204. } else {
  205. //刷新左列
  206. leftFiles.clear();
  207. leftFiles.addAll(_Folders);
  208. leftFiles.addAll(_Files);
  209. }
  210. changeUI();
  211. }
  212. //文件是否在favorite中
  213. bool isInFavorite(String filePath) {
  214. bool flag = false;
  215. for (var fileItem in Common().favoriteFileList) {
  216. if (fileItem == filePath) {
  217. flag = true;
  218. }
  219. }
  220. return flag;
  221. }
  222. //将更新后的喜爱条目写回文件
  223. void writeIntoLocal() {
  224. String temp = '';
  225. int i = 0;
  226. for (var item in Common().favoriteFileList) {
  227. print('写入' + item);
  228. if (i == 0) {
  229. temp = item.toString();
  230. } else {
  231. temp = temp + '\n' + item.toString();
  232. }
  233. i++;
  234. }
  235. Common().favoriteAll = temp;
  236. try {
  237. File favoriteTxt = File(Common().favoriteDir + '/favorite.txt');
  238. favoriteTxt.writeAsStringSync(Common().favoriteAll);
  239. } catch (err) {
  240. print('错误信息' + err.toString());
  241. }
  242. }
  243. void copyToDir(FileSystemEntity file, int type) {
  244. mode.cutMode = false;
  245. mode.copyMode = true;
  246. mode.copyTempFile = file;
  247. }
  248. //剪切文件到
  249. void cutToDir(FileSystemEntity file, int type) {
  250. mode.copyMode = false;
  251. mode.cutMode = true;
  252. mode.copyTempFile = file;
  253. }
  254. //从favorite中移除
  255. void removeFavorite(FileSystemEntity file, int type) {
  256. bool flag = isInFavorite(file.path);
  257. if (flag) {
  258. //如果要取消收藏的文件在favorite条目中存在
  259. int i = 0;
  260. //从Common中的喜爱条目中删除它
  261. for (var item in Common().favoriteFileList) {
  262. print(i.toString() + ' ' + item.toString());
  263. if (item.toString() == file.path) {
  264. Common().favoriteFileList.removeAt(i);
  265. break;
  266. }
  267. i++;
  268. }
  269. //将更新后的喜爱条目写回文件
  270. writeIntoLocal();
  271. changeUI();
  272. }
  273. showCupertinoDialog(
  274. context: context,
  275. builder: (BuildContext context) {
  276. return CupertinoAlertDialog(
  277. title: Text(flag ? '已取消收藏' : '取消收藏失败'),
  278. actions: <Widget>[
  279. CupertinoDialogAction(
  280. child: Text('确定'),
  281. onPressed: () {
  282. Navigator.pop(context);
  283. },
  284. )
  285. ],
  286. );
  287. Navigator.pop(context);
  288. });
  289. }
  290. //删除文件
  291. void deleteFile(FileSystemEntity file, int type) {
  292. showCupertinoDialog(
  293. context: context,
  294. builder: (BuildContext context) {
  295. return CupertinoAlertDialog(
  296. title: Text('重命名'),
  297. content: Text('删除后不可恢复'),
  298. actions: <Widget>[
  299. CupertinoDialogAction(
  300. child: Text(
  301. '取消',
  302. style: TextStyle(color: Colors.blue),
  303. ),
  304. onPressed: () {
  305. Navigator.pop(context);
  306. },
  307. ),
  308. CupertinoDialogAction(
  309. child: Text(
  310. '确定',
  311. style: TextStyle(color: Colors.blue),
  312. ),
  313. onPressed: () {
  314. String temp = file.parent.path;
  315. if (file.statSync().type == FileSystemEntityType.directory) {
  316. Directory directory = Directory(file.path);
  317. directory.deleteSync(recursive: true);
  318. } else if (file.statSync().type ==
  319. FileSystemEntityType.file) {
  320. file.deleteSync();
  321. }
  322. //TODO
  323. if (type == -1) {
  324. //删除了左边的文件
  325. sortFile(Directory(temp), 1);
  326. } else if (type == 1) {
  327. //删除了右边的文件
  328. initPathFiles(mode.parentDir.path, -1);
  329. }
  330. changeUI();
  331. Navigator.pop(context);
  332. },
  333. )
  334. ],
  335. );
  336. });
  337. }
  338. }