game_manager.dart 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 = await getExternalStorageDirectory();
  10. String documentsPath = documentsDir.path;
  11. // 先创建目录
  12. Directory saveDir = Directory("$documentsPath/apk/${game.id}");
  13. if (!await saveDir.exists()) {
  14. saveDir.createSync(recursive: true);
  15. }
  16. String savePath = "${saveDir.path}/${getFileName(game)}";
  17. File _file = File(savePath);
  18. if (!await _file.exists()) {
  19. _file.createSync();
  20. }
  21. print("file: $_file");
  22. return _file;
  23. }
  24. static deleteFile(GameInfoData game) async {
  25. Directory documentsDir = await getExternalStorageDirectory();
  26. String documentsPath = documentsDir.path;
  27. Directory saveDir = Directory("$documentsPath/apk/${game.id}");
  28. if (!await saveDir.exists()) {
  29. return;
  30. }
  31. var files = saveDir.listSync();
  32. files.forEach((element) {
  33. element.delete();
  34. });
  35. }
  36. }