Milad akarie
Committed by David PHAM-VAN

Add RTL support to Flex widget

... ... @@ -16,6 +16,7 @@
import 'dart:math' as math;
import 'package:pdf/widgets.dart';
import 'package:vector_math/vector_math_64.dart';
import '../../pdf.dart';
... ... @@ -396,10 +397,10 @@ class Flex extends MultiChildWidget with SpanningWidget {
final remainingSpace = math.max(0.0, actualSizeDelta);
double? leadingSpace;
late double betweenSpace;
final flipMainAxis = (verticalDirection == VerticalDirection.down &&
direction == Axis.vertical) ||
(verticalDirection == VerticalDirection.up &&
direction == Axis.horizontal);
final textDirection = Directionality.of(context) ;
final flipMainAxis = !(_startIsTopLeft(direction, textDirection, verticalDirection) ?? true);
switch (mainAxisAlignment) {
case MainAxisAlignment.start:
leadingSpace = 0.0;
... ... @@ -430,10 +431,6 @@ class Flex extends MultiChildWidget with SpanningWidget {
}
// Position elements
final flipCrossAxis = (verticalDirection == VerticalDirection.down &&
direction == Axis.horizontal) ||
(verticalDirection == VerticalDirection.up &&
direction == Axis.vertical);
var childMainPosition =
flipMainAxis ? actualSize - leadingSpace : leadingSpace;
... ... @@ -442,12 +439,11 @@ class Flex extends MultiChildWidget with SpanningWidget {
double? childCrossPosition;
switch (crossAxisAlignment) {
case CrossAxisAlignment.start:
childCrossPosition =
flipCrossAxis ? crossSize - _getCrossSize(child) : 0.0;
break;
case CrossAxisAlignment.end:
childCrossPosition =
!flipCrossAxis ? crossSize - _getCrossSize(child) : 0.0;
childCrossPosition = _startIsTopLeft(flipAxis(direction), textDirection, verticalDirection)
== (crossAxisAlignment == CrossAxisAlignment.start)
? 0.0
: crossSize - _getCrossSize(child);
break;
case CrossAxisAlignment.center:
childCrossPosition = crossSize / 2.0 - _getCrossSize(child) / 2.0;
... ... @@ -478,6 +474,39 @@ class Flex extends MultiChildWidget with SpanningWidget {
}
}
Axis flipAxis(Axis direction) {
switch (direction) {
case Axis.horizontal:
return Axis.vertical;
case Axis.vertical:
return Axis.horizontal;
}
}
bool? _startIsTopLeft(Axis direction, TextDirection? textDirection, VerticalDirection? verticalDirection) {
// If the relevant value of textDirection or verticalDirection is null, this returns null too.
switch (direction) {
case Axis.horizontal:
switch (textDirection) {
case TextDirection.ltr:
return true;
case TextDirection.rtl:
return false;
case null:
return null;
}
case Axis.vertical:
switch (verticalDirection) {
case VerticalDirection.down:
return false;
case VerticalDirection.up:
return true;
case null:
return null;
}
}
}
@override
void paint(Context context) {
super.paint(context);
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:io';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:test/test.dart';
late Document pdf;
final _blueBox = Container(
width: 50,
height: 50,
color: PdfColors.blue,
);
final _redBox = Container(
width: 50,
height: 50,
color: PdfColors.red,
);
void main() {
setUpAll(() {
Document.debug = true;
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) => Row(
children: [_blueBox, _redBox],
),
),
);
});
test('Should render a blue box followed by a red box ordered RTL with aligned center', () {
pdf.addPage(
Page(
textDirection: TextDirection.rtl,
pageFormat: const PdfPageFormat(150, 50),
build: (Context context) => Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [_blueBox, _redBox],
),
),
);
});
test('Should render a blue box followed by a red box ordered RTL with CrossAxisAlignment.end aligned right', () {
pdf.addPage(
Page(
pageFormat: const PdfPageFormat(150, 100),
textDirection: TextDirection.rtl,
build: (Context context) => SizedBox(
width: 150,
height: 100,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [_blueBox, _redBox],
),
),
),
);
});
test('Should render a blue box followed by a red box ordered LTR aligned left', () {
pdf.addPage(
Page(
pageFormat: const PdfPageFormat(150, 50),
build: (Context context) => Row(
children: [_blueBox, _redBox],
),
),
);
});
test('Should render a blue box followed by a red box ordered TTB aligned right', () {
pdf.addPage(
Page(
textDirection: TextDirection.rtl,
pageFormat: const PdfPageFormat(150, 150),
build: (Context context) => SizedBox(
width: 150,
height: 150,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [_blueBox, _redBox],
),
),
),
);
});
test('Should render a blue box followed by a red box ordered TTB aligned left', () {
pdf.addPage(
Page(
textDirection: TextDirection.ltr,
pageFormat: const PdfPageFormat(150, 150),
build: (Context context) => SizedBox(
width: 150,
height: 150,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [_blueBox, _redBox],
),
),
),
);
});
tearDownAll(() async {
final file = File('rtl-layout.pdf');
await file.writeAsBytes(await pdf.save());
});
}
... ...