Add Text.rich constructor to Text widget (#15317)
* add fromSpan constructor to Text widget and simple smoke test: * change fromSpan to rich, clean up comments * 'directly from' to 'with' * make sure text styles are applied to either data or textspan. add diagnostic properties for span * add expectation for text style to test case * some work on diagnostics and docs
This commit is contained in:
parent
4ace88bfc0
commit
7d1ceb4080
@ -164,8 +164,9 @@ class DefaultTextStyle extends InheritedWidget {
|
|||||||
/// for example, to make the text bold while using the default font family and
|
/// for example, to make the text bold while using the default font family and
|
||||||
/// size.
|
/// size.
|
||||||
///
|
///
|
||||||
/// To display text that uses multiple styles (e.g., a paragraph with some bold
|
/// Using the [new TextSpan.rich] constructor, the [Text] widget can also be
|
||||||
/// words), use [RichText].
|
/// created with a [TextSpan] to display text that use multiple styles
|
||||||
|
/// (e.g., a paragraph with some bold words).
|
||||||
///
|
///
|
||||||
/// ## Sample code
|
/// ## Sample code
|
||||||
///
|
///
|
||||||
@ -210,11 +211,33 @@ class Text extends StatelessWidget {
|
|||||||
this.textScaleFactor,
|
this.textScaleFactor,
|
||||||
this.maxLines,
|
this.maxLines,
|
||||||
}) : assert(data != null),
|
}) : assert(data != null),
|
||||||
|
textSpan = null,
|
||||||
super(key: key);
|
super(key: key);
|
||||||
|
|
||||||
|
/// Creates a text widget with a [TextSpan].
|
||||||
|
const Text.rich(this.textSpan, {
|
||||||
|
Key key,
|
||||||
|
this.style,
|
||||||
|
this.textAlign,
|
||||||
|
this.textDirection,
|
||||||
|
this.softWrap,
|
||||||
|
this.overflow,
|
||||||
|
this.textScaleFactor,
|
||||||
|
this.maxLines,
|
||||||
|
}): assert(textSpan != null),
|
||||||
|
data = null,
|
||||||
|
super(key: key);
|
||||||
|
|
||||||
/// The text to display.
|
/// The text to display.
|
||||||
|
///
|
||||||
|
/// This will be null if a [textSpan] is provided instead.
|
||||||
final String data;
|
final String data;
|
||||||
|
|
||||||
|
/// The text to display as a [TextSpan].
|
||||||
|
///
|
||||||
|
/// This will be null if [data] is provided instead.
|
||||||
|
final TextSpan textSpan;
|
||||||
|
|
||||||
/// If non-null, the style to use for this text.
|
/// If non-null, the style to use for this text.
|
||||||
///
|
///
|
||||||
/// If the style's "inherit" property is true, the style will be merged with
|
/// If the style's "inherit" property is true, the style will be merged with
|
||||||
@ -287,7 +310,8 @@ class Text extends StatelessWidget {
|
|||||||
text: new TextSpan(
|
text: new TextSpan(
|
||||||
style: effectiveTextStyle,
|
style: effectiveTextStyle,
|
||||||
text: data,
|
text: data,
|
||||||
)
|
children: textSpan != null ? <TextSpan>[textSpan] : null,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,6 +319,9 @@ class Text extends StatelessWidget {
|
|||||||
void debugFillProperties(DiagnosticPropertiesBuilder description) {
|
void debugFillProperties(DiagnosticPropertiesBuilder description) {
|
||||||
super.debugFillProperties(description);
|
super.debugFillProperties(description);
|
||||||
description.add(new StringProperty('data', data, showName: false));
|
description.add(new StringProperty('data', data, showName: false));
|
||||||
|
if (textSpan != null) {
|
||||||
|
description.add(textSpan.toDiagnosticsNode(name: 'textSpan', style: DiagnosticsTreeStyle.transition));
|
||||||
|
}
|
||||||
style?.debugFillProperties(description);
|
style?.debugFillProperties(description);
|
||||||
description.add(new EnumProperty<TextAlign>('textAlign', textAlign, defaultValue: null));
|
description.add(new EnumProperty<TextAlign>('textAlign', textAlign, defaultValue: null));
|
||||||
description.add(new EnumProperty<TextDirection>('textDirection', textDirection, defaultValue: null));
|
description.add(new EnumProperty<TextDirection>('textDirection', textDirection, defaultValue: null));
|
||||||
|
@ -85,4 +85,28 @@ void main() {
|
|||||||
expect(message, contains('Directionality'));
|
expect(message, contains('Directionality'));
|
||||||
expect(message, contains(' Text '));
|
expect(message, contains(' Text '));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('Text can be created from TextSpans and uses defaultTextStyle', (WidgetTester tester) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
const DefaultTextStyle(
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 20.0,
|
||||||
|
),
|
||||||
|
child: const Text.rich(
|
||||||
|
const TextSpan(
|
||||||
|
text: 'Hello',
|
||||||
|
children: const <TextSpan>[
|
||||||
|
const TextSpan(text: ' beautiful ', style: const TextStyle(fontStyle: FontStyle.italic)),
|
||||||
|
const TextSpan(text: 'world', style: const TextStyle(fontWeight: FontWeight.bold)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
textDirection: TextDirection.ltr,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final RichText text = tester.firstWidget(find.byType(RichText));
|
||||||
|
expect(text, isNotNull);
|
||||||
|
expect(text.text.style.fontSize, 20.0);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user