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.

example/main.dart

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:widget2image/widget2image.dart';

void main() => runApp( const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
          appBar: AppBar(),
          body: HomePage(),
        ));
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

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 {
    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
49%
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