endStroke method
Implementation
Future<void> endStroke(int pageIndex) async {
final List<Offset>? points = _liveStrokes.remove(pageIndex);
notifyListeners();
if (points == null || points.length < 2) return;
attach();
final UPdfEdit? edit = _edit;
if (edit == null) return;
final UDocPageInfo info = viewer.pageInfo(pageIndex);
final List<Offset> converted = points.map((Offset point) => devicePointToPdf(info, point)).toList();
double left = converted.first.dx;
double right = converted.first.dx;
double top = converted.first.dy;
double bottom = converted.first.dy;
for (final Offset point in converted) {
left = min(left, point.dx);
right = max(right, point.dx);
top = min(top, point.dy);
bottom = max(bottom, point.dy);
}
final Rect bounds = Rect.fromLTRB(left, top, right, bottom).inflate(_strokeWidth + 2);
int number = -1;
switch (_tool) {
case UPdfTool.ink:
number = await edit.addAnnotation(
pageIndex,
UPdfAnnotationSpec(style: UPdfAnnotationStyle.ink, rect: bounds, inkPaths: <List<Offset>>[converted], color: _inkColor, borderColor: _inkColor, borderWidth: _strokeWidth, author: _author),
);
break;
case UPdfTool.rectangle:
number = await edit.addAnnotation(
pageIndex,
UPdfAnnotationSpec(style: UPdfAnnotationStyle.square, rect: bounds, color: const Color(0x00000000), borderColor: _inkColor, borderWidth: _strokeWidth, author: _author),
);
break;
case UPdfTool.ellipse:
number = await edit.addAnnotation(
pageIndex,
UPdfAnnotationSpec(style: UPdfAnnotationStyle.circle, rect: bounds, color: const Color(0x00000000), borderColor: _inkColor, borderWidth: _strokeWidth, author: _author),
);
break;
case UPdfTool.arrow:
number = await edit.addAnnotation(
pageIndex,
UPdfAnnotationSpec(
style: UPdfAnnotationStyle.arrow,
rect: Rect.fromPoints(converted.first, converted.last),
color: _inkColor,
borderColor: _inkColor,
borderWidth: _strokeWidth,
author: _author,
),
);
break;
case UPdfTool.redact:
number = await edit.addAnnotation(pageIndex, UPdfAnnotationSpec(style: UPdfAnnotationStyle.redact, rect: bounds, color: const Color(0xFF000000), author: _author));
break;
case UPdfTool.select:
case UPdfTool.highlight:
case UPdfTool.underline:
case UPdfTool.strikeOut:
case UPdfTool.eraser:
case UPdfTool.note:
case UPdfTool.textBox:
return;
}
if (number > 0) _undoStack.add(number);
viewer.invalidatePage(pageIndex);
notifyListeners();
}