Unverified Commit 28f17d34 authored by Dan Field's avatar Dan Field Committed by GitHub

Make EditableText respect MediaQuery.boldTextOf (#122754)

parent 6b7c60d6
...@@ -677,9 +677,6 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio ...@@ -677,9 +677,6 @@ class _SelectableTextState extends State<SelectableText> implements TextSelectio
if (effectiveTextStyle == null || effectiveTextStyle.inherit) { if (effectiveTextStyle == null || effectiveTextStyle.inherit) {
effectiveTextStyle = defaultTextStyle.style.merge(widget.style ?? _controller._textSpan.style); effectiveTextStyle = defaultTextStyle.style.merge(widget.style ?? _controller._textSpan.style);
} }
if (MediaQuery.boldTextOf(context)) {
effectiveTextStyle = effectiveTextStyle.merge(const TextStyle(fontWeight: FontWeight.bold));
}
final Widget child = RepaintBoundary( final Widget child = RepaintBoundary(
child: EditableText( child: EditableText(
key: editableTextKey, key: editableTextKey,
......
...@@ -2084,6 +2084,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2084,6 +2084,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
AutofillClient get _effectiveAutofillClient => widget.autofillClient ?? this; AutofillClient get _effectiveAutofillClient => widget.autofillClient ?? this;
late SpellCheckConfiguration _spellCheckConfiguration; late SpellCheckConfiguration _spellCheckConfiguration;
late TextStyle _style;
/// Configuration that determines how spell check will be performed. /// Configuration that determines how spell check will be performed.
/// ///
...@@ -2589,6 +2590,10 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2589,6 +2590,10 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
void didChangeDependencies() { void didChangeDependencies() {
super.didChangeDependencies(); super.didChangeDependencies();
_style = MediaQuery.boldTextOf(context)
? widget.style.merge(const TextStyle(fontWeight: FontWeight.bold))
: widget.style;
final AutofillGroupState? newAutofillGroup = AutofillGroup.maybeOf(context); final AutofillGroupState? newAutofillGroup = AutofillGroup.maybeOf(context);
if (currentAutofillScope != newAutofillGroup) { if (currentAutofillScope != newAutofillGroup) {
_currentAutofillScope?.unregister(autofillId); _currentAutofillScope?.unregister(autofillId);
...@@ -2682,14 +2687,16 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -2682,14 +2687,16 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
} }
if (widget.style != oldWidget.style) { if (widget.style != oldWidget.style) {
final TextStyle style = widget.style;
// The _textInputConnection will pick up the new style when it attaches in // The _textInputConnection will pick up the new style when it attaches in
// _openInputConnection. // _openInputConnection.
_style = MediaQuery.boldTextOf(context)
? widget.style.merge(const TextStyle(fontWeight: FontWeight.bold))
: widget.style;
if (_hasInputConnection) { if (_hasInputConnection) {
_textInputConnection!.setStyle( _textInputConnection!.setStyle(
fontFamily: style.fontFamily, fontFamily: _style.fontFamily,
fontSize: style.fontSize, fontSize: _style.fontSize,
fontWeight: style.fontWeight, fontWeight: _style.fontWeight,
textDirection: _textDirection, textDirection: _textDirection,
textAlign: widget.textAlign, textAlign: widget.textAlign,
); );
...@@ -3154,12 +3161,11 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -3154,12 +3161,11 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
: TextInput.attach(this, _effectiveAutofillClient.textInputConfiguration); : TextInput.attach(this, _effectiveAutofillClient.textInputConfiguration);
_updateSizeAndTransform(); _updateSizeAndTransform();
_schedulePeriodicPostFrameCallbacks(); _schedulePeriodicPostFrameCallbacks();
final TextStyle style = widget.style;
_textInputConnection! _textInputConnection!
..setStyle( ..setStyle(
fontFamily: style.fontFamily, fontFamily: _style.fontFamily,
fontSize: style.fontSize, fontSize: _style.fontSize,
fontWeight: style.fontWeight, fontWeight: _style.fontWeight,
textDirection: _textDirection, textDirection: _textDirection,
textAlign: widget.textAlign, textAlign: widget.textAlign,
) )
...@@ -3222,13 +3228,12 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -3222,13 +3228,12 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
?? TextInput.attach(this, _effectiveAutofillClient.textInputConfiguration); ?? TextInput.attach(this, _effectiveAutofillClient.textInputConfiguration);
_textInputConnection = newConnection; _textInputConnection = newConnection;
final TextStyle style = widget.style;
newConnection newConnection
..show() ..show()
..setStyle( ..setStyle(
fontFamily: style.fontFamily, fontFamily: _style.fontFamily,
fontSize: style.fontSize, fontSize: _style.fontSize,
fontWeight: style.fontWeight, fontWeight: _style.fontWeight,
textDirection: _textDirection, textDirection: _textDirection,
textAlign: widget.textAlign, textAlign: widget.textAlign,
) )
...@@ -4636,6 +4641,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -4636,6 +4641,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
/// By default makes text in composing range appear as underlined. /// By default makes text in composing range appear as underlined.
/// Descendants can override this method to customize appearance of text. /// Descendants can override this method to customize appearance of text.
TextSpan buildTextSpan() { TextSpan buildTextSpan() {
if (widget.obscureText) { if (widget.obscureText) {
String text = _value.text; String text = _value.text;
text = widget.obscuringCharacter * text.length; text = widget.obscuringCharacter * text.length;
...@@ -4653,7 +4659,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -4653,7 +4659,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
text = text.replaceRange(o, o + 1, _value.text.substring(o, o + 1)); text = text.replaceRange(o, o + 1, _value.text.substring(o, o + 1));
} }
} }
return TextSpan(style: widget.style, text: text); return TextSpan(style: _style, text: text);
} }
if (_placeholderLocation >= 0 && _placeholderLocation <= _value.text.length) { if (_placeholderLocation >= 0 && _placeholderLocation <= _value.text.length) {
final List<_ScribblePlaceholder> placeholders = <_ScribblePlaceholder>[]; final List<_ScribblePlaceholder> placeholders = <_ScribblePlaceholder>[];
...@@ -4665,7 +4671,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -4665,7 +4671,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
} else { } else {
placeholders.add(const _ScribblePlaceholder(child: SizedBox.shrink(), size: Size(100.0, 0.0))); placeholders.add(const _ScribblePlaceholder(child: SizedBox.shrink(), size: Size(100.0, 0.0)));
} }
return TextSpan(style: widget.style, children: <InlineSpan>[ return TextSpan(style: _style, children: <InlineSpan>[
TextSpan(text: _value.text.substring(0, placeholderLocation)), TextSpan(text: _value.text.substring(0, placeholderLocation)),
...placeholders, ...placeholders,
TextSpan(text: _value.text.substring(placeholderLocation)), TextSpan(text: _value.text.substring(placeholderLocation)),
...@@ -4684,7 +4690,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -4684,7 +4690,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
return buildTextSpanWithSpellCheckSuggestions( return buildTextSpanWithSpellCheckSuggestions(
_value, _value,
composingRegionOutOfRange, composingRegionOutOfRange,
widget.style, _style,
_spellCheckConfiguration.misspelledTextStyle!, _spellCheckConfiguration.misspelledTextStyle!,
spellCheckResults!, spellCheckResults!,
); );
...@@ -4693,7 +4699,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien ...@@ -4693,7 +4699,7 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
// Read only mode should not paint text composing. // Read only mode should not paint text composing.
return widget.controller.buildTextSpan( return widget.controller.buildTextSpan(
context: context, context: context,
style: widget.style, style: _style,
withComposing: withComposing, withComposing: withComposing,
); );
} }
......
...@@ -5374,6 +5374,46 @@ void main() { ...@@ -5374,6 +5374,46 @@ void main() {
); );
}); });
testWidgets('text styling info is sent on show keyboard (bold override)', (WidgetTester tester) async {
final List<MethodCall> log = <MethodCall>[];
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.textInput, (MethodCall methodCall) async {
log.add(methodCall);
return null;
});
final TextEditingController controller = TextEditingController();
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(boldText: true),
child: EditableText(
textDirection: TextDirection.rtl,
controller: controller,
focusNode: FocusNode(),
style: const TextStyle(
fontSize: 20.0,
fontFamily: 'Roboto',
fontWeight: FontWeight.w600,
),
cursorColor: Colors.blue,
backgroundCursorColor: Colors.grey,
),
),
);
await tester.showKeyboard(find.byType(EditableText));
final MethodCall setStyle = log.firstWhere((MethodCall m) => m.method == 'TextInput.setStyle');
expect(
setStyle,
isMethodCall('TextInput.setStyle', arguments: <String, dynamic>{
'fontSize': 20.0,
'fontFamily': 'Roboto',
'fontWeightIndex': FontWeight.bold.index,
'textAlignIndex': 4,
'textDirectionIndex': 0,
}),
);
});
testWidgets('text styling info is sent on style update', (WidgetTester tester) async { testWidgets('text styling info is sent on style update', (WidgetTester tester) async {
final GlobalKey<EditableTextState> editableTextKey = GlobalKey<EditableTextState>(); final GlobalKey<EditableTextState> editableTextKey = GlobalKey<EditableTextState>();
late StateSetter setState; late StateSetter setState;
...@@ -15519,6 +15559,27 @@ testWidgets('Floating cursor ending with selection', (WidgetTester tester) async ...@@ -15519,6 +15559,27 @@ testWidgets('Floating cursor ending with selection', (WidgetTester tester) async
skip: !kIsWeb, // [intended] skip: !kIsWeb, // [intended]
); );
}); });
testWidgets('EditableText respects MediaQuery.boldText', (WidgetTester tester) async {
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: const MediaQueryData(boldText: true),
child: EditableText(
controller: controller,
focusNode: focusNode,
style: const TextStyle(fontWeight: FontWeight.normal),
cursorColor: Colors.red,
backgroundCursorColor: Colors.green,
),
),
));
controller.text = 'foo';
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
expect(state.buildTextSpan().style!.fontWeight, FontWeight.bold);
});
} }
class UnsettableController extends TextEditingController { class UnsettableController extends TextEditingController {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment