@@ -217,6 +217,86 @@ class TextPainter {
217217 _textWidthBasis = textWidthBasis,
218218 _textHeightBehavior = textHeightBehavior;
219219
220+ /// Computes the width of a configured [TextPainter] .
221+ ///
222+ /// This is a convenience method that creates a text painter with the supplied
223+ /// parameters, lays it out with the supplied [minWidth] and [maxWidth] , and
224+ /// returns its [TextPainter.width] making sure to dispose the underlying
225+ /// resources.
226+ static double computeWidth ({
227+ required InlineSpan text,
228+ TextAlign textAlign = TextAlign .start,
229+ TextDirection ? textDirection,
230+ double textScaleFactor = 1.0 ,
231+ int ? maxLines,
232+ String ? ellipsis,
233+ Locale ? locale,
234+ StrutStyle ? strutStyle,
235+ TextWidthBasis textWidthBasis = TextWidthBasis .parent,
236+ ui.TextHeightBehavior ? textHeightBehavior,
237+ double minWidth = 0.0 ,
238+ double maxWidth = double .infinity,
239+ }) {
240+ final TextPainter painter = TextPainter (
241+ text: text,
242+ textAlign: textAlign,
243+ textDirection: textDirection,
244+ textScaleFactor: textScaleFactor,
245+ maxLines: maxLines,
246+ ellipsis: ellipsis,
247+ locale: locale,
248+ strutStyle: strutStyle,
249+ textWidthBasis: textWidthBasis,
250+ textHeightBehavior: textHeightBehavior,
251+ )..layout (minWidth: minWidth, maxWidth: maxWidth);
252+
253+ try {
254+ return painter.width;
255+ } finally {
256+ painter.dispose ();
257+ }
258+ }
259+
260+ /// Computes the max intrinsic width of a configured [TextPainter] .
261+ ///
262+ /// This is a convenience method that creates a text painter with the supplied
263+ /// parameters, lays it out with the supplied [minWidth] and [maxWidth] , and
264+ /// returns its [TextPainter.maxIntrinsicWidth] making sure to dispose the
265+ /// underlying resources.
266+ static double computeMaxIntrinsicWidth ({
267+ required InlineSpan text,
268+ TextAlign textAlign = TextAlign .start,
269+ TextDirection ? textDirection,
270+ double textScaleFactor = 1.0 ,
271+ int ? maxLines,
272+ String ? ellipsis,
273+ Locale ? locale,
274+ StrutStyle ? strutStyle,
275+ TextWidthBasis textWidthBasis = TextWidthBasis .parent,
276+ ui.TextHeightBehavior ? textHeightBehavior,
277+ double minWidth = 0.0 ,
278+ double maxWidth = double .infinity,
279+ }) {
280+ final TextPainter painter = TextPainter (
281+ text: text,
282+ textAlign: textAlign,
283+ textDirection: textDirection,
284+ textScaleFactor: textScaleFactor,
285+ maxLines: maxLines,
286+ ellipsis: ellipsis,
287+ locale: locale,
288+ strutStyle: strutStyle,
289+ textWidthBasis: textWidthBasis,
290+ textHeightBehavior: textHeightBehavior,
291+ )..layout (minWidth: minWidth, maxWidth: maxWidth);
292+
293+ try {
294+ return painter.maxIntrinsicWidth;
295+ } finally {
296+ painter.dispose ();
297+ }
298+ }
299+
220300 // _paragraph being null means the text needs layout because of style changes.
221301 // Setting _paragraph to null invalidates all the layout cache.
222302 //
0 commit comments