import 'dart:io'; import 'dart:typed_data'; import 'dart:ui' as ui; import 'package:flutter/cupertino.dart'; import 'package:flutter/rendering.dart'; import 'package:lszlgl/main.dart'; import 'package:path_provider/path_provider.dart'; class FileUtils { static Future convertUint8ListToFile(Uint8List data, String fileName) async { final directory = await getApplicationCacheDirectory(); final filePath = '${directory.path}/$fileName'; final file = File(filePath); await file.writeAsBytes(data); return file; } /// 从Context中获取图片 static Future getBitmapFromContext( BuildContext? context, { double pixelRatio = 3.0, }) async { if (context == null) return null; RenderRepaintBoundary render = context.findRenderObject() as RenderRepaintBoundary; ui.Image image = await render.toImage(pixelRatio: pixelRatio); ByteData? bytes = await image.toByteData(format: ui.ImageByteFormat.png); return bytes?.buffer.asUint8List(); } }