diff --git a/packages/flutter/lib/src/material/selectable_text.dart b/packages/flutter/lib/src/material/selectable_text.dart index 31ef4ace27..ee00400766 100644 --- a/packages/flutter/lib/src/material/selectable_text.dart +++ b/packages/flutter/lib/src/material/selectable_text.dart @@ -32,7 +32,7 @@ class _TextSpanEditingController extends TextEditingController { final TextSpan _textSpan; @override - TextSpan buildTextSpan({TextStyle? style ,bool? withComposing}) { + TextSpan buildTextSpan({required BuildContext context, TextStyle? style, required bool withComposing}) { // This does not care about composing. return TextSpan( style: style, diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index c58d7552af..ffee8b248a 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -198,7 +198,7 @@ class TextEditingController extends ValueNotifier { /// /// By default makes text in composing range appear as underlined. Descendants /// can override this method to customize appearance of text. - TextSpan buildTextSpan({TextStyle? style , required bool withComposing}) { + TextSpan buildTextSpan({required BuildContext context, TextStyle? style , required bool withComposing}) { assert(!value.composing.isValid || !withComposing || value.isComposingRangeValid); // If the composing range is out of range for the current text, ignore it to // preserve the tree integrity, otherwise in release mode a RangeError will @@ -2680,6 +2680,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien } // Read only mode should not paint text composing. return widget.controller.buildTextSpan( + context: context, style: widget.style, withComposing: !widget.readOnly, ); diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 596529cc25..07aca67820 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -5810,6 +5810,25 @@ void main() { }), ); }); + + testWidgets('TextEditingController.buildTextSpan receives build context', (WidgetTester tester) async { + final _AccentColorTextEditingController controller = _AccentColorTextEditingController('a'); + const Color color = Color.fromARGB(255, 1, 2, 3); + await tester.pumpWidget(MaterialApp( + theme: ThemeData.light().copyWith(accentColor: color), + home: EditableText( + controller: controller, + focusNode: FocusNode(), + style: Typography.material2018(platform: TargetPlatform.android).black.subtitle1!, + cursorColor: Colors.blue, + backgroundCursorColor: Colors.grey, + ), + )); + + final RenderEditable renderEditable = findRenderEditable(tester); + final TextSpan textSpan = renderEditable.text!; + expect(textSpan.style!.color, color); + }); }); testWidgets('autofocus:true on first frame does not throw', (WidgetTester tester) async { @@ -7286,3 +7305,13 @@ class SkipPaintingRenderObject extends RenderProxyBox { @override void paint(PaintingContext context, Offset offset) { } } + +class _AccentColorTextEditingController extends TextEditingController { + _AccentColorTextEditingController(String text) : super(text: text); + + @override + TextSpan buildTextSpan({required BuildContext context, TextStyle? style, required bool withComposing}) { + final Color color = Theme.of(context).accentColor; + return super.buildTextSpan(context: context, style: TextStyle(color: color), withComposing: withComposing); + } +}