123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import 'dart:convert';
- import 'dart:ui' as ui;
- import 'dart:io';
- import 'package:image/image.dart' as img;
- import 'package:camera/camera.dart';
- import 'package:file_picker/file_picker.dart';
- import 'package:flutter/foundation.dart';
- import "package:universal_html/html.dart" as html;
- import 'package:flutter/foundation.dart' show kIsWeb;
- import '../models/media_model.dart';
- import '../services/base_service.dart';
- //import 'dart:html' as html show window;
- //import 'dart:html' as html;
- class MediaViewModel extends ChangeNotifier {
- List<XFile> _audios = [];
- List<XFile> get audios => _audios;
- List<XFile> _temporales = [];
- List<XFile> get temporales => _temporales;
- List<XFile> _archivos = [];
- List<XFile> get archivos => _archivos;
- bool _isLoading = false;
- bool get isLoading => _isLoading;
- Future<void> fetchAudio() async {
- _temporales = [];
- notifyListeners();
- }
- Future agregarAudio(XFile value) async {
- _audios.add(value);
- notifyListeners();
- }
- Future limpiar() async {
- _audios = [];
- _temporales = [];
- _archivos = [];
- notifyListeners();
- }
- void setIsLoading(bool loading) {
- _isLoading = loading;
- notifyListeners();
- }
- Future guardarAudio(
- {required int idModuloLocal,
- File? audio,
- required String nombreModulo}) async {}
- Future<XFile?> convertirPlatformFileAXFile(PlatformFile platformFile) async {
- if (!kIsWeb) {
- final XFile xFile = XFile(platformFile.path.toString(),
- name: platformFile.name, mimeType: "archivo");
- return xFile;
- }
- try {
- final Uint8List bytes = platformFile.bytes!;
- final html.Blob blob = html.Blob([bytes], 'application/octet-stream');
- String url = html.Url.createObjectUrlFromBlob(blob);
- final XFile xFile =
- XFile(url.toString(), name: platformFile.name, mimeType: "archivo");
- int lastIndex = xFile.name.lastIndexOf('.');
- if (lastIndex != -1 && lastIndex < xFile.name.length - 1) {
- String ext = xFile.name.substring(lastIndex + 1);
- }
- return xFile;
- } catch (e) {
- print('Error al convertir PlatformFile a XFile: $e');
- return null;
- }
- }
- agregarArchivo(XFile? value) {
- _archivos.add(value!);
- notifyListeners();
- }
- Future<XFile> convertirAImagePickerWebFile(ui.Image image) async {
- // Convertir ui.Image a lista de bytes (Uint8List)
- ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
- Uint8List pngBytes = byteData!.buffer.asUint8List();
- // Crear imagen con el paquete 'image'
- img.Image imgImage = img.decodeImage(pngBytes)!;
- // Convertir imagen a lista de bytes (Uint8List)
- Uint8List imgBytes = img.encodePng(imgImage) as Uint8List;
- // Crear archivo temporal
- final tempDir = Directory.systemTemp;
- final tempFile =
- await File('${tempDir.path}/temp_image.png').writeAsBytes(imgBytes);
- // Crear XFile a partir del archivo temporal
- return XFile(tempFile.path);
- }
- Future<void> eliminar(Media m) async {
- var r = await BaseService()
- .delete("admin/media", body: {"id": m.id.toString()});
- Map<String, dynamic> resJson = jsonDecode(r.body);
- if (r.statusCode == 200) {}
- }
- }
|