widget2image 1.0.2 copy "widget2image: ^1.0.2" to clipboard
widget2image: ^1.0.2 copied to clipboard

A helpful widget which can change any widget to image byte data easily.

使用: #

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Widget2Image( // <--- 包裹需要变成图片的组件
      format: ImageByteFormat.png,
      child: Container(
        alignment: Alignment.center,
        height: 200,
        width: 200,
        color: Colors.red,
        child: Builder(
          builder: (ctx) => IconButton(
            icon: const Icon(Icons.ac_unit),
            onPressed: () => _loadImage(ctx),
          ),
        ),
      ),
    );
  }

  void _loadImage(BuildContext context) async {
    // 获取图片字节 ,注意需要 Widget2Image 下方的上下文
    var bytes = await Widget2Image.of(context).loadImage();
    print(bytes);
    // 获取到图片字节数据 ---- 之后可随意操作
//    final dir = await getTemporaryDirectory();
//    final dest = path.join(dir.path, "widget.png");
//    await File(dest).writeAsBytes(bytes);
//    Scaffold.of(context).showSnackBar(SnackBar(content: Text("图片已保存到:$dest")));
  }
}
5
likes
130
pub points
16%
popularity

Publisher

verified publishertoly1994.com

A helpful widget which can change any widget to image byte data easily.

Homepage

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on widget2image