From ee903af03f04478795b4ad37881849d5bc304b42 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 6 May 2016 17:33:27 -0700 Subject: [PATCH] Move TextAlign out of TextStyle (#3789) TextAlign applies to a whole paragraph instead of applying to an individual text span. This patch moves the property out of TextStyle and into a separate property on Text and RichText. --- dev/manual_tests/card_collection.dart | 39 +++++---- dev/manual_tests/drag_and_drop.dart | 9 +- .../lib/demo/modal_bottom_sheet_demo.dart | 16 ++-- .../demo/persistent_bottom_sheet_demo.dart | 16 ++-- .../lib/demo/tabs_fab_demo.dart | 14 ++-- examples/stocks/lib/stock_row.dart | 6 +- examples/stocks/lib/stock_symbol_viewer.dart | 2 +- packages/flutter/lib/src/material/app.dart | 1 - .../flutter/lib/src/material/time_picker.dart | 2 +- .../lib/src/painting/text_painter.dart | 37 ++++++--- .../flutter/lib/src/painting/text_style.dart | 26 +----- .../flutter/lib/src/rendering/child_view.dart | 5 +- .../lib/src/rendering/editable_line.dart | 2 +- .../flutter/lib/src/rendering/paragraph.dart | 20 ++++- packages/flutter/lib/src/widgets/banner.dart | 8 +- packages/flutter/lib/src/widgets/basic.dart | 82 +++++++++++++++---- .../lib/src/widgets/semantics_debugger.dart | 4 +- .../lib/src/markdown_raw.dart | 4 +- packages/flutter_sprites/lib/src/label.dart | 29 ++++--- packages/playfair/lib/src/base.dart | 4 +- 20 files changed, 194 insertions(+), 132 deletions(-) diff --git a/dev/manual_tests/card_collection.dart b/dev/manual_tests/card_collection.dart index 992fdf1606..3541673098 100644 --- a/dev/manual_tests/card_collection.dart +++ b/dev/manual_tests/card_collection.dart @@ -32,13 +32,12 @@ class CardCollectionState extends State { static const double kCardMargins = 8.0; static const double kFixedCardHeight = 100.0; - final TextStyle backgroundTextStyle = - Typography.white.title.copyWith(textAlign: TextAlign.center); + final TextStyle backgroundTextStyle = Typography.white.title; Map _primaryColor = Colors.deepPurple; List _cardModels; DismissDirection _dismissDirection = DismissDirection.horizontal; - TextStyle _textStyle = new TextStyle(textAlign: TextAlign.center); + TextAlign _textAlign = TextAlign.center; bool _editable = false; bool _snapToCenter = false; bool _fixedSizeCards = false; @@ -143,9 +142,9 @@ class CardCollectionState extends State { buildDrawerDirectionRadioItem("Dismiss left", DismissDirection.endToStart, _dismissDirection, _changeDismissDirection, icon: Icons.arrow_back), buildDrawerDirectionRadioItem("Dismiss right", DismissDirection.startToEnd, _dismissDirection, _changeDismissDirection, icon: Icons.arrow_forward), new Divider(), - buildFontRadioItem("Left-align text", new TextStyle(textAlign: TextAlign.left), _textStyle, _changeTextStyle, icon: Icons.format_align_left, enabled: !_editable), - buildFontRadioItem("Center-align text", new TextStyle(textAlign: TextAlign.center), _textStyle, _changeTextStyle, icon: Icons.format_align_center, enabled: !_editable), - buildFontRadioItem("Right-align text", new TextStyle(textAlign: TextAlign.right), _textStyle, _changeTextStyle, icon: Icons.format_align_right, enabled: !_editable), + buildFontRadioItem("Left-align text", TextAlign.left, _textAlign, _changeTextAlign, icon: Icons.format_align_left, enabled: !_editable), + buildFontRadioItem("Center-align text", TextAlign.center, _textAlign, _changeTextAlign, icon: Icons.format_align_center, enabled: !_editable), + buildFontRadioItem("Right-align text", TextAlign.right, _textAlign, _changeTextAlign, icon: Icons.format_align_right, enabled: !_editable), new Divider(), new DrawerItem( icon: Icons.dvr, @@ -205,9 +204,9 @@ class CardCollectionState extends State { }); } - void _changeTextStyle(TextStyle newTextStyle) { + void _changeTextAlign(TextAlign newTextAlign) { setState(() { - _textStyle = newTextStyle; + _textAlign = newTextAlign; }); } @@ -260,14 +259,14 @@ class CardCollectionState extends State { ); } - Widget buildFontRadioItem(String label, TextStyle itemValue, TextStyle currentValue, ValueChanged onChanged, { IconData icon, bool enabled: true }) { + Widget buildFontRadioItem(String label, TextAlign itemValue, TextAlign currentValue, ValueChanged onChanged, { IconData icon, bool enabled: true }) { return new DrawerItem( icon: icon, onPressed: enabled ? () { onChanged(itemValue); } : null, child: new Row( children: [ new Flexible(child: new Text(label)), - new Radio( + new Radio( value: itemValue, groupValue: currentValue, onChanged: enabled ? onChanged : null @@ -319,16 +318,17 @@ class CardCollectionState extends State { } ) ) - : new DefaultTextStyle( - style: DefaultTextStyle.of(context).merge(cardLabelStyle).merge(_textStyle).copyWith( + : new DefaultTextStyle.inherit( + context: context, + style: cardLabelStyle.copyWith( fontSize: _varyFontSizes ? 5.0 + index : null ), child: new Column( - children: [ - new Text(cardModel.inputValue.text) - ], crossAxisAlignment: CrossAxisAlignment.stretch, - mainAxisAlignment: MainAxisAlignment.center + mainAxisAlignment: MainAxisAlignment.center, + children: [ + new Text(cardModel.inputValue.text, textAlign: _textAlign) + ] ) ) ) @@ -379,7 +379,12 @@ class CardCollectionState extends State { child: new Row( children: [ leftArrowIcon, - new Flexible(child: new Text(backgroundMessage, style: backgroundTextStyle)), + new Flexible( + child: new Text(backgroundMessage, + style: backgroundTextStyle, + textAlign: TextAlign.center + ) + ), rightArrowIcon ] ) diff --git a/dev/manual_tests/drag_and_drop.dart b/dev/manual_tests/drag_and_drop.dart index 80fe9c9d31..64304b82c8 100644 --- a/dev/manual_tests/drag_and_drop.dart +++ b/dev/manual_tests/drag_and_drop.dart @@ -98,7 +98,8 @@ class ExampleDragSource extends StatelessWidget { size *= kHeavyMultiplier; Widget contents = new DefaultTextStyle( - style: Theme.of(context).textTheme.body1.copyWith(textAlign: TextAlign.center), + style: Theme.of(context).textTheme.body1, + textAlign: TextAlign.center, child: new Dot( color: color, size: size, @@ -185,10 +186,8 @@ class MovableBall extends StatelessWidget { @override Widget build(BuildContext context) { Widget ball = new DefaultTextStyle( - style: Theme.of(context).textTheme.body1.copyWith( - textAlign: TextAlign.center, - color: Colors.white - ), + style: Typography.white.body1, + textAlign: TextAlign.center, child: new Dot( key: kBallKey, color: Colors.blue[700], diff --git a/examples/material_gallery/lib/demo/modal_bottom_sheet_demo.dart b/examples/material_gallery/lib/demo/modal_bottom_sheet_demo.dart index d7dddb8c3f..d404e50e7b 100644 --- a/examples/material_gallery/lib/demo/modal_bottom_sheet_demo.dart +++ b/examples/material_gallery/lib/demo/modal_bottom_sheet_demo.dart @@ -4,15 +4,14 @@ import 'package:flutter/material.dart'; +final TextStyle _kTextStyle = new TextStyle( + color: Colors.indigo[400], + fontSize: 24.0 +); + class ModalBottomSheetDemo extends StatelessWidget { static const String routeName = '/modal-bottom-sheet'; - final TextStyle textStyle = new TextStyle( - color: Colors.indigo[400], - fontSize: 24.0, - textAlign: TextAlign.center - ); - @override Widget build(BuildContext context) { return new Scaffold( @@ -25,7 +24,10 @@ class ModalBottomSheetDemo extends StatelessWidget { return new Container( child: new Padding( padding: const EdgeInsets.all(32.0), - child: new Text('This is the modal bottom sheet. Click anywhere to dismiss.', style: textStyle) + child: new Text('This is the modal bottom sheet. Click anywhere to dismiss.', + style: _kTextStyle, + textAlign: TextAlign.center + ) ) ); }); diff --git a/examples/material_gallery/lib/demo/persistent_bottom_sheet_demo.dart b/examples/material_gallery/lib/demo/persistent_bottom_sheet_demo.dart index 242c8d7e85..cb3c3b05c5 100644 --- a/examples/material_gallery/lib/demo/persistent_bottom_sheet_demo.dart +++ b/examples/material_gallery/lib/demo/persistent_bottom_sheet_demo.dart @@ -4,6 +4,11 @@ import 'package:flutter/material.dart'; +final TextStyle _kTextStyle = new TextStyle( + color: Colors.indigo[400], + fontSize: 24.0 +); + class PersistentBottomSheetDemo extends StatefulWidget { static const String routeName = '/persistent-bottom-sheet'; @@ -14,12 +19,6 @@ class PersistentBottomSheetDemo extends StatefulWidget { class _PersistentBottomSheetDemoState extends State { final GlobalKey _scaffoldKey = new GlobalKey(); - final TextStyle textStyle = new TextStyle( - color: Colors.indigo[400], - fontSize: 24.0, - textAlign: TextAlign.center - ); - VoidCallback _showBottomSheetCallback; @override @@ -40,7 +39,10 @@ class _PersistentBottomSheetDemoState extends State { ), child: new Padding( padding: const EdgeInsets.all(32.0), - child: new Text('This is a Material persistent bottom sheet. Drag downwards to dismiss it.', style: textStyle) + child: new Text('This is a Material persistent bottom sheet. Drag downwards to dismiss it.', + style: _kTextStyle, + textAlign: TextAlign.center + ) ) ); }) diff --git a/examples/material_gallery/lib/demo/tabs_fab_demo.dart b/examples/material_gallery/lib/demo/tabs_fab_demo.dart index 5c32c09e2c..6dbaec1bef 100644 --- a/examples/material_gallery/lib/demo/tabs_fab_demo.dart +++ b/examples/material_gallery/lib/demo/tabs_fab_demo.dart @@ -72,18 +72,18 @@ class _TabsFabDemoState extends State { Widget buildTabView(_Page page) { return new Builder( builder: (BuildContext context) { - final TextStyle textStyle = new TextStyle( - color: page.labelColor, - fontSize: 32.0, - textAlign: TextAlign.center - ); - return new Container( key: new ValueKey(page.label), padding: const EdgeInsets.fromLTRB(48.0, 48.0, 48.0, 96.0), child: new Card( child: new Center( - child: new Text(page.label, style: textStyle) + child: new Text(page.label, + style: new TextStyle( + color: page.labelColor, + fontSize: 32.0 + ), + textAlign: TextAlign.center + ) ) ) ); diff --git a/examples/stocks/lib/stock_row.dart b/examples/stocks/lib/stock_row.dart index c3ff432326..200449aaad 100644 --- a/examples/stocks/lib/stock_row.dart +++ b/examples/stocks/lib/stock_row.dart @@ -100,18 +100,18 @@ class StockRow extends StatelessWidget { new Flexible( child: new Text( lastSale, - style: const TextStyle(textAlign: TextAlign.right) + textAlign: TextAlign.right ) ), new Flexible( child: new Text( changeInPrice, - style: const TextStyle(textAlign: TextAlign.right) + textAlign: TextAlign.right ) ), ], crossAxisAlignment: CrossAxisAlignment.baseline, - textBaseline: DefaultTextStyle.of(context).textBaseline + textBaseline: DefaultTextStyle.of(context).style.textBaseline ) ), ] diff --git a/examples/stocks/lib/stock_symbol_viewer.dart b/examples/stocks/lib/stock_symbol_viewer.dart index 6812fedc12..4af800bfc2 100644 --- a/examples/stocks/lib/stock_symbol_viewer.dart +++ b/examples/stocks/lib/stock_symbol_viewer.dart @@ -52,7 +52,7 @@ class StockSymbolView extends StatelessWidget { ), new RichText( text: new TextSpan( - style: DefaultTextStyle.of(context).merge(new TextStyle(fontSize: 8.0)), + style: DefaultTextStyle.of(context).style.merge(new TextStyle(fontSize: 8.0)), text: 'Prices may be delayed by ', children: [ new TextSpan(text: 'several', style: new TextStyle(fontStyle: FontStyle.italic)), diff --git a/packages/flutter/lib/src/material/app.dart b/packages/flutter/lib/src/material/app.dart index da6a9e50cf..6fc9c0acbb 100644 --- a/packages/flutter/lib/src/material/app.dart +++ b/packages/flutter/lib/src/material/app.dart @@ -16,7 +16,6 @@ const TextStyle _errorTextStyle = const TextStyle( fontFamily: 'monospace', fontSize: 48.0, fontWeight: FontWeight.w900, - textAlign: TextAlign.right, decoration: TextDecoration.underline, decorationColor: const Color(0xFFFFFF00), decorationStyle: TextDecorationStyle.double diff --git a/packages/flutter/lib/src/material/time_picker.dart b/packages/flutter/lib/src/material/time_picker.dart index fed10e5783..d08088fcb4 100644 --- a/packages/flutter/lib/src/material/time_picker.dart +++ b/packages/flutter/lib/src/material/time_picker.dart @@ -274,7 +274,7 @@ List _initPainters(List labels) { for (int i = 0; i < painters.length; ++i) { String label = labels[i]; painters[i] = new TextPainter( - new TextSpan(style: style, text: label) + text: new TextSpan(style: style, text: label) )..layout(); } return painters; diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index cbbc6f9080..d85f44228b 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -30,8 +30,11 @@ class TextPainter { /// /// The text argument is optional but [text] must be non-null before calling /// [layout]. - TextPainter([ TextSpan text ]) { - this.text = text; + TextPainter({ + TextSpan text, + TextAlign textAlign + }) : _text = text, _textAlign = textAlign { + assert(text == null || text.debugAssertValid()); } ui.Paragraph _paragraph; @@ -45,15 +48,19 @@ class TextPainter { if (_text == value) return; _text = value; - if (_text != null) { - ui.ParagraphBuilder builder = new ui.ParagraphBuilder(); - _text.build(builder); - _paragraph = builder.build(_text.style?.paragraphStyle ?? new ui.ParagraphStyle()); - _needsLayout = true; - } else { - _paragraph = null; - _needsLayout = false; - } + _paragraph = null; + _needsLayout = true; + } + + /// How the text should be aligned horizontally. + TextAlign get textAlign => _textAlign; + TextAlign _textAlign; + void set textAlign(TextAlign value) { + if (_textAlign == value) + return; + _textAlign = value; + _paragraph = null; + _needsLayout = true; } // Unfortunately, using full precision floating point here causes bad layouts @@ -129,9 +136,17 @@ class TextPainter { /// width as possible while still being greater than or equal to minWidth and /// less than or equal to maxWidth. void layout({ double minWidth: 0.0, double maxWidth: double.INFINITY }) { + assert(_text != null); if (!_needsLayout && minWidth == _lastMinWidth && maxWidth == _lastMaxWidth) return; _needsLayout = false; + if (_paragraph == null) { + ui.ParagraphBuilder builder = new ui.ParagraphBuilder(); + _text.build(builder); + ui.ParagraphStyle paragraphStyle = _text.style?.getParagraphStyle(textAlign: textAlign); + paragraphStyle ??= new ui.ParagraphStyle(); + _paragraph = builder.build(paragraphStyle); + } _lastMinWidth = minWidth; _lastMaxWidth = maxWidth; _paragraph.layout(new ui.ParagraphConstraints(width: maxWidth)); diff --git a/packages/flutter/lib/src/painting/text_style.dart b/packages/flutter/lib/src/painting/text_style.dart index 4aaca87a3f..3d42fd2a76 100644 --- a/packages/flutter/lib/src/painting/text_style.dart +++ b/packages/flutter/lib/src/painting/text_style.dart @@ -18,7 +18,6 @@ class TextStyle { this.fontStyle, this.letterSpacing, this.wordSpacing, - this.textAlign, this.textBaseline, this.height, this.decoration, @@ -50,10 +49,6 @@ class TextStyle { /// The amount of space (in logical pixels) to add at each sequence of white-space (i.e. between each word). final double wordSpacing; - /// How the text should be aligned (applies only to the outermost - /// StyledTextSpan, which establishes the container for the text). - final TextAlign textAlign; - /// The baseline to use for aligning the text. final TextBaseline textBaseline; @@ -82,7 +77,6 @@ class TextStyle { FontStyle fontStyle, double letterSpacing, double wordSpacing, - TextAlign textAlign, TextBaseline textBaseline, double height, TextDecoration decoration, @@ -98,7 +92,6 @@ class TextStyle { fontStyle: fontStyle != null ? fontStyle : this.fontStyle, letterSpacing: letterSpacing != null ? letterSpacing : this.letterSpacing, wordSpacing: wordSpacing != null ? wordSpacing : this.wordSpacing, - textAlign: textAlign != null ? textAlign : this.textAlign, textBaseline: textBaseline != null ? textBaseline : this.textBaseline, height: height != null ? height : this.height, decoration: decoration != null ? decoration : this.decoration, @@ -122,7 +115,6 @@ class TextStyle { fontStyle: other.fontStyle, letterSpacing: other.letterSpacing, wordSpacing: other.wordSpacing, - textAlign: other.textAlign, textBaseline: other.textBaseline, height: other.height, decoration: other.decoration, @@ -146,7 +138,6 @@ class TextStyle { fontStyle: t < 0.5 ? begin.fontStyle : end.fontStyle, letterSpacing: ui.lerpDouble(begin.letterSpacing ?? end.letterSpacing, end.letterSpacing ?? begin.letterSpacing, t), wordSpacing: ui.lerpDouble(begin.wordSpacing ?? end.wordSpacing, end.wordSpacing ?? begin.wordSpacing, t), - textAlign: t < 0.5 ? begin.textAlign : end.textAlign, textBaseline: t < 0.5 ? begin.textBaseline : end.textBaseline, height: ui.lerpDouble(begin.height ?? end.height, end.height ?? begin.height, t), decoration: t < 0.5 ? begin.decoration : end.decoration, @@ -173,7 +164,7 @@ class TextStyle { } /// The style information for paragraphs, encoded for use by `dart:ui`. - ui.ParagraphStyle get paragraphStyle { + ui.ParagraphStyle getParagraphStyle({ TextAlign textAlign }) { return new ui.ParagraphStyle( textAlign: textAlign, textBaseline: textBaseline, @@ -200,7 +191,6 @@ class TextStyle { fontStyle == typedOther.fontStyle && letterSpacing == typedOther.letterSpacing && wordSpacing == typedOther.wordSpacing && - textAlign == typedOther.textAlign && textBaseline == typedOther.textBaseline && height == typedOther.height && decoration == typedOther.decoration && @@ -219,7 +209,6 @@ class TextStyle { fontStyle, letterSpacing, wordSpacing, - textAlign, textBaseline, height, decoration, @@ -283,19 +272,6 @@ class TextStyle { result.add('${prefix}letterSpacing: ${letterSpacing}x'); if (wordSpacing != null) result.add('${prefix}wordSpacing: ${wordSpacing}x'); - if (textAlign != null) { - switch (textAlign) { - case TextAlign.left: - result.add('${prefix}align: left'); - break; - case TextAlign.right: - result.add('${prefix}align: right'); - break; - case TextAlign.center: - result.add('${prefix}align: center'); - break; - } - } if (textBaseline != null) { switch (textBaseline) { case TextBaseline.alphabetic: diff --git a/packages/flutter/lib/src/rendering/child_view.dart b/packages/flutter/lib/src/rendering/child_view.dart index 94ed402b4c..7abc8f8f35 100644 --- a/packages/flutter/lib/src/rendering/child_view.dart +++ b/packages/flutter/lib/src/rendering/child_view.dart @@ -287,8 +287,9 @@ class RenderChildView extends RenderBox { _child._setChildProperties(_physicalWidth, _physicalHeight, scale); assert(() { if (_view == null) { - _debugErrorMessage ??= new TextPainter() - ..text = new TextSpan(text: 'Child view are supported only when running in Mojo shell.'); + _debugErrorMessage ??= new TextPainter( + text: new TextSpan(text: 'Child views are supported only when running in Mojo shell.') + ); _debugErrorMessage.layout(minWidth: size.width, maxWidth: size.width); } return true; diff --git a/packages/flutter/lib/src/rendering/editable_line.dart b/packages/flutter/lib/src/rendering/editable_line.dart index c490d7fd2c..d7ffb5b6db 100644 --- a/packages/flutter/lib/src/rendering/editable_line.dart +++ b/packages/flutter/lib/src/rendering/editable_line.dart @@ -42,7 +42,7 @@ class RenderEditableLine extends RenderBox { this.onSelectionChanged, Offset paintOffset: Offset.zero, this.onPaintOffsetUpdateNeeded - }) : _textPainter = new TextPainter(text), + }) : _textPainter = new TextPainter(text: text), _cursorColor = cursorColor, _showCursor = showCursor, _selection = selection, diff --git a/packages/flutter/lib/src/rendering/paragraph.dart b/packages/flutter/lib/src/rendering/paragraph.dart index 3e240211bf..2a23a40d83 100644 --- a/packages/flutter/lib/src/rendering/paragraph.dart +++ b/packages/flutter/lib/src/rendering/paragraph.dart @@ -11,9 +11,9 @@ import 'semantics.dart'; /// A render object that displays a paragraph of text class RenderParagraph extends RenderBox { - RenderParagraph( - TextSpan text - ) : _textPainter = new TextPainter(text) { + RenderParagraph(TextSpan text, { + TextAlign textAlign + }) : _textPainter = new TextPainter(text: text, textAlign: textAlign) { assert(text != null); assert(text.debugAssertValid()); } @@ -23,13 +23,22 @@ class RenderParagraph extends RenderBox { /// The text to display TextSpan get text => _textPainter.text; void set text(TextSpan value) { - assert(value.debugAssertValid()); + assert(value != null); if (_textPainter.text == value) return; _textPainter.text = value; markNeedsLayout(); } + /// How the text should be aligned horizontally. + TextAlign get textAlign => _textPainter.textAlign; + void set textAlign(TextAlign value) { + if (_textPainter.textAlign == value) + return; + _textPainter.textAlign = value; + markNeedsPaint(); + } + void _layoutText(BoxConstraints constraints) { assert(constraints != null); assert(constraints.debugAssertIsValid()); @@ -101,6 +110,9 @@ class RenderParagraph extends RenderBox { // // TODO(abarth): Make computing the min/max intrinsic width/height // a non-destructive operation. + // + // If you remove this call, make sure that changing the textAlign still + // works properly. _layoutText(constraints); _textPainter.paint(context.canvas, offset); } diff --git a/packages/flutter/lib/src/widgets/banner.dart b/packages/flutter/lib/src/widgets/banner.dart index 3b4b3c0271..77814dab41 100644 --- a/packages/flutter/lib/src/widgets/banner.dart +++ b/packages/flutter/lib/src/widgets/banner.dart @@ -15,7 +15,6 @@ const TextStyle _kTextStyles = const TextStyle( color: const Color(0xFFFFFFFF), fontSize: _kHeight * 0.85, fontWeight: FontWeight.w900, - textAlign: TextAlign.center, height: 1.0 ); @@ -64,9 +63,10 @@ class BannerPainter extends CustomPainter { ..drawRect(_kRect, paintBanner); final double width = _kOffset * 2.0; - final TextPainter textPainter = new TextPainter() - ..text = new TextSpan(style: _kTextStyles, text: message) - ..layout(minWidth: width, maxWidth: width); + final TextPainter textPainter = new TextPainter( + text: new TextSpan(style: _kTextStyles, text: message), + textAlign: TextAlign.center + )..layout(minWidth: width, maxWidth: width); textPainter.paint(canvas, _kRect.topLeft.toOffset() + new Offset(0.0, (_kRect.height - textPainter.height) / 2.0)); } diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 5d2aa0aa62..530f00b388 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -1904,40 +1904,84 @@ class RichText extends LeafRenderObjectWidget { /// Creates a paragraph of rich text. /// /// The [text] argument is required to be non-null. - RichText({ Key key, this.text }) : super(key: key) { + RichText({ Key key, this.text, this.textAlign }) : super(key: key) { assert(text != null); } /// The text to display in this widget. final TextSpan text; + /// How the text should be aligned horizontally. + final TextAlign textAlign; + @override - RenderParagraph createRenderObject(BuildContext context) => new RenderParagraph(text); + RenderParagraph createRenderObject(BuildContext context) { + return new RenderParagraph(text, textAlign: textAlign); + } @override void updateRenderObject(BuildContext context, RenderParagraph renderObject) { - renderObject.text = text; + renderObject + ..text = text + ..textAlign = textAlign; } } /// The text style to apply to descendant [Text] widgets without explicit style. class DefaultTextStyle extends InheritedWidget { + /// Creates a default text style for the given subtree. + /// + /// Consider using [DefaultTextStyle.inherit] to inherit styling information + /// from a the current default text style for a given [BuildContext]. DefaultTextStyle({ Key key, this.style, + this.textAlign, Widget child }) : super(key: key, child: child) { assert(style != null); assert(child != null); } + /// A const-constructible default text style that provides fallback values. + /// + /// Returned from [of] when the given [BuildContext] doesn't have an enclosing default text style. + const DefaultTextStyle.fallback() : style = const TextStyle(), textAlign = null; + + /// Creates a default text style that inherits from the given [BuildContext]. + /// + /// The given [style] is merged with the [style] from the default text style + /// for the given [BuildContext] and, if non-null, the given [textAlign] + /// replaces the [textAlign] from the default text style for the given + /// [BuildContext]. + factory DefaultTextStyle.inherit({ + Key key, + BuildContext context, + TextStyle style, + TextAlign textAlign, + Widget child + }) { + DefaultTextStyle parent = DefaultTextStyle.of(context); + return new DefaultTextStyle( + key: key, + style: parent.style.merge(style), + textAlign: textAlign ?? parent.textAlign, + child: child + ); + } + /// The text style to apply. final TextStyle style; - /// The style from the closest instance of this class that encloses the given context. - static TextStyle of(BuildContext context) { - DefaultTextStyle result = context.inheritFromWidgetOfExactType(DefaultTextStyle); - return result?.style ?? const TextStyle(); + /// How the text should be aligned horizontally. + final TextAlign textAlign; + + /// The closest instance of this class that encloses the given context. + /// + /// If no such instance exists, returns an instance created by + /// [DefaultTextStyle.fallback], which contains fallback values. + static DefaultTextStyle of(BuildContext context) { + return context.inheritFromWidgetOfExactType(DefaultTextStyle) ?? const DefaultTextStyle.fallback(); } @override @@ -1975,7 +2019,7 @@ class Text extends StatelessWidget { /// /// If the [style] argument is null, the text will use the style from the /// closest enclosing [DefaultTextStyle]. - Text(this.data, { Key key, this.style }) : super(key: key) { + Text(this.data, { Key key, this.style, this.textAlign }) : super(key: key) { assert(data != null); } @@ -1989,18 +2033,26 @@ class Text extends StatelessWidget { /// replace the closest enclosing [DefaultTextStyle]. final TextStyle style; - TextStyle _getEffectiveStyle(BuildContext context) { - if (style == null || style.inherit) - return DefaultTextStyle.of(context).merge(style); - else - return style; - } + /// How the text should be aligned horizontally. + final TextAlign textAlign; @override Widget build(BuildContext context) { + DefaultTextStyle defaultTextStyle; + TextStyle effectiveTextStyle = style; + if (style == null || style.inherit) { + defaultTextStyle ??= DefaultTextStyle.of(context); + effectiveTextStyle = defaultTextStyle.style.merge(style); + } + TextAlign effectiveTextAlign = textAlign; + if (effectiveTextAlign == null) { + defaultTextStyle ??= DefaultTextStyle.of(context); + effectiveTextAlign = defaultTextStyle.textAlign; + } return new RichText( + textAlign: effectiveTextAlign, text: new TextSpan( - style: _getEffectiveStyle(context), + style: effectiveTextStyle, text: data ) ); diff --git a/packages/flutter/lib/src/widgets/semantics_debugger.dart b/packages/flutter/lib/src/widgets/semantics_debugger.dart index b025995ed3..686df6e0d6 100644 --- a/packages/flutter/lib/src/widgets/semantics_debugger.dart +++ b/packages/flutter/lib/src/widgets/semantics_debugger.dart @@ -146,8 +146,7 @@ class _SemanticsDebuggerEntry { static const TextStyle textStyles = const TextStyle( color: const Color(0xFF000000), fontSize: 10.0, - height: 0.8, - textAlign: TextAlign.center + height: 0.8 ); TextPainter textPainter; @@ -185,6 +184,7 @@ class _SemanticsDebuggerEntry { textPainter ??= new TextPainter(); textPainter ..text = new TextSpan(style: textStyles, text: message) + ..textAlign = TextAlign.center ..layout(maxWidth: rect.width); } else { textPainter = null; diff --git a/packages/flutter_markdown/lib/src/markdown_raw.dart b/packages/flutter_markdown/lib/src/markdown_raw.dart index adb2f72227..45d16ab194 100644 --- a/packages/flutter_markdown/lib/src/markdown_raw.dart +++ b/packages/flutter_markdown/lib/src/markdown_raw.dart @@ -387,7 +387,7 @@ class _Block { if (listIndents.last == 'ul') { bullet = new Text( '•', - style: new TextStyle(textAlign: TextAlign.center) + textAlign: TextAlign.center ); } else { @@ -395,7 +395,7 @@ class _Block { padding: new EdgeInsets.only(right: 5.0), child: new Text( "${blockPosition + 1}.", - style: new TextStyle(textAlign: TextAlign.right) + textAlign: TextAlign.right ) ); } diff --git a/packages/flutter_sprites/lib/src/label.dart b/packages/flutter_sprites/lib/src/label.dart index 98629f397d..1487f2337a 100644 --- a/packages/flutter_sprites/lib/src/label.dart +++ b/packages/flutter_sprites/lib/src/label.dart @@ -3,47 +3,46 @@ part of flutter_sprites; /// Labels are used to display a string of text in a the node tree. To align /// the label, the textAlign property of the [TextStyle] can be set. class Label extends Node { - /// Creates a new Label with the provided [_text] and [_textStyle]. - Label(this._text, [this._textStyle]) { - if (_textStyle == null) { - _textStyle = new TextStyle(); - } - } - - String _text; + /// Creates a new Label with the provided [text] and [textStyle]. + Label(this._text, { + TextStyle textStyle, + TextAlign textAlign + }) : _textStyle = textStyle ?? const TextStyle(), + textAlign = textAlign ?? TextAlign.left; /// The text being drawn by the label. String get text => _text; - + String _text; void set text(String text) { _text = text; _painter = null; } - TextStyle _textStyle; - /// The style to draw the text in. TextStyle get textStyle => _textStyle; - + TextStyle _textStyle; void set textStyle(TextStyle textStyle) { _textStyle = textStyle; _painter = null; } + /// How the text should be aligned horizontally. + TextAlign textAlign; + TextPainter _painter; double _width; @override void paint(Canvas canvas) { if (_painter == null) { - _painter = new TextPainter(new TextSpan(style: _textStyle, text: _text)) + _painter = new TextPainter(text: new TextSpan(style: _textStyle, text: _text)) ..layout(); } Offset offset = Offset.zero; - if (_textStyle.textAlign == TextAlign.center) { + if (textAlign == TextAlign.center) { offset = new Offset(-_width / 2.0, 0.0); - } else if (_textStyle.textAlign == TextAlign.right) { + } else if (textAlign == TextAlign.right) { offset = new Offset(-_width, 0.0); } diff --git a/packages/playfair/lib/src/base.dart b/packages/playfair/lib/src/base.dart index 94a272ae67..f619ec4ba7 100644 --- a/packages/playfair/lib/src/base.dart +++ b/packages/playfair/lib/src/base.dart @@ -174,7 +174,7 @@ class ChartPainter { style: _textTheme.body1, text: '${gridline.value}' ); - gridline.labelPainter = new TextPainter(text) + gridline.labelPainter = new TextPainter(text: text) ..layout(maxWidth: _rect.width); _horizontalGridlines.add(gridline); yScaleWidth = math.max(yScaleWidth, gridline.labelPainter.maxIntrinsicWidth); @@ -220,7 +220,7 @@ class ChartPainter { style: _textTheme.body1, text: '${data.indicatorText}' ); - _indicator.labelPainter = new TextPainter(text) + _indicator.labelPainter = new TextPainter(text: text) ..layout(maxWidth: markerRect.width); _indicator.labelPosition = new Point( ((_indicator.start.x + _indicator.end.x) / 2.0) - _indicator.labelPainter.maxIntrinsicWidth / 2.0,