fix(share): fix capture and share methods

This commit is contained in:
ZhuJHua
2025-03-22 23:38:38 +08:00
parent 182008a678
commit f9f2056d04
2 changed files with 7 additions and 5 deletions

View File

@@ -20,18 +20,20 @@ class ShareLogic extends GetxController {
super.onInit();
}
Future<Uint8List> captureWidget() async {
Future<Uint8List> _captureWidget(BuildContext context) async {
final boundary =
state.key.currentContext?.findRenderObject() as RenderRepaintBoundary;
final pixelRatio = MediaQuery.devicePixelRatioOf(Get.context!);
final pixelRatio = MediaQuery.devicePixelRatioOf(context);
final image = await boundary.toImage(pixelRatio: pixelRatio);
final data = await image.toByteData(format: ImageByteFormat.png);
return data!.buffer.asUint8List();
}
Future<void> share() async {
Future<void> share(BuildContext context) async {
final name = 'screenshot-${const Uuid().v7()}.png';
File(FileUtil.getCachePath(name)).writeAsBytes(await captureWidget());
await File(
FileUtil.getCachePath(name),
).writeAsBytes(await _captureWidget(context));
await Share.shareXFiles([XFile(FileUtil.getCachePath(name))]);
}
}

View File

@@ -134,7 +134,7 @@ class SharePage extends StatelessWidget {
),
floatingActionButton: FloatingActionButton(
onPressed: () {
logic.share();
logic.share(context);
},
child: const Icon(Icons.share),
),