image_painter 0.1.6 copy "image_painter: ^0.1.6" to clipboard
image_painter: ^0.1.6 copied to clipboard

outdated

A flutter package for drawing over images. Supports color, strokeWidth, different paintModes and exporting image.

example/lib/main.dart

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_material_color_picker/flutter_material_color_picker.dart';
import 'package:image_painter/image_painter.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(ExampleApp());

class ExampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Image Painter Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: ImagePainterExample(),
    );
  }
}

class ImagePainterExample extends StatefulWidget {
  @override
  _ImagePainterExampleState createState() => _ImagePainterExampleState();
}

class _ImagePainterExampleState extends State<ImagePainterExample> {
  final _imageKey = GlobalKey<ImagePainterState>();
  final _key = GlobalKey<ScaffoldState>();
  Controller imageController;
  List<MapEntry<IconData, PaintMode>> options = [
    MapEntry(Icons.zoom_out_map, PaintMode.None),
    MapEntry(Icons.horizontal_rule, PaintMode.Line),
    MapEntry(Icons.crop_free, PaintMode.Box),
    MapEntry(Icons.edit, PaintMode.FreeStyle),
    MapEntry(Icons.lens_outlined, PaintMode.Circle),
    MapEntry(Icons.arrow_right_alt_outlined, PaintMode.Arrow),
    MapEntry(Icons.power_input, PaintMode.DottedLine)
  ];
  @override
  void initState() {
    imageController =
        Controller(color: Colors.blue, mode: PaintMode.Line, strokeWidth: 4.0);
    super.initState();
  }

  void saveImage() async {
    Uint8List image = await _imageKey.currentState.exportImage();
    final directory = (await getApplicationDocumentsDirectory()).path;
    await Directory('$directory/sample').create(recursive: true);
    String fullPath = '$directory/${DateTime.now().millisecondsSinceEpoch}.png';
    File imgFile = new File('$fullPath');
    imgFile.writeAsBytesSync(image);
    _key.currentState.showSnackBar(
      SnackBar(
        backgroundColor: Colors.grey[700],
        padding: EdgeInsets.only(left: 10),
        content: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text("Image Exported successfully.",
                style: TextStyle(color: Colors.white)),
            FlatButton(
                onPressed: () => OpenFile.open("$fullPath"),
                child: Text("Open", style: TextStyle(color: Colors.blue[200])))
          ],
        ),
      ),
    );
  }

  void _openMainColorPicker() async {
    await showDialog<Color>(
      context: context,
      builder: (_) {
        return StatefulBuilder(
          builder: (context, setstate) {
            return AlertDialog(
              contentPadding: const EdgeInsets.all(6.0),
              title: Text("Pick a color"),
              content: MaterialColorPicker(
                  shrinkWrap: true,
                  selectedColor: imageController.color,
                  allowShades: false,
                  onMainColorChange: (color) {
                    setstate(() {
                      imageController = imageController.copyWith(color: color);
                    });
                  }),
              actions: [
                FlatButton(
                    child: Text('Done'),
                    onPressed: () {
                      Navigator.of(context).pop();
                      setState(() {});
                    }),
              ],
            );
          },
        );
      },
    );
  }

  void _openStrokeDialog() async {
    await showDialog<double>(
      context: context,
      builder: (_) {
        return StatefulBuilder(
          builder: (context, setstate) {
            return Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                AlertDialog(
                  contentPadding: const EdgeInsets.all(6.0),
                  title: Text("Set StrokeWidth"),
                  content: Column(
                    children: [
                      CupertinoSlider(
                        value: imageController.strokeWidth,
                        min: 2.0,
                        max: 20.0,
                        divisions: 9,
                        onChanged: (value) {
                          setstate(() {
                            imageController =
                                imageController.copyWith(strokeWidth: value);
                          });
                        },
                      ),
                      Text(
                        "${imageController.strokeWidth.toInt()}",
                        style: TextStyle(fontSize: 20, color: Colors.blue),
                      )
                    ],
                  ),
                  actions: [
                    FlatButton(
                        child: Text('Done'),
                        onPressed: () {
                          Navigator.of(context).pop();
                          setState(() {});
                        }),
                  ],
                ),
              ],
            );
          },
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        key: _key,
        appBar: AppBar(
          title: Text("Image Painter Example"),
          actions: [
            IconButton(
                icon: Icon(Icons.brush_sharp),
                onPressed: () {
                  _openStrokeDialog();
                }),
            IconButton(
                icon: Icon(Icons.color_lens),
                onPressed: () {
                  _openMainColorPicker();
                }),
            IconButton(icon: Icon(Icons.save), onPressed: () => saveImage()),
          ],
          bottom: PreferredSize(
            preferredSize: Size.fromHeight(40),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                IconButton(
                    icon: Icon(Icons.reply, color: Colors.white),
                    onPressed: () => _imageKey.currentState.undo()),
                IconButton(
                    icon: Icon(Icons.clear, color: Colors.white),
                    onPressed: () {
                      _imageKey.currentState.clearAll();
                    }),
              ],
            ),
          ),
        ),
        body: Column(
          children: [
            Container(
              width: double.infinity,
              padding: EdgeInsets.all(10),
              color: Colors.black54,
              child: Wrap(
                  alignment: WrapAlignment.center,
                  spacing: 5,
                  children: options.map((item) {
                    return SelectionItems(
                        icon: item.key,
                        isSelected: imageController.mode == item.value,
                        onTap: () {
                          setState(() {
                            imageController =
                                imageController.copyWith(mode: item.value);
                          });
                        });
                  }).toList()),
            ),
            Expanded(
              child: ImagePainter.asset("assets/sample.jpg",
                  key: _imageKey, controller: imageController),
            ),
          ],
        ));
  }
}

class SelectionItems extends StatelessWidget {
  final bool isSelected;
  final IconData icon;
  final VoidCallback onTap;

  const SelectionItems({Key key, this.isSelected, this.icon, this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      clipBehavior: Clip.none,
      children: [
        GestureDetector(
          onTap: onTap,
          child: Container(
            padding: EdgeInsets.all(10),
            decoration: BoxDecoration(
                color: isSelected ? Colors.white70 : Colors.transparent,
                shape: BoxShape.circle),
            child: Icon(icon,
                color: isSelected ? Colors.blue : Colors.white, size: 20),
          ),
        ),
        if (isSelected)
          Positioned(
            top: 0,
            right: 0,
            child: Container(
              decoration:
                  BoxDecoration(shape: BoxShape.circle, color: Colors.green),
              child: Icon(Icons.check, color: Colors.white, size: 10),
            ),
          )
      ],
    );
  }
}
193
likes
0
pub points
94%
popularity

Publisher

unverified uploader

A flutter package for drawing over images. Supports color, strokeWidth, different paintModes and exporting image.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on image_painter