Add semantics label property to Text widget (#18518)
This commit is contained in:
parent
24a1f57065
commit
278c69c4d3
@ -211,6 +211,7 @@ class Text extends StatelessWidget {
|
|||||||
this.overflow,
|
this.overflow,
|
||||||
this.textScaleFactor,
|
this.textScaleFactor,
|
||||||
this.maxLines,
|
this.maxLines,
|
||||||
|
this.semanticsLabel,
|
||||||
}) : assert(data != null),
|
}) : assert(data != null),
|
||||||
textSpan = null,
|
textSpan = null,
|
||||||
super(key: key);
|
super(key: key);
|
||||||
@ -226,6 +227,7 @@ class Text extends StatelessWidget {
|
|||||||
this.overflow,
|
this.overflow,
|
||||||
this.textScaleFactor,
|
this.textScaleFactor,
|
||||||
this.maxLines,
|
this.maxLines,
|
||||||
|
this.semanticsLabel,
|
||||||
}): assert(textSpan != null),
|
}): assert(textSpan != null),
|
||||||
data = null,
|
data = null,
|
||||||
super(key: key);
|
super(key: key);
|
||||||
@ -305,13 +307,27 @@ class Text extends StatelessWidget {
|
|||||||
/// widget directly to entirely override the [DefaultTextStyle].
|
/// widget directly to entirely override the [DefaultTextStyle].
|
||||||
final int maxLines;
|
final int maxLines;
|
||||||
|
|
||||||
|
/// An alternative semantics label for this text.
|
||||||
|
///
|
||||||
|
/// If present, the semantics of this widget will contain this value instead
|
||||||
|
/// of the actual text.
|
||||||
|
///
|
||||||
|
/// This is useful for replacing abbreviations or shorthands will the full
|
||||||
|
/// text value:
|
||||||
|
///
|
||||||
|
/// ```dart
|
||||||
|
/// new Text(r'$$', semanticsLabel: 'Double dollars')
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
final String semanticsLabel;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
|
final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
|
||||||
TextStyle effectiveTextStyle = style;
|
TextStyle effectiveTextStyle = style;
|
||||||
if (style == null || style.inherit)
|
if (style == null || style.inherit)
|
||||||
effectiveTextStyle = defaultTextStyle.style.merge(style);
|
effectiveTextStyle = defaultTextStyle.style.merge(style);
|
||||||
return new RichText(
|
Widget result = new RichText(
|
||||||
textAlign: textAlign ?? defaultTextStyle.textAlign ?? TextAlign.start,
|
textAlign: textAlign ?? defaultTextStyle.textAlign ?? TextAlign.start,
|
||||||
textDirection: textDirection, // RichText uses Directionality.of to obtain a default if this is null.
|
textDirection: textDirection, // RichText uses Directionality.of to obtain a default if this is null.
|
||||||
locale: locale, // RichText uses Localizations.localeOf to obtain a default if this is null
|
locale: locale, // RichText uses Localizations.localeOf to obtain a default if this is null
|
||||||
@ -325,6 +341,16 @@ class Text extends StatelessWidget {
|
|||||||
children: textSpan != null ? <TextSpan>[textSpan] : null,
|
children: textSpan != null ? <TextSpan>[textSpan] : null,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
if (semanticsLabel != null) {
|
||||||
|
result = new Semantics(
|
||||||
|
textDirection: textDirection,
|
||||||
|
label: semanticsLabel,
|
||||||
|
child: new ExcludeSemantics(
|
||||||
|
child: result,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -342,5 +368,8 @@ class Text extends StatelessWidget {
|
|||||||
properties.add(new EnumProperty<TextOverflow>('overflow', overflow, defaultValue: null));
|
properties.add(new EnumProperty<TextOverflow>('overflow', overflow, defaultValue: null));
|
||||||
properties.add(new DoubleProperty('textScaleFactor', textScaleFactor, defaultValue: null));
|
properties.add(new DoubleProperty('textScaleFactor', textScaleFactor, defaultValue: null));
|
||||||
properties.add(new IntProperty('maxLines', maxLines, defaultValue: null));
|
properties.add(new IntProperty('maxLines', maxLines, defaultValue: null));
|
||||||
|
if (semanticsLabel != null) {
|
||||||
|
properties.add(new StringProperty('semanticsLabel', semanticsLabel));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
|
|
||||||
|
import 'semantics_tester.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('Text respects media query', (WidgetTester tester) async {
|
testWidgets('Text respects media query', (WidgetTester tester) async {
|
||||||
await tester.pumpWidget(const MediaQuery(
|
await tester.pumpWidget(const MediaQuery(
|
||||||
@ -109,4 +111,29 @@ void main() {
|
|||||||
expect(text, isNotNull);
|
expect(text, isNotNull);
|
||||||
expect(text.text.style.fontSize, 20.0);
|
expect(text.text.style.fontSize, 20.0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('semanticsLabel can override text label', (WidgetTester tester) async {
|
||||||
|
final SemanticsTester semantics = new SemanticsTester(tester);
|
||||||
|
await tester.pumpWidget(
|
||||||
|
const Text('\$\$', semanticsLabel: 'Double dollars', textDirection: TextDirection.ltr)
|
||||||
|
);
|
||||||
|
final TestSemantics expectedSemantics = new TestSemantics.root(
|
||||||
|
children: <TestSemantics>[
|
||||||
|
new TestSemantics.rootChild(
|
||||||
|
label: 'Double dollars',
|
||||||
|
textDirection: TextDirection.ltr,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
expect(semantics, hasSemantics(expectedSemantics, ignoreTransform: true, ignoreId: true, ignoreRect: true));
|
||||||
|
|
||||||
|
await tester.pumpWidget(
|
||||||
|
const Directionality(
|
||||||
|
textDirection: TextDirection.ltr,
|
||||||
|
child: const Text('\$\$', semanticsLabel: 'Double dollars')),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(semantics, hasSemantics(expectedSemantics, ignoreTransform: true, ignoreId: true, ignoreRect: true));
|
||||||
|
semantics.dispose();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user