Fix RenderEditable register the raw keyboard listener twice (#79877)
* Fix RenderEditable register the raw keyboard listener twice * skip web test
This commit is contained in:
parent
68492c5b69
commit
303ce76d85
@ -257,10 +257,10 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
|||||||
_obscureText = obscureText,
|
_obscureText = obscureText,
|
||||||
_readOnly = readOnly,
|
_readOnly = readOnly,
|
||||||
_forceLine = forceLine,
|
_forceLine = forceLine,
|
||||||
_clipBehavior = clipBehavior {
|
_clipBehavior = clipBehavior,
|
||||||
|
_hasFocus = hasFocus ?? false {
|
||||||
assert(_showCursor != null);
|
assert(_showCursor != null);
|
||||||
assert(!_showCursor.value || cursorColor != null);
|
assert(!_showCursor.value || cursorColor != null);
|
||||||
this.hasFocus = hasFocus ?? false;
|
|
||||||
|
|
||||||
_selectionPainter.highlightColor = selectionColor;
|
_selectionPainter.highlightColor = selectionColor;
|
||||||
_selectionPainter.highlightedRange = selection;
|
_selectionPainter.highlightedRange = selection;
|
||||||
@ -2016,6 +2016,13 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
|||||||
if (_hasFocus == value)
|
if (_hasFocus == value)
|
||||||
return;
|
return;
|
||||||
_hasFocus = value;
|
_hasFocus = value;
|
||||||
|
markNeedsSemanticsUpdate();
|
||||||
|
|
||||||
|
if (!attached) {
|
||||||
|
assert(!_listenerAttached);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (_hasFocus) {
|
if (_hasFocus) {
|
||||||
assert(!_listenerAttached);
|
assert(!_listenerAttached);
|
||||||
// TODO(justinmc): This listener should be ported to Actions and removed.
|
// TODO(justinmc): This listener should be ported to Actions and removed.
|
||||||
@ -2029,7 +2036,6 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
|||||||
RawKeyboard.instance.removeListener(_handleKeyEvent);
|
RawKeyboard.instance.removeListener(_handleKeyEvent);
|
||||||
_listenerAttached = false;
|
_listenerAttached = false;
|
||||||
}
|
}
|
||||||
markNeedsSemanticsUpdate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether this rendering object will take a full line regardless the text width.
|
/// Whether this rendering object will take a full line regardless the text width.
|
||||||
@ -2614,8 +2620,11 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
|||||||
_offset.addListener(markNeedsPaint);
|
_offset.addListener(markNeedsPaint);
|
||||||
_showHideCursor();
|
_showHideCursor();
|
||||||
_showCursor.addListener(_showHideCursor);
|
_showCursor.addListener(_showHideCursor);
|
||||||
if (_listenerAttached)
|
assert(!_listenerAttached);
|
||||||
|
if (_hasFocus) {
|
||||||
RawKeyboard.instance.addListener(_handleKeyEvent);
|
RawKeyboard.instance.addListener(_handleKeyEvent);
|
||||||
|
_listenerAttached = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -2626,8 +2635,10 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
|||||||
_showCursor.removeListener(_showHideCursor);
|
_showCursor.removeListener(_showHideCursor);
|
||||||
// TODO(justinmc): This listener should be ported to Actions and removed.
|
// TODO(justinmc): This listener should be ported to Actions and removed.
|
||||||
// https://github.com/flutter/flutter/issues/75004
|
// https://github.com/flutter/flutter/issues/75004
|
||||||
if (_listenerAttached)
|
if (_listenerAttached) {
|
||||||
RawKeyboard.instance.removeListener(_handleKeyEvent);
|
RawKeyboard.instance.removeListener(_handleKeyEvent);
|
||||||
|
_listenerAttached = false;
|
||||||
|
}
|
||||||
super.detach();
|
super.detach();
|
||||||
_foregroundRenderObject?.detach();
|
_foregroundRenderObject?.detach();
|
||||||
_backgroundRenderObject?.detach();
|
_backgroundRenderObject?.detach();
|
||||||
|
@ -1175,6 +1175,45 @@ void main() {
|
|||||||
expect(delegate.textEditingValue.text, 'W Sczebrzeszynie chrząszcz brzmi w trzcinie');
|
expect(delegate.textEditingValue.text, 'W Sczebrzeszynie chrząszcz brzmi w trzcinie');
|
||||||
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/61021
|
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/61021
|
||||||
|
|
||||||
|
test('RenderEditable registers and unregisters raw keyboard listener correctly', () async {
|
||||||
|
final TextSelectionDelegate delegate = FakeEditableTextState()
|
||||||
|
..textEditingValue = const TextEditingValue(
|
||||||
|
text: 'how are you',
|
||||||
|
selection: TextSelection.collapsed(offset: 0),
|
||||||
|
);
|
||||||
|
final ViewportOffset viewportOffset = ViewportOffset.zero();
|
||||||
|
final RenderEditable editable = RenderEditable(
|
||||||
|
backgroundCursorColor: Colors.grey,
|
||||||
|
selectionColor: Colors.black,
|
||||||
|
textDirection: TextDirection.ltr,
|
||||||
|
cursorColor: Colors.red,
|
||||||
|
offset: viewportOffset,
|
||||||
|
hasFocus: true,
|
||||||
|
textSelectionDelegate: delegate,
|
||||||
|
onSelectionChanged: (TextSelection selection, RenderEditable renderObject, SelectionChangedCause cause) {
|
||||||
|
renderObject.selection = selection;
|
||||||
|
},
|
||||||
|
startHandleLayerLink: LayerLink(),
|
||||||
|
endHandleLayerLink: LayerLink(),
|
||||||
|
text: const TextSpan(
|
||||||
|
text: 'how are you',
|
||||||
|
style: TextStyle(
|
||||||
|
height: 1.0, fontSize: 10.0, fontFamily: 'Ahem',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
selection: const TextSelection.collapsed(
|
||||||
|
offset: 0,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final PipelineOwner pipelineOwner = PipelineOwner();
|
||||||
|
editable.attach(pipelineOwner);
|
||||||
|
|
||||||
|
await simulateKeyDownEvent(LogicalKeyboardKey.delete, platform: 'android');
|
||||||
|
await simulateKeyUpEvent(LogicalKeyboardKey.delete, platform: 'android');
|
||||||
|
expect(delegate.textEditingValue.text, 'ow are you');
|
||||||
|
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/61021
|
||||||
|
|
||||||
test('arrow keys with selection text', () async {
|
test('arrow keys with selection text', () async {
|
||||||
const String text = '012345';
|
const String text = '012345';
|
||||||
final TextSelectionDelegate delegate = FakeEditableTextState()
|
final TextSelectionDelegate delegate = FakeEditableTextState()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user