From 1f4a6dc0f048cd8cc8c8e39eba248c4fe357ac5c Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Wed, 1 Jul 2026 15:26:24 -0300 Subject: [PATCH] fix: Workaround for the TextBoxComponent entering an endless loop --- .../lib/src/components/text_box_component.dart | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/flame/lib/src/components/text_box_component.dart b/packages/flame/lib/src/components/text_box_component.dart index 0450f48df49..519f0003083 100644 --- a/packages/flame/lib/src/components/text_box_component.dart +++ b/packages/flame/lib/src/components/text_box_component.dart @@ -208,7 +208,23 @@ class TextBoxComponent extends TextComponent { var offset = 0; while (offset < text.length) { - final range = textPainter.getLineBoundary(TextPosition(offset: offset)); + var range = textPainter.getLineBoundary(TextPosition(offset: offset)); + // Flutter web sometimes report the range on the previous page instead of + // the following. This is probably a bug cause by the + // JS floating number inaccuracies. + // + // We have an issue tracking that down on Flutter itself: + // https://github.com/flutter/flutter/issues/188874 + // + // This code is a workaround, that captures when such we fall + // in that issue and tries to get the next page. + if (range.start < offset) { + range = textPainter.getLineBoundary( + TextPosition( + offset: offset + 1, + ), + ); + } var lineText = text.substring(range.start, range.end); // Don't include the trailing newline character in the line text