David PHAM-VAN

Add Directionality Widget

1 # Changelog 1 # Changelog
2 2
3 -## 1.10.2 3 +## 1.11.0
4 4
5 - Fix mixing Arabic with English [Anas Altair] 5 - Fix mixing Arabic with English [Anas Altair]
6 - Support Dagger alif in Arabic [Anas Altair] 6 - Support Dagger alif in Arabic [Anas Altair]
7 - Support ARABIC TATWEEL [Anas Altair] 7 - Support ARABIC TATWEEL [Anas Altair]
8 - Update Arabic tests [Anas Altair] 8 - Update Arabic tests [Anas Altair]
  9 +- Add Directionality Widget
9 10
10 ## 1.10.1 11 ## 1.10.1
11 12
@@ -445,14 +445,13 @@ class RichText extends Widget { @@ -445,14 +445,13 @@ class RichText extends Widget {
445 RichText( 445 RichText(
446 {@required this.text, 446 {@required this.text,
447 TextAlign textAlign, 447 TextAlign textAlign,
448 - TextDirection textDirection, 448 + this.textDirection,
449 bool softWrap, 449 bool softWrap,
450 this.tightBounds = false, 450 this.tightBounds = false,
451 this.textScaleFactor = 1.0, 451 this.textScaleFactor = 1.0,
452 int maxLines}) 452 int maxLines})
453 : assert(text != null), 453 : assert(text != null),
454 _textAlign = textAlign, 454 _textAlign = textAlign,
455 - textDirection = textDirection ?? TextDirection.ltr,  
456 _softWrap = softWrap, 455 _softWrap = softWrap,
457 _maxLines = maxLines; 456 _maxLines = maxLines;
458 457
@@ -486,6 +485,7 @@ class RichText extends Widget { @@ -486,6 +485,7 @@ class RichText extends Widget {
486 double wordsWidth, 485 double wordsWidth,
487 bool last, 486 bool last,
488 double baseline, 487 double baseline,
  488 + TextDirection textDirection,
489 ) { 489 ) {
490 double delta = 0; 490 double delta = 0;
491 switch (textAlign) { 491 switch (textAlign) {
@@ -553,6 +553,8 @@ class RichText extends Widget { @@ -553,6 +553,8 @@ class RichText extends Widget {
553 _softWrap ??= theme.softWrap; 553 _softWrap ??= theme.softWrap;
554 _maxLines ??= theme.maxLines; 554 _maxLines ??= theme.maxLines;
555 _textAlign ??= theme.textAlign; 555 _textAlign ??= theme.textAlign;
  556 + final TextDirection _textDirection =
  557 + textDirection ?? Directionality.of(context);
556 558
557 final double constraintWidth = constraints.hasBoundedWidth 559 final double constraintWidth = constraints.hasBoundedWidth
558 ? constraints.maxWidth 560 ? constraints.maxWidth
@@ -583,7 +585,7 @@ class RichText extends Widget { @@ -583,7 +585,7 @@ class RichText extends Widget {
583 final PdfFontMetrics space = 585 final PdfFontMetrics space =
584 font.stringMetrics(' ') * (style.fontSize * textScaleFactor); 586 font.stringMetrics(' ') * (style.fontSize * textScaleFactor);
585 587
586 - final List<String> spanLines = (textDirection == TextDirection.rtl 588 + final List<String> spanLines = (_textDirection == TextDirection.rtl
587 ? PdfArabic.convert(span.text) 589 ? PdfArabic.convert(span.text)
588 : span.text) 590 : span.text)
589 .split('\n'); 591 .split('\n');
@@ -613,6 +615,7 @@ class RichText extends Widget { @@ -613,6 +615,7 @@ class RichText extends Widget {
613 style.letterSpacing, 615 style.letterSpacing,
614 false, 616 false,
615 bottom, 617 bottom,
  618 + _textDirection,
616 )); 619 ));
617 620
618 spanStart += spanCount; 621 spanStart += spanCount;
@@ -676,6 +679,7 @@ class RichText extends Widget { @@ -676,6 +679,7 @@ class RichText extends Widget {
676 style.letterSpacing, 679 style.letterSpacing,
677 true, 680 true,
678 bottom, 681 bottom,
  682 + _textDirection,
679 )); 683 ));
680 684
681 spanStart += spanCount; 685 spanStart += spanCount;
@@ -725,6 +729,7 @@ class RichText extends Widget { @@ -725,6 +729,7 @@ class RichText extends Widget {
725 offsetX, 729 offsetX,
726 false, 730 false,
727 bottom, 731 bottom,
  732 + _textDirection,
728 )); 733 ));
729 734
730 spanStart += spanCount; 735 spanStart += spanCount;
@@ -782,6 +787,7 @@ class RichText extends Widget { @@ -782,6 +787,7 @@ class RichText extends Widget {
782 offsetX, 787 offsetX,
783 true, 788 true,
784 bottom, 789 bottom,
  790 + _textDirection,
785 )); 791 ));
786 792
787 bottom ??= 0.0; 793 bottom ??= 0.0;
@@ -380,3 +380,45 @@ class TextStyle { @@ -380,3 +380,45 @@ class TextStyle {
380 String toString() => 380 String toString() =>
381 'TextStyle(color:$color font:$font size:$fontSize weight:$fontWeight style:$fontStyle letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background decoration:$decoration decorationColor:$decorationColor decorationStyle:$decorationStyle decorationThickness:$decorationThickness, renderingMode:$renderingMode)'; 381 'TextStyle(color:$color font:$font size:$fontSize weight:$fontWeight style:$fontStyle letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background decoration:$decoration decorationColor:$decorationColor decorationStyle:$decorationStyle decorationThickness:$decorationThickness, renderingMode:$renderingMode)';
382 } 382 }
  383 +
  384 +class Directionality extends StatelessWidget implements Inherited {
  385 + /// Creates a widget that determines the directionality of text and
  386 + /// text-direction-sensitive render objects.
  387 + ///
  388 + /// The [textDirection] and [child] arguments must not be null.
  389 + Directionality({
  390 + @required this.textDirection,
  391 + @required this.child,
  392 + }) : assert(textDirection != null),
  393 + assert(child != null);
  394 +
  395 + /// The subtree
  396 + final Widget child;
  397 +
  398 + /// The text direction for this subtree.
  399 + final TextDirection textDirection;
  400 +
  401 + /// The text direction from the closest instance of this class that encloses
  402 + /// the given context.
  403 + ///
  404 + /// If there is no [Directionality] ancestor widget in the tree at the given
  405 + /// context, then this will return TextDirection.ltr.
  406 + ///
  407 + /// Typical usage is as follows:
  408 + ///
  409 + /// ```dart
  410 + /// TextDirection textDirection = Directionality.of(context);
  411 + /// ```
  412 + static TextDirection of(Context context) {
  413 + final Directionality widget = context.inherited[Directionality];
  414 + return widget?.textDirection ?? TextDirection.ltr;
  415 + }
  416 +
  417 + @override
  418 + Widget build(Context context) {
  419 + return InheritedWidget(
  420 + build: (context) => child,
  421 + inherited: this,
  422 + );
  423 + }
  424 +}
@@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf 4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
5 repository: https://github.com/DavBfr/dart_pdf 5 repository: https://github.com/DavBfr/dart_pdf
6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
7 -version: 1.10.2 7 +version: 1.11.0
8 8
9 environment: 9 environment:
10 sdk: ">=2.3.0 <3.0.0" 10 sdk: ">=2.3.0 <3.0.0"
@@ -24,6 +24,8 @@ import 'package:test/test.dart'; @@ -24,6 +24,8 @@ import 'package:test/test.dart';
24 import 'package:pdf/pdf.dart'; 24 import 'package:pdf/pdf.dart';
25 import 'package:pdf/widgets.dart'; 25 import 'package:pdf/widgets.dart';
26 26
  27 +import 'utils.dart';
  28 +
27 Document pdf; 29 Document pdf;
28 30
29 List<TableRow> buildTable( 31 List<TableRow> buildTable(
@@ -232,6 +234,25 @@ void main() { @@ -232,6 +234,25 @@ void main() {
232 )); 234 ));
233 }); 235 });
234 236
  237 + test('Table fromTextArray with directionality', () {
  238 + pdf.addPage(Page(
  239 + theme: ThemeData.withFont(
  240 + base: loadFont('hacen-tunisia.ttf'),
  241 + ),
  242 + build: (Context context) => Directionality(
  243 + textDirection: TextDirection.rtl,
  244 + child: Table.fromTextArray(
  245 + headers: <dynamic>['ثلاثة', 'اثنان', 'واحد'],
  246 + cellAlignment: Alignment.centerRight,
  247 + data: <List<dynamic>>[
  248 + <dynamic>['الكلب', 'قط', 'ذئب'],
  249 + <dynamic>['فأر', 'بقرة', 'طائر'],
  250 + ],
  251 + ),
  252 + ),
  253 + ));
  254 + });
  255 +
235 tearDownAll(() { 256 tearDownAll(() {
236 final File file = File('widgets-table.pdf'); 257 final File file = File('widgets-table.pdf');
237 file.writeAsBytesSync(pdf.save()); 258 file.writeAsBytesSync(pdf.save());