Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
dart_pdf
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Milad akarie
2023-07-03 16:38:53 +0300
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Committed by
David PHAM-VAN
2023-07-24 08:39:22 -0300
Commit
4f6ec254753166af53db62ecf193f9faa04761b8
4f6ec254
1 parent
81ba6e8b
support stack's Positioned directional
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
150 additions
and
53 deletions
pdf/lib/src/widgets/basic.dart
pdf/lib/src/widgets/stack.dart
pdf/test/rtl_layout_test.dart
pdf/lib/src/widgets/basic.dart
View file @
4f6ec25
...
...
@@ -74,32 +74,31 @@ class Padding extends SingleChildWidget {
@override
void
layout
(
Context
context
,
BoxConstraints
constraints
,
{
bool
parentUsesSize
=
false
})
{
final
effective
Padding
=
padding
.
resolve
(
Directionality
.
of
(
context
));
final
resolved
Padding
=
padding
.
resolve
(
Directionality
.
of
(
context
));
if
(
child
!=
null
)
{
final
childConstraints
=
constraints
.
deflate
(
effective
Padding
);
final
childConstraints
=
constraints
.
deflate
(
resolved
Padding
);
child
!.
layout
(
context
,
childConstraints
,
parentUsesSize:
parentUsesSize
);
assert
(
child
!.
box
!=
null
);
box
=
constraints
.
constrainRect
(
width:
child
!.
box
!.
width
+
effectivePadding
.
horizontal
,
height:
child
!.
box
!.
height
+
effectivePadding
.
vertical
);
width:
child
!.
box
!.
width
+
resolvedPadding
.
horizontal
,
height:
child
!.
box
!.
height
+
resolvedPadding
.
vertical
);
}
else
{
box
=
constraints
.
constrainRect
(
width:
effectivePadding
.
horizontal
,
height:
effective
Padding
.
vertical
);
box
=
constraints
.
constrainRect
(
width:
resolvedPadding
.
horizontal
,
height:
resolved
Padding
.
vertical
);
}
}
@override
void
debugPaint
(
Context
context
)
{
final
effective
Padding
=
padding
.
resolve
(
Directionality
.
of
(
context
));
final
resolved
Padding
=
padding
.
resolve
(
Directionality
.
of
(
context
));
context
.
canvas
..
setFillColor
(
PdfColors
.
lime
)
..
moveTo
(
box
!.
x
,
box
!.
y
)
..
lineTo
(
box
!.
right
,
box
!.
y
)
..
lineTo
(
box
!.
right
,
box
!.
top
)
..
lineTo
(
box
!.
x
,
box
!.
top
)
..
moveTo
(
box
!.
x
+
effectivePadding
.
left
,
box
!.
y
+
effectivePadding
.
bottom
)
..
lineTo
(
box
!.
x
+
effectivePadding
.
left
,
box
!.
top
-
effectivePadding
.
top
)
..
lineTo
(
box
!.
right
-
effectivePadding
.
right
,
box
!.
top
-
effectivePadding
.
top
)
..
lineTo
(
box
!.
right
-
effectivePadding
.
right
,
box
!.
y
+
effectivePadding
.
bottom
)
..
moveTo
(
box
!.
x
+
resolvedPadding
.
left
,
box
!.
y
+
resolvedPadding
.
bottom
)
..
lineTo
(
box
!.
x
+
resolvedPadding
.
left
,
box
!.
top
-
resolvedPadding
.
top
)
..
lineTo
(
box
!.
right
-
resolvedPadding
.
right
,
box
!.
top
-
resolvedPadding
.
top
)
..
lineTo
(
box
!.
right
-
resolvedPadding
.
right
,
box
!.
y
+
resolvedPadding
.
bottom
)
..
fillPath
();
}
...
...
pdf/lib/src/widgets/stack.dart
View file @
4f6ec25
...
...
@@ -19,9 +19,7 @@ import 'dart:math' as math;
import
'package:vector_math/vector_math_64.dart'
;
import
'../../pdf.dart'
;
import
'geometry.dart'
;
import
'text.dart'
;
import
'widget.dart'
;
import
'../../widgets.dart'
;
/// How to size the non-positioned children of a [Stack].
enum
StackFit
{
loose
,
expand
,
passthrough
}
...
...
@@ -33,22 +31,26 @@ enum Overflow { visible, clip }
/// A widget that controls where a child of a [Stack] is positioned.
class
Positioned
extends
SingleChildWidget
{
Positioned
({
this
.
left
,
double
?
left
,
this
.
top
,
this
.
right
,
double
?
right
,
this
.
bottom
,
required
Widget
child
,
})
:
super
(
child:
child
);
})
:
_left
=
left
,
_right
=
right
,
super
(
child:
child
);
/// Creates a Positioned object with left, top, right, and bottom set to 0.0
/// unless a value for them is passed.
Positioned
.
fill
({
this
.
left
=
0.0
,
double
?
left
=
0.0
,
this
.
top
=
0.0
,
this
.
right
=
0.0
,
double
?
right
=
0.0
,
this
.
bottom
=
0.0
,
required
Widget
child
,
})
:
super
(
child:
child
);
})
:
_left
=
left
,
_right
=
right
,
super
(
child:
child
);
/// Creates a widget that controls where a child of a [Stack] is positioned.
factory
Positioned
.
directional
({
...
...
@@ -80,11 +82,14 @@ class Positioned extends SingleChildWidget {
);
}
final
double
?
left
;
double
?
get
left
=>
_
left
;
final
double
?
top
;
double
?
get
right
=>
_right
;
final
double
?
_left
;
final
double
?
_right
;
final
double
?
right
;
final
double
?
top
;
final
double
?
bottom
;
...
...
@@ -99,6 +104,63 @@ class Positioned extends SingleChildWidget {
}
}
/// A widget that controls where a child of a [Stack] is positioned without
/// committing to a specific [TextDirection].
class
PositionedDirectional
extends
Positioned
{
PositionedDirectional
({
this
.
start
,
this
.
end
,
double
?
top
,
double
?
bottom
,
required
Widget
child
,
})
:
super
(
child:
child
,
top:
top
,
bottom:
bottom
,
);
PositionedDirectional
.
fill
({
this
.
start
=
0.0
,
this
.
end
=
0.0
,
double
?
top
=
0.0
,
double
?
bottom
=
0.0
,
required
Widget
child
,
})
:
super
(
child:
child
,
top:
top
,
bottom:
bottom
,
);
final
double
?
start
;
double
?
_resolvedLeft
;
double
?
_resolvedRight
;
@override
double
?
get
left
=>
_resolvedLeft
;
@override
double
?
get
right
=>
_resolvedRight
;
final
double
?
end
;
@override
void
layout
(
Context
context
,
BoxConstraints
constraints
,
{
bool
parentUsesSize
=
false
})
{
super
.
layout
(
context
,
constraints
,
parentUsesSize:
parentUsesSize
);
switch
(
Directionality
.
of
(
context
))
{
case
TextDirection
.
rtl
:
_resolvedLeft
=
end
;
_resolvedRight
=
start
;
break
;
case
TextDirection
.
ltr
:
_resolvedLeft
=
start
;
_resolvedRight
=
end
;
break
;
}
}
}
/// A widget that positions its children relative to the edges of its box.
class
Stack
extends
MultiChildWidget
{
Stack
({
...
...
@@ -110,7 +172,7 @@ class Stack extends MultiChildWidget {
/// How to align the non-positioned and partially-positioned children in the
/// stack.
final
Alignment
alignment
;
final
Alignment
Geometry
alignment
;
/// How to size the non-positioned children in the stack.
final
StackFit
fit
;
...
...
@@ -119,8 +181,7 @@ class Stack extends MultiChildWidget {
final
Overflow
overflow
;
@override
void
layout
(
Context
context
,
BoxConstraints
constraints
,
{
bool
parentUsesSize
=
false
})
{
void
layout
(
Context
context
,
BoxConstraints
constraints
,
{
bool
parentUsesSize
=
false
})
{
final
childCount
=
children
.
length
;
var
hasNonPositionedChildren
=
false
;
...
...
@@ -150,7 +211,6 @@ class Stack extends MultiChildWidget {
for
(
final
child
in
children
)
{
if
(
child
is
!
Positioned
)
{
hasNonPositionedChildren
=
true
;
child
.
layout
(
context
,
nonPositionedConstraints
,
parentUsesSize:
true
);
assert
(
child
.
box
!=
null
);
...
...
@@ -167,28 +227,25 @@ class Stack extends MultiChildWidget {
}
else
{
box
=
PdfRect
.
fromPoints
(
PdfPoint
.
zero
,
constraints
.
biggest
);
}
final
resolvedAlignment
=
alignment
.
resolve
(
Directionality
.
of
(
context
));
for
(
final
child
in
children
)
{
if
(
child
is
!
Positioned
)
{
child
.
box
=
PdfRect
.
fromPoints
(
alignment
.
inscribe
(
child
.
box
!.
size
,
box
!).
offset
,
child
.
box
!.
size
);
child
.
box
=
PdfRect
.
fromPoints
(
resolvedAlignment
.
inscribe
(
child
.
box
!.
size
,
box
!).
offset
,
child
.
box
!.
size
);
}
else
{
final
positioned
=
child
;
var
childConstraints
=
const
BoxConstraints
();
if
(
positioned
.
left
!=
null
&&
positioned
.
right
!=
null
)
{
childConstraints
=
childConstraints
.
tighten
(
width:
box
!.
width
-
positioned
.
right
!
-
positioned
.
left
!);
childConstraints
=
childConstraints
.
tighten
(
width:
box
!.
width
-
positioned
.
right
!
-
positioned
.
left
!);
}
else
if
(
positioned
.
width
!=
null
)
{
childConstraints
=
childConstraints
.
tighten
(
width:
positioned
.
width
);
}
if
(
positioned
.
top
!=
null
&&
positioned
.
bottom
!=
null
)
{
childConstraints
=
childConstraints
.
tighten
(
height:
box
!.
height
-
positioned
.
bottom
!
-
positioned
.
top
!);
childConstraints
=
childConstraints
.
tighten
(
height:
box
!.
height
-
positioned
.
bottom
!
-
positioned
.
top
!);
}
else
if
(
positioned
.
height
!=
null
)
{
childConstraints
=
childConstraints
.
tighten
(
height:
positioned
.
height
);
childConstraints
=
childConstraints
.
tighten
(
height:
positioned
.
height
);
}
positioned
.
layout
(
context
,
childConstraints
,
parentUsesSize:
true
);
...
...
@@ -200,7 +257,7 @@ class Stack extends MultiChildWidget {
}
else
if
(
positioned
.
right
!=
null
)
{
x
=
box
!.
width
-
positioned
.
right
!
-
positioned
.
width
!;
}
else
{
x
=
a
lignment
.
inscribe
(
positioned
.
box
!.
size
,
box
!).
x
;
x
=
resolvedA
lignment
.
inscribe
(
positioned
.
box
!.
size
,
box
!).
x
;
}
double
?
y
;
...
...
@@ -209,11 +266,10 @@ class Stack extends MultiChildWidget {
}
else
if
(
positioned
.
top
!=
null
)
{
y
=
box
!.
height
-
positioned
.
top
!
-
positioned
.
height
!;
}
else
{
y
=
a
lignment
.
inscribe
(
positioned
.
box
!.
size
,
box
!).
y
;
y
=
resolvedA
lignment
.
inscribe
(
positioned
.
box
!.
size
,
box
!).
y
;
}
positioned
.
box
=
PdfRect
.
fromPoints
(
PdfPoint
(
x
!,
y
!),
positioned
.
box
!.
size
);
positioned
.
box
=
PdfRect
.
fromPoints
(
PdfPoint
(
x
!,
y
!),
positioned
.
box
!.
size
);
}
}
}
...
...
pdf/test/rtl_layout_test.dart
View file @
4f6ec25
...
...
@@ -46,20 +46,7 @@ void main() {
pdf
=
Document
();
});
test
(
'Should render a blue box followed by a red box ordered RTL aligned right'
,
()
{
pdf
.
addPage
(
Page
(
textDirection:
TextDirection
.
rtl
,
pageFormat:
const
PdfPageFormat
(
150
,
50
),
build:
(
Context
context
)
=>
TestAnnotation
(
anno:
'RTL Row'
,
child:
Row
(
children:
[
_blueBox
,
_redBox
],
),
),
),
);
});
test
(
'RTL Text'
,
()
{
pdf
.
addPage
(
...
...
@@ -147,6 +134,21 @@ void main() {
);
});
test
(
'Should render a blue box followed by a red box ordered RTL aligned right'
,
()
{
pdf
.
addPage
(
Page
(
textDirection:
TextDirection
.
rtl
,
pageFormat:
const
PdfPageFormat
(
150
,
50
),
build:
(
Context
context
)
=>
TestAnnotation
(
anno:
'RTL Row'
,
child:
Row
(
children:
[
_blueBox
,
_redBox
],
),
),
),
);
});
test
(
'Should render a blue box followed by a red box ordered RTL with aligned center'
,
()
{
pdf
.
addPage
(
Page
(
...
...
@@ -651,6 +653,46 @@ void main() {
);
});
test
(
'RTL Stack, should directional child to right44'
,
()
{
pdf
.
addPage
(
Page
(
textDirection:
TextDirection
.
rtl
,
pageFormat:
const
PdfPageFormat
(
150
,
150
),
build:
(
Context
context
)
{
return
TestAnnotation
(
anno:
'RTL Stack PositionDirectional.start'
,
child:
Stack
(
children:
[
PositionedDirectional
(
start:
0
,
child:
_blueBox
,
)
]),
);
},
),
);
});
test
(
'LTR Stack, should directional child to right44'
,
()
{
pdf
.
addPage
(
Page
(
textDirection:
TextDirection
.
ltr
,
pageFormat:
const
PdfPageFormat
(
150
,
150
),
build:
(
Context
context
)
{
return
TestAnnotation
(
anno:
'LTR Stack PositionDirectional.start'
,
child:
Stack
(
children:
[
PositionedDirectional
(
start:
0
,
child:
_blueBox
,
)
]),
);
},
),
);
});
tearDownAll
(()
async
{
final
file
=
File
(
'rtl-layout.pdf'
);
await
file
.
writeAsBytes
(
await
pdf
.
save
());
...
...
Please
register
or
login
to post a comment