Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
278c69c4
Unverified
Commit
278c69c4
authored
Jun 16, 2018
by
Jonah Williams
Committed by
GitHub
Jun 16, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add semantics label property to Text widget (#18518)
parent
24a1f570
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
1 deletion
+57
-1
text.dart
packages/flutter/lib/src/widgets/text.dart
+30
-1
text_test.dart
packages/flutter/test/widgets/text_test.dart
+27
-0
No files found.
packages/flutter/lib/src/widgets/text.dart
View file @
278c69c4
...
...
@@ -211,6 +211,7 @@ class Text extends StatelessWidget {
this
.
overflow
,
this
.
textScaleFactor
,
this
.
maxLines
,
this
.
semanticsLabel
,
})
:
assert
(
data
!=
null
),
textSpan
=
null
,
super
(
key:
key
);
...
...
@@ -226,6 +227,7 @@ class Text extends StatelessWidget {
this
.
overflow
,
this
.
textScaleFactor
,
this
.
maxLines
,
this
.
semanticsLabel
,
}):
assert
(
textSpan
!=
null
),
data
=
null
,
super
(
key:
key
);
...
...
@@ -305,13 +307,27 @@ class Text extends StatelessWidget {
/// widget directly to entirely override the [DefaultTextStyle].
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
Widget
build
(
BuildContext
context
)
{
final
DefaultTextStyle
defaultTextStyle
=
DefaultTextStyle
.
of
(
context
);
TextStyle
effectiveTextStyle
=
style
;
if
(
style
==
null
||
style
.
inherit
)
effectiveTextStyle
=
defaultTextStyle
.
style
.
merge
(
style
);
return
new
RichText
(
Widget
result
=
new
RichText
(
textAlign:
textAlign
??
defaultTextStyle
.
textAlign
??
TextAlign
.
start
,
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
...
...
@@ -325,6 +341,16 @@ class Text extends StatelessWidget {
children:
textSpan
!=
null
?
<
TextSpan
>[
textSpan
]
:
null
,
),
);
if
(
semanticsLabel
!=
null
)
{
result
=
new
Semantics
(
textDirection:
textDirection
,
label:
semanticsLabel
,
child:
new
ExcludeSemantics
(
child:
result
,
)
);
}
return
result
;
}
@override
...
...
@@ -342,5 +368,8 @@ class Text extends StatelessWidget {
properties
.
add
(
new
EnumProperty
<
TextOverflow
>(
'overflow'
,
overflow
,
defaultValue:
null
));
properties
.
add
(
new
DoubleProperty
(
'textScaleFactor'
,
textScaleFactor
,
defaultValue:
null
));
properties
.
add
(
new
IntProperty
(
'maxLines'
,
maxLines
,
defaultValue:
null
));
if
(
semanticsLabel
!=
null
)
{
properties
.
add
(
new
StringProperty
(
'semanticsLabel'
,
semanticsLabel
));
}
}
}
packages/flutter/test/widgets/text_test.dart
View file @
278c69c4
...
...
@@ -5,6 +5,8 @@
import
'package:flutter_test/flutter_test.dart'
;
import
'package:flutter/widgets.dart'
;
import
'semantics_tester.dart'
;
void
main
(
)
{
testWidgets
(
'Text respects media query'
,
(
WidgetTester
tester
)
async
{
await
tester
.
pumpWidget
(
const
MediaQuery
(
...
...
@@ -109,4 +111,29 @@ void main() {
expect
(
text
,
isNotNull
);
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
();
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment