截图与分享是常见需求,推荐用 screenshot + share_plus。
0. 依赖
dependencies:
screenshot: ^2.5.0
share_plus: ^7.2.1
1. 截图组件
final controller = ScreenshotController();
Screenshot(
controller: controller,
child: YourWidget(),
);
2. 生成图片并分享
final image = await controller.capture();
if (image == null) return;
final dir = await getTemporaryDirectory();
final file = File('${dir.path}/share.png');
await file.writeAsBytes(image);
await Share.shareXFiles([XFile(file.path)], text: '分享内容');
3. 常见坑点
- 空图片:截图节点还未渲染完成
- 权限未处理:iOS 分享失败
4. 实践清单
- 截图节点独立封装
- 临时文件清理
- 分享文案可配置