Skip to content

Commit

Permalink
Fix: Widget args cast types error (#77)
Browse files Browse the repository at this point in the history
* πŸš€ feat: exportUnbuiltWidget

* πŸ› fix: throw errors in Align convert

* πŸ› fix: new extensions and convertions

* πŸ› fix: alignDirectional

* πŸ› fix: container edge cast errors

* 🎨 code: improve structure and debug prints

* ♻️ refactor: better import impl

Co-authored-by: Joseph Grabinger <[email protected]>

* ♻️ refactor: more reusable impl of .toPdfAligment

Co-authored-by: Joseph Grabinger <[email protected]>

* πŸš€ feat: new impl of aligmentGeometryConverter

* πŸ”₯ remove: edge_insets_directional.dart

* πŸš€ feat: better impl to edge_insets.dart

---------

Co-authored-by: sousa-p <[email protected]>
Co-authored-by: Joseph Grabinger <[email protected]>
  • Loading branch information
3 people authored Jul 13, 2024
1 parent cb11d2f commit a8817eb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 16 deletions.
17 changes: 16 additions & 1 deletion lib/args/alignment.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import 'package:flutter/widgets.dart' show Alignment, AlignmentDirectional;
import 'package:flutter/widgets.dart'
show Alignment, AlignmentDirectional, AlignmentGeometry, debugPrint;

import 'package:pdf/widgets.dart' as pw show Alignment;

extension AlignmentGeometryConverter on AlignmentGeometry {
pw.Alignment? toPdfAlignment() {
switch (runtimeType) {
case const (Alignment):
return (this as Alignment).toPdfAlignment();
case const (AlignmentDirectional):
return (this as AlignmentDirectional).toPdfAlignment();
default:
debugPrint('Unsupported AlignmentGeometry: $this; defaulting to null');
return null;
}
}
}

extension AlignmentConverter on Alignment {
pw.Alignment toPdfAlignment() => pw.Alignment(x, y);
}
Expand Down
26 changes: 25 additions & 1 deletion lib/args/edge_insets.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import 'package:flutter/widgets.dart' show EdgeInsets;
import 'package:flutter/widgets.dart'
show EdgeInsets, EdgeInsetsDirectional, EdgeInsetsGeometry, debugPrint;

import 'package:pdf/widgets.dart' as pw show EdgeInsets;

extension EdgeInsetsGeometryConverter on EdgeInsetsGeometry {
pw.EdgeInsets? toPdfEdgeInsets() {
switch (runtimeType) {
case const (EdgeInsets):
return (this as EdgeInsets).toPdfEdgeInsets();
case const (EdgeInsetsDirectional):
return (this as EdgeInsetsDirectional).toPdfEdgeInsetsDirectional();
default:
debugPrint('Unsupported EdgeInsetsGeometry: $this; defaulting to null');
return null;
}
}
}

extension EdgeInsetsConverter on EdgeInsets {
pw.EdgeInsets toPdfEdgeInsets() => pw.EdgeInsets.only(
left: left,
Expand All @@ -10,3 +25,12 @@ extension EdgeInsetsConverter on EdgeInsets {
bottom: bottom,
);
}

extension EdgeInsetsDirectionalConverter on EdgeInsetsDirectional {
pw.EdgeInsets toPdfEdgeInsetsDirectional() => pw.EdgeInsets.only(
left: start,
top: top,
right: end,
bottom: bottom,
);
}
5 changes: 5 additions & 0 deletions lib/export_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,9 @@ class ExportDelegate {

return pdf;
}

// Wrap the private method _exportWidget
// Exports unbuilted [widget].
Future<pw.Widget> exportUnbuiltWidget(Widget widget) async =>
await _exportWidget(widget, null);
}
9 changes: 4 additions & 5 deletions lib/widgets/align.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import 'package:flutter/widgets.dart' show Align, Alignment;
import 'package:flutter/widgets.dart' show Align;
import 'package:flutter_to_pdf/args/alignment.dart';

import 'package:pdf/widgets.dart' as pw show Align, Widget;

import '/args/alignment.dart';
import 'package:pdf/widgets.dart' as pw show Align, Widget, Alignment;

/// Extension on [Align] to convert it to the pdf equivalent [pw.Align].
extension AlignConverter on Align {
/// Converts the [Align] to a [pw.Align].
pw.Align toPdfWidget(pw.Widget? child) => pw.Align(
alignment: (alignment as Alignment).toPdfAlignment(),
alignment: alignment.toPdfAlignment() ?? pw.Alignment.center,
widthFactor: widthFactor,
heightFactor: heightFactor,
child: child,
Expand Down
11 changes: 5 additions & 6 deletions lib/widgets/container.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'package:flutter/widgets.dart'
show Alignment, BoxDecoration, Container, EdgeInsets;
import 'package:flutter/widgets.dart' show BoxDecoration, Container;

import 'package:flutter_to_pdf/args/alignment.dart';
import 'package:pdf/widgets.dart' as pw show Container, Widget;

import '../args/box_decoration.dart';
import '../args/alignment.dart';
import '../args/color.dart';
import '../args/box_constraints.dart';
import '../args/edge_insets.dart';
Expand All @@ -13,14 +12,14 @@ import '../args/edge_insets.dart';
extension ContainerConverter on Container {
/// Converts the [Container] to a [pw.Container].
Future<pw.Container> toPdfWidget(pw.Widget? child) async => pw.Container(
alignment: (alignment as Alignment?)?.toPdfAlignment(),
alignment: alignment?.toPdfAlignment(),
decoration: await (decoration as BoxDecoration?)?.toPdfBoxDecoration(),
color: color?.toPdfColor(),
constraints: constraints?.toPdfBoxConstraints(),
foregroundDecoration: await (foregroundDecoration as BoxDecoration?)
?.toPdfBoxDecoration(),
margin: (margin as EdgeInsets?)?.toPdfEdgeInsets(),
padding: (padding as EdgeInsets?)?.toPdfEdgeInsets(),
margin: margin?.toPdfEdgeInsets(),
padding: padding?.toPdfEdgeInsets(),
transform: transform,
child: child,
);
Expand Down
6 changes: 3 additions & 3 deletions lib/widgets/padding.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'package:flutter/widgets.dart' show EdgeInsets, Padding;
import 'package:flutter/widgets.dart' show Padding;

import 'package:pdf/widgets.dart' as pw show Padding, Widget;
import 'package:pdf/widgets.dart' as pw show Padding, Widget, EdgeInsets;

import '/args/edge_insets.dart';

/// Extension on [Padding] to convert it to the pdf equivalent [pw.Padding].
extension PaddingConverter on Padding {
/// Converts the [Padding] to a [pw.Padding].
pw.Padding toPdfWidget(pw.Widget? child) => pw.Padding(
padding: (padding as EdgeInsets).toPdfEdgeInsets(),
padding: padding.toPdfEdgeInsets() ?? const pw.EdgeInsets.all(0),
child: child,
);
}

0 comments on commit a8817eb

Please sign in to comment.