Skip to content

Image capture and full screenshot always return null. Code Snippet Shared #195

@dgurudot

Description

@dgurudot

class WebViewXPageState extends State {
ScreenshotController screenshotController = ScreenshotController();

@OverRide
Widget build(BuildContext context) {
return Scaffold(
body: Screenshot(controller: screenshotController, child: _buildWebViewX()),
);
}

// Function to capture and share the screenshot of the current screen
Future _captureAndShare(BuildContext context) async {
try {
// Capture the screenshot from the widget
print("Capturing screenshot");
final Uint8List? image = await screenshotController.capture(delay: Duration(milliseconds: 10));
if (image != null) {
print("Image is not null");
// Get the directory to save the image
final directory = await getApplicationDocumentsDirectory();
final imagePath = '${directory.path}/image.png';

    // Write the image bytes to a file
    await File(imagePath).writeAsBytes(image);

    // Share the image using share_plus package
    final String body = '''${widget.title}\n\n${widget.shareLink}\n\nvia @AAPWiki Mobile App''';
    await Share.shareXFiles(
      [XFile(imagePath)],
      text: body,
      subject: widget.title,
      sharePositionOrigin: Rect.fromLTWH(0, 0, 10, 10), // For iPad
    );
  } else {
    print("Image captured is null");
  }
} catch (e) {
  print("Error: $e");
}

}

Widget _buildWebViewX() {
return Center(child: Text("Testing"));
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions