Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saving signature #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 94 additions & 0 deletions example/drawing_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;


import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
Expand All @@ -17,19 +20,38 @@ class _DrawingPageState extends State<DrawingPage> {
List<Stroke> lines = <Stroke>[];

Stroke? line;
ui.PictureRecorder recorder = ui.PictureRecorder();

StrokeOptions options = StrokeOptions();
Canvas? canvas;
Sketcher? sketcher;

StreamController<Stroke> currentLineStreamController =
StreamController<Stroke>.broadcast();

StreamController<List<Stroke>> linesStreamController =
StreamController<List<Stroke>>.broadcast();

Uint8List? imageData;
bool _isPaintStarted = false;

set isPaintStarted(bool isStarted) {
setState(() {
_isPaintStarted = isStarted;
});
}

@override
void initState() {
canvas = Canvas(recorder);
super.initState();
}

Future<void> clear() async {
setState(() {
lines = [];
line = null;
isPaintStarted = false;
});
}

Expand All @@ -40,6 +62,7 @@ class _DrawingPageState extends State<DrawingPage> {
}

void onPanStart(DragStartDetails details) {
isPaintStarted = true;
final box = context.findRenderObject() as RenderBox;
final offset = box.globalToLocal(details.globalPosition);
final point = Point(offset.dx, offset.dy);
Expand All @@ -62,6 +85,59 @@ class _DrawingPageState extends State<DrawingPage> {
linesStreamController.add(lines);
}

Future<ui.Image?> toImage() async {
sketcher = Sketcher(lines: lines, options: options);
if (lines.isEmpty) {
return null;
}

double minX = double.infinity, minY = double.infinity;
double maxX = 0, maxY = 0;
for (int i = 0; i < lines.length; ++i) {
var line = lines[i];
for (Point point in line.path) {
if (point.x < minX) {
minX = point.x;
}
if (point.y < minY) {
minY = point.y;
}
if (point.x > maxX) {
maxX = point.x;
}
if (point.y > maxY) {
maxY = point.y;
}
}
}

final ui.PictureRecorder recorder = ui.PictureRecorder();
final ui.Canvas canvas = Canvas(recorder)
..translate(-(minX - options.size), -(minY - options.size));
final ui.Paint paint = Paint()..color = Colors.transparent;
canvas.drawPaint(paint);

sketcher?.paint(canvas, Size.infinite);
final ui.Picture picture = recorder.endRecording();
return picture.toImage(
(maxX - minX + options.size * 2).toInt(),
(maxY - minY + options.size * 2).toInt(),
);
}

saveSignature() async {
if (canvas != null) {
ui.Image? image = await toImage();
ByteData? byteData =
await image?.toByteData(format: ui.ImageByteFormat.png);
if (byteData != null) {
setState(() {
imageData = byteData.buffer.asUint8List();
});
}
}
}

Widget buildCurrentPath(BuildContext context) {
return GestureDetector(
onPanStart: onPanStart,
Expand Down Expand Up @@ -223,6 +299,24 @@ class _DrawingPageState extends State<DrawingPage> {
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 12),
),
buildClearButton(),
(_isPaintStarted)
? ElevatedButton(
onPressed: () async {
await saveSignature();

Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Scaffold(
body: Center(
child: Image.memory(imageData ??
Uint8List.fromList([])),
),
)));
clear();
},
child: const Text("Save"))
: Container(),
]));
}

Expand Down
3 changes: 2 additions & 1 deletion example/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import './drawing_page.dart';
import 'package:flutter/material.dart';

import './drawing_page.dart';

void main() {
runApp(const MyApp());
}
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ homepage: https://github.com/steveruizok/perfect-freehand-dart
repository: https://github.com/steveruizok/perfect-freehand-dart
documentation: https://github.com/steveruizok/perfect-freehand-dart
issue_tracker: https://github.com/steveruizok/perfect-freehand-dart/issues

environment:
sdk: ">=2.12.0 <3.0.0"

Expand Down