game_manager.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'dart:io';
  2. import 'package:path_provider/path_provider.dart';
  3. import 'package:sport/bean/game.dart';
  4. class GameManager {
  5. static String getFileName(GameInfoData game) {
  6. return "${game.version}.apk";
  7. }
  8. static Future<File?> createFile(GameInfoData game) async {
  9. Directory? documentsDir = Platform.isIOS ? await getTemporaryDirectory() : await getExternalStorageDirectory();
  10. if(documentsDir == null)
  11. return null;
  12. String documentsPath = documentsDir.path;
  13. // 先创建目录
  14. Directory saveDir = Directory("$documentsPath/apk/${game.id}");
  15. if (!await saveDir.exists()) {
  16. saveDir.createSync(recursive: true);
  17. }
  18. String savePath = "${saveDir.path}/${getFileName(game)}";
  19. File _file = File(savePath);
  20. if (!await _file.exists()) {
  21. _file.createSync();
  22. }
  23. print("file: $_file");
  24. return _file;
  25. }
  26. static deleteFile(GameInfoData game) async {
  27. Directory? documentsDir = Platform.isIOS ? await getTemporaryDirectory() : await getExternalStorageDirectory();
  28. if(documentsDir == null)
  29. return null;
  30. String documentsPath = documentsDir.path;
  31. Directory saveDir = Directory("$documentsPath/apk/${game.id}");
  32. if (!await saveDir.exists()) {
  33. return;
  34. }
  35. var files = saveDir.listSync();
  36. files.forEach((element) {
  37. // if(!element.path.endsWith(".apk"))
  38. element.delete();
  39. });
  40. }
  41. }