33 lines
797 B
Dart
Executable File
33 lines
797 B
Dart
Executable File
import 'dart:typed_data';
|
|
import 'dart:ui';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
|
|
// ignore: must_be_immutable
|
|
class Screenshottable extends StatelessWidget {
|
|
Screenshottable({
|
|
required this.child,
|
|
super.key,
|
|
});
|
|
|
|
final Widget child;
|
|
late BuildContext _context;
|
|
|
|
Future<Uint8List> generateScreenshot() async {
|
|
final boundary = _context.findRenderObject() as RenderRepaintBoundary;
|
|
final image = await boundary.toImage(pixelRatio: 2.5);
|
|
final byteData = await image.toByteData(
|
|
format: ImageByteFormat.png,
|
|
);
|
|
final pngBytes = byteData!.buffer.asUint8List();
|
|
return pngBytes;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
_context = context;
|
|
return RepaintBoundary(child: child);
|
|
}
|
|
}
|