Milad akarie
Committed by David PHAM-VAN

Add RTL support to Wrap widget

@@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
16 16
17 import 'dart:math' as math; 17 import 'dart:math' as math;
18 18
  19 +import 'package:pdf/widgets.dart';
19 import 'package:vector_math/vector_math_64.dart'; 20 import 'package:vector_math/vector_math_64.dart';
20 21
21 import '../../pdf.dart'; 22 import '../../pdf.dart';
@@ -164,11 +165,14 @@ class Wrap extends MultiChildWidget with SpanningWidget { @@ -164,11 +165,14 @@ class Wrap extends MultiChildWidget with SpanningWidget {
164 double? mainAxisLimit = 0.0; 165 double? mainAxisLimit = 0.0;
165 var flipMainAxis = false; 166 var flipMainAxis = false;
166 var flipCrossAxis = false; 167 var flipCrossAxis = false;
167 - 168 + final textDirection = Directionality.of(context);
168 switch (direction) { 169 switch (direction) {
169 case Axis.horizontal: 170 case Axis.horizontal:
170 childConstraints = BoxConstraints(maxWidth: constraints.maxWidth); 171 childConstraints = BoxConstraints(maxWidth: constraints.maxWidth);
171 mainAxisLimit = constraints.maxWidth; 172 mainAxisLimit = constraints.maxWidth;
  173 + if (textDirection == TextDirection.rtl) {
  174 + flipMainAxis = true;
  175 + }
172 if (verticalDirection == VerticalDirection.down) { 176 if (verticalDirection == VerticalDirection.down) {
173 flipCrossAxis = true; 177 flipCrossAxis = true;
174 } 178 }
@@ -179,6 +183,9 @@ class Wrap extends MultiChildWidget with SpanningWidget { @@ -179,6 +183,9 @@ class Wrap extends MultiChildWidget with SpanningWidget {
179 if (verticalDirection == VerticalDirection.down) { 183 if (verticalDirection == VerticalDirection.down) {
180 flipMainAxis = true; 184 flipMainAxis = true;
181 } 185 }
  186 + if (textDirection == TextDirection.rtl) {
  187 + flipCrossAxis = true;
  188 + }
182 break; 189 break;
183 } 190 }
184 191
@@ -34,6 +34,17 @@ final _redBox = Container( @@ -34,6 +34,17 @@ final _redBox = Container(
34 color: PdfColors.red, 34 color: PdfColors.red,
35 ); 35 );
36 36
  37 +final _yellowBox = Container(
  38 + width: 50,
  39 + height: 50,
  40 + color: PdfColors.yellow,
  41 +);
  42 +
  43 +final _greenBox = Container(
  44 + width: 50,
  45 + height: 50,
  46 + color: PdfColors.green,
  47 +);
37 void main() { 48 void main() {
38 setUpAll(() { 49 setUpAll(() {
39 Document.debug = true; 50 Document.debug = true;
@@ -124,6 +135,75 @@ void main() { @@ -124,6 +135,75 @@ void main() {
124 ); 135 );
125 }); 136 });
126 137
  138 + test('Wrap Should render blue,red,yellow ordered RTL', () {
  139 + pdf.addPage(
  140 + Page(
  141 + textDirection: TextDirection.rtl,
  142 + pageFormat: const PdfPageFormat(150, 150),
  143 + build: (Context context) => SizedBox(
  144 + width: 150,
  145 + height: 150,
  146 + child: Wrap(
  147 + children: [_blueBox, _redBox,_yellowBox],
  148 + )
  149 + ),
  150 + ),
  151 + );
  152 + });
  153 +
  154 + test('Wrap Should render blue,red,yellow ordered LTR', () {
  155 + pdf.addPage(
  156 + Page(
  157 + textDirection: TextDirection.ltr,
  158 + pageFormat: const PdfPageFormat(150, 150),
  159 + build: (Context context) => SizedBox(
  160 + width: 150,
  161 + height: 150,
  162 + child: Wrap(
  163 + children: [_blueBox, _redBox,_yellowBox],
  164 + )
  165 + ),
  166 + ),
  167 + );
  168 + });
  169 + test('Wrap Should render blue,red,yellow ordered RTL aligned center', () {
  170 + pdf.addPage(
  171 + Page(
  172 + textDirection: TextDirection.rtl,
  173 + pageFormat: const PdfPageFormat(150, 150),
  174 + build: (Context context) => SizedBox(
  175 + width: 150,
  176 + height: 150,
  177 + child: Wrap(
  178 + spacing: 10,
  179 + runSpacing: 10,
  180 + runAlignment: WrapAlignment.center,
  181 + children: [_blueBox, _redBox,_yellowBox],
  182 + )
  183 + ),
  184 + ),
  185 + );
  186 + });
  187 +
  188 + test('Wrap Should render blue,red,yellow ordered RTL aligned bottom', () {
  189 + pdf.addPage(
  190 + Page(
  191 + textDirection: TextDirection.rtl,
  192 + pageFormat: const PdfPageFormat(150, 150),
  193 + build: (Context context) => SizedBox(
  194 + width: 150,
  195 + height: 150,
  196 + child: Wrap(
  197 + spacing: 10,
  198 + runSpacing: 10,
  199 + runAlignment: WrapAlignment.end,
  200 + children: [_blueBox, _redBox,_yellowBox],
  201 + )
  202 + ),
  203 + ),
  204 + );
  205 + });
  206 +
127 tearDownAll(() async { 207 tearDownAll(() async {
128 final file = File('rtl-layout.pdf'); 208 final file = File('rtl-layout.pdf');
129 await file.writeAsBytes(await pdf.save()); 209 await file.writeAsBytes(await pdf.save());