widget_to_img 0.1.0 copy "widget_to_img: ^0.1.0" to clipboard
widget_to_img: ^0.1.0 copied to clipboard

A Flutter package to capture a widget as an image.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:widget_to_img/widget_to_img.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Widget to Image Example',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Widget to Image Example'),
        ),
        body: const Center(
          child: WidgetCaptureDemo(),
        ),
      ),
    );
  }
}

class WidgetCaptureDemo extends StatefulWidget {
  const WidgetCaptureDemo({super.key});

  @override
  State<WidgetCaptureDemo> createState() => _WidgetCaptureDemoState();
}

class _WidgetCaptureDemoState extends State<WidgetCaptureDemo> {
  final GlobalKey _globalKey = GlobalKey();
  Image? _capturedImage;

  void _captureWidget() async {
    final image = await WidgetToImage.captureAsImage(
      _globalKey,
    );
    setState(() {
      _capturedImage = image;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        WidgetToImage(
          captureKey: _globalKey,
          child: Container(
            color: Colors.blue,
            padding: const EdgeInsets.all(20),
            child: const Text(
              'Capture this widget!',
              style: TextStyle(color: Colors.white, fontSize: 24),
            ),
          ),
        ),
        const SizedBox(height: 20),
        ElevatedButton(
          onPressed: _captureWidget,
          child: const Text('Capture Widget'),
        ),
        const SizedBox(height: 20),
        if (_capturedImage != null) _capturedImage!,
      ],
    );
  }
}
2
likes
160
points
19
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package to capture a widget as an image.

Repository (GitHub)
View/report issues

Topics

#widget #image #capture #screenshot

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on widget_to_img