David PHAM-VAN

Implement properly RichText.softWrap

@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 * Add SizedBox Widget 5 * Add SizedBox Widget
6 * Fix RichText Widget word spacing 6 * Fix RichText Widget word spacing
7 * Improve Theme and TextStyle 7 * Improve Theme and TextStyle
  8 +* Implement properly RichText.softWrap
8 9
9 # 1.3.7 10 # 1.3.7
10 * Add Pdf Creation date 11 * Add Pdf Creation date
@@ -98,11 +98,10 @@ class RichText extends Widget { @@ -98,11 +98,10 @@ class RichText extends Widget {
98 RichText( 98 RichText(
99 {@required this.text, 99 {@required this.text,
100 this.textAlign = TextAlign.left, 100 this.textAlign = TextAlign.left,
101 - bool softWrap = true, 101 + this.softWrap = true,
102 this.textScaleFactor = 1.0, 102 this.textScaleFactor = 1.0,
103 - int maxLines})  
104 - : maxLines = !softWrap ? 1 : maxLines,  
105 - assert(text != null); 103 + this.maxLines})
  104 + : assert(text != null);
106 105
107 static const bool debug = false; 106 static const bool debug = false;
108 107
@@ -112,6 +111,8 @@ class RichText extends Widget { @@ -112,6 +111,8 @@ class RichText extends Widget {
112 111
113 final double textScaleFactor; 112 final double textScaleFactor;
114 113
  114 + final bool softWrap;
  115 +
115 final int maxLines; 116 final int maxLines;
116 117
117 final List<_Word> _words = <_Word>[]; 118 final List<_Word> _words = <_Word>[];
@@ -183,7 +184,9 @@ class RichText extends Widget { @@ -183,7 +184,9 @@ class RichText extends Widget {
183 final PdfFontMetrics space = 184 final PdfFontMetrics space =
184 font.stringMetrics(' ') * (style.fontSize * textScaleFactor); 185 font.stringMetrics(' ') * (style.fontSize * textScaleFactor);
185 186
186 - for (String word in span.text.split(RegExp(r'\s'))) { 187 + final List<String> spanLines = span.text.split('\n');
  188 + for (int line = 0; line < spanLines.length; line++) {
  189 + for (String word in spanLines[line].split(RegExp(r'\s'))) {
187 if (word.isEmpty) { 190 if (word.isEmpty) {
188 offsetX += space.advanceWidth * style.wordSpacing; 191 offsetX += space.advanceWidth * style.wordSpacing;
189 continue; 192 continue;
@@ -201,7 +204,9 @@ class RichText extends Widget { @@ -201,7 +204,9 @@ class RichText extends Widget {
201 offsetX - space.advanceWidth * style.wordSpacing, 204 offsetX - space.advanceWidth * style.wordSpacing,
202 false, 205 false,
203 bottom)); 206 bottom));
  207 +
204 lineStart += wCount; 208 lineStart += wCount;
  209 +
205 if (maxLines != null && ++lines > maxLines) { 210 if (maxLines != null && ++lines > maxLines) {
206 break; 211 break;
207 } 212 }
@@ -229,6 +234,38 @@ class RichText extends Widget { @@ -229,6 +234,38 @@ class RichText extends Widget {
229 metrics.advanceWidth + space.advanceWidth * style.wordSpacing; 234 metrics.advanceWidth + space.advanceWidth * style.wordSpacing;
230 } 235 }
231 236
  237 + if (softWrap && line < spanLines.length - 1) {
  238 + width = math.max(
  239 + width,
  240 + _realignLine(
  241 + _words.sublist(lineStart),
  242 + constraintWidth,
  243 + offsetX - space.advanceWidth * style.wordSpacing,
  244 + false,
  245 + bottom));
  246 +
  247 + lineStart += wCount;
  248 +
  249 + if (maxLines != null && ++lines > maxLines) {
  250 + break;
  251 + }
  252 +
  253 + offsetX = 0.0;
  254 + if (wCount > 0) {
  255 + offsetY += bottom - top + style.lineSpacing;
  256 + } else {
  257 + offsetY += space.ascent + space.descent + style.lineSpacing;
  258 + }
  259 + top = null;
  260 + bottom = null;
  261 +
  262 + if (offsetY > constraintHeight) {
  263 + return false;
  264 + }
  265 + wCount = 0;
  266 + }
  267 + }
  268 +
232 offsetX -= space.advanceWidth * style.wordSpacing; 269 offsetX -= space.advanceWidth * style.wordSpacing;
233 return true; 270 return true;
234 }, defaultstyle); 271 }, defaultstyle);