Files
tuiche/lib/controller/setting/pdf/UserPdfController.dart
2025-05-22 08:56:27 +08:00

41 lines
1.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;
class UserPdfController extends GetxController {
var localPdfPath = Rx<String?>(null);
// 加载 PDF 文件
Future<void> loadPdf(int type, [String? url]) async {
final tempDir = await getTemporaryDirectory();
final filename = type == 1 ? 'service.pdf' : 'privacy.pdf';
final filePath = p.join(tempDir.path, filename);
final file = File(filePath);
try {
if (url == null || url.isEmpty) {
final byteData = await rootBundle
.load(type == 1 ? 'assets/img/服务协议.pdf' : 'assets/img/隐私协议.pdf');
await file.writeAsBytes(byteData.buffer.asUint8List());
} else {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
await file.writeAsBytes(response.bodyBytes);
} else {
throw Exception('【PDF加载】类型$type:无法下载 PDF状态码 ${response.statusCode}');
}
}
} catch (e) {
final byteData = await rootBundle
.load(type == 1 ? 'assets/img/服务协议.pdf' : 'assets/img/隐私协议.pdf');
await file.writeAsBytes(byteData.buffer.asUint8List());
}
localPdfPath.value = filePath;
}
}