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
07c1ef25
Unverified
Commit
07c1ef25
authored
Sep 13, 2022
by
Dan Field
Committed by
GitHub
Sep 13, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Utility methods for measuring text (#111493)
parent
7b5241b3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
105 additions
and
11 deletions
+105
-11
date_picker.dart
packages/flutter/lib/src/cupertino/date_picker.dart
+1
-11
text_painter.dart
packages/flutter/lib/src/painting/text_painter.dart
+80
-0
text_painter_test.dart
packages/flutter/test/painting/text_painter_test.dart
+24
-0
No files found.
packages/flutter/lib/src/cupertino/date_picker.dart
View file @
07c1ef25
...
...
@@ -470,23 +470,13 @@ class CupertinoDatePicker extends StatefulWidget {
assert
(
longestText
!=
''
,
'column type is not appropriate'
);
final
TextPainter
painter
=
TextPainter
(
return
TextPainter
.
computeMaxIntrinsicWidth
(
text:
TextSpan
(
style:
_themeTextStyle
(
context
),
text:
longestText
,
),
textDirection:
Directionality
.
of
(
context
),
);
// This operation is expensive and should be avoided. It is called here only
// because there's no other way to get the information we want without
// laying out the text.
painter
.
layout
();
try
{
return
painter
.
maxIntrinsicWidth
;
}
finally
{
painter
.
dispose
();
}
}
}
...
...
packages/flutter/lib/src/painting/text_painter.dart
View file @
07c1ef25
...
...
@@ -217,6 +217,86 @@ class TextPainter {
_textWidthBasis
=
textWidthBasis
,
_textHeightBehavior
=
textHeightBehavior
;
/// Computes the width of a configured [TextPainter].
///
/// This is a convenience method that creates a text painter with the supplied
/// parameters, lays it out with the supplied [minWidth] and [maxWidth], and
/// returns its [TextPainter.width] making sure to dispose the underlying
/// resources.
static
double
computeWidth
({
required
InlineSpan
text
,
TextAlign
textAlign
=
TextAlign
.
start
,
TextDirection
?
textDirection
,
double
textScaleFactor
=
1.0
,
int
?
maxLines
,
String
?
ellipsis
,
Locale
?
locale
,
StrutStyle
?
strutStyle
,
TextWidthBasis
textWidthBasis
=
TextWidthBasis
.
parent
,
ui
.
TextHeightBehavior
?
textHeightBehavior
,
double
minWidth
=
0.0
,
double
maxWidth
=
double
.
infinity
,
})
{
final
TextPainter
painter
=
TextPainter
(
text:
text
,
textAlign:
textAlign
,
textDirection:
textDirection
,
textScaleFactor:
textScaleFactor
,
maxLines:
maxLines
,
ellipsis:
ellipsis
,
locale:
locale
,
strutStyle:
strutStyle
,
textWidthBasis:
textWidthBasis
,
textHeightBehavior:
textHeightBehavior
,
)..
layout
(
minWidth:
minWidth
,
maxWidth:
maxWidth
);
try
{
return
painter
.
width
;
}
finally
{
painter
.
dispose
();
}
}
/// Computes the max intrinsic width of a configured [TextPainter].
///
/// This is a convenience method that creates a text painter with the supplied
/// parameters, lays it out with the supplied [minWidth] and [maxWidth], and
/// returns its [TextPainter.maxIntrinsicWidth] making sure to dispose the
/// underlying resources.
static
double
computeMaxIntrinsicWidth
({
required
InlineSpan
text
,
TextAlign
textAlign
=
TextAlign
.
start
,
TextDirection
?
textDirection
,
double
textScaleFactor
=
1.0
,
int
?
maxLines
,
String
?
ellipsis
,
Locale
?
locale
,
StrutStyle
?
strutStyle
,
TextWidthBasis
textWidthBasis
=
TextWidthBasis
.
parent
,
ui
.
TextHeightBehavior
?
textHeightBehavior
,
double
minWidth
=
0.0
,
double
maxWidth
=
double
.
infinity
,
})
{
final
TextPainter
painter
=
TextPainter
(
text:
text
,
textAlign:
textAlign
,
textDirection:
textDirection
,
textScaleFactor:
textScaleFactor
,
maxLines:
maxLines
,
ellipsis:
ellipsis
,
locale:
locale
,
strutStyle:
strutStyle
,
textWidthBasis:
textWidthBasis
,
textHeightBehavior:
textHeightBehavior
,
)..
layout
(
minWidth:
minWidth
,
maxWidth:
maxWidth
);
try
{
return
painter
.
maxIntrinsicWidth
;
}
finally
{
painter
.
dispose
();
}
}
// _paragraph being null means the text needs layout because of style changes.
// Setting _paragraph to null invalidates all the layout cache.
//
...
...
packages/flutter/test/painting/text_painter_test.dart
View file @
07c1ef25
...
...
@@ -1184,6 +1184,30 @@ void main() {
painter
.
dispose
();
expect
(
painter
.
debugDisposed
,
true
);
});
test
(
'TextPainter computeWidth'
,
()
{
const
InlineSpan
text
=
TextSpan
(
text:
'foobar'
);
final
TextPainter
painter
=
TextPainter
(
text:
text
,
textDirection:
TextDirection
.
ltr
);
painter
.
layout
();
expect
(
painter
.
width
,
TextPainter
.
computeWidth
(
text:
text
,
textDirection:
TextDirection
.
ltr
));
painter
.
layout
(
minWidth:
500
);
expect
(
painter
.
width
,
TextPainter
.
computeWidth
(
text:
text
,
textDirection:
TextDirection
.
ltr
,
minWidth:
500
));
painter
.
dispose
();
});
test
(
'TextPainter computeMaxIntrinsicWidth'
,
()
{
const
InlineSpan
text
=
TextSpan
(
text:
'foobar'
);
final
TextPainter
painter
=
TextPainter
(
text:
text
,
textDirection:
TextDirection
.
ltr
);
painter
.
layout
();
expect
(
painter
.
maxIntrinsicWidth
,
TextPainter
.
computeMaxIntrinsicWidth
(
text:
text
,
textDirection:
TextDirection
.
ltr
));
painter
.
layout
(
minWidth:
500
);
expect
(
painter
.
maxIntrinsicWidth
,
TextPainter
.
computeMaxIntrinsicWidth
(
text:
text
,
textDirection:
TextDirection
.
ltr
,
minWidth:
500
));
painter
.
dispose
();
});
}
class
MockCanvas
extends
Fake
implements
Canvas
{
...
...
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