image_painter_hap 0.6.2 image_painter_hap: ^0.6.2 copied to clipboard
A flutter package for drawing over images. Supports color, strokeWidth, different paintModes and exporting image.
import 'dart:io';
import 'package:flutter/material.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>();
void saveImage() async {
final image = await _imageKey.currentState?.exportImage();
final directory = (await getApplicationDocumentsDirectory()).path;
await Directory('$directory/sample').create(recursive: true);
final fullPath =
'$directory/sample/${DateTime.now().millisecondsSinceEpoch}.png';
final imgFile = File('$fullPath');
if (image != null) {
imgFile.writeAsBytesSync(image);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.grey[700],
padding: const EdgeInsets.only(left: 10),
content: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text("Image Exported successfully.",
style: TextStyle(color: Colors.white)),
TextButton(
onPressed: () => OpenFile.open("$fullPath"),
child: Text(
"Open",
style: TextStyle(
color: Colors.blue[200],
),
),
)
],
),
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Image Painter Example"),
actions: [
IconButton(
icon: const Icon(Icons.save_alt),
onPressed: saveImage,
)
],
),
body: ImagePainter.asset(
"assets/sample.jpg",
key: _imageKey,
scalable: true,
initialStrokeWidth: 2,
textDelegate: TextDelegate(),
initialColor: Colors.green,
initialPaintMode: PaintMode.line,
),
);
}
}