Fix issue 21640: Assertion Error : '_listenerAttached': is not true (#30513)

This commit is contained in:
chunhtai 2019-04-05 11:31:40 -07:00 committed by GitHub
parent 32f1b810ae
commit 2b7e98973a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View file

@ -196,7 +196,6 @@ class RenderEditable extends RenderBox {
_cursorColor = cursorColor,
_backgroundCursorColor = backgroundCursorColor,
_showCursor = showCursor ?? ValueNotifier<bool>(false),
_hasFocus = hasFocus ?? false,
_maxLines = maxLines,
_minLines = minLines,
_expands = expands,
@ -213,6 +212,7 @@ class RenderEditable extends RenderBox {
_obscureText = obscureText {
assert(_showCursor != null);
assert(!_showCursor.value || cursorColor != null);
this.hasFocus = hasFocus ?? false;
_tap = TapGestureRecognizer(debugOwner: this)
..onTapDown = _handleTapDown
..onTap = _handleTap;
@ -707,7 +707,7 @@ class RenderEditable extends RenderBox {
/// Whether the editable is currently focused.
bool get hasFocus => _hasFocus;
bool _hasFocus;
bool _hasFocus = false;
bool _listenerAttached = false;
set hasFocus(bool value) {
assert(value != null);
@ -723,7 +723,6 @@ class RenderEditable extends RenderBox {
RawKeyboard.instance.removeListener(_handleKeyEvent);
_listenerAttached = false;
}
markNeedsSemanticsUpdate();
}

View file

@ -422,4 +422,25 @@ void main() {
expect(updatedSelection.extentOffset, 5);
expect(selectionChangedCount, 1);
});
test('editable hasFocus correctly initialized', () {
// Regression test for https://github.com/flutter/flutter/issues/21640
final TextSelectionDelegate delegate = FakeEditableTextState();
final RenderEditable editable = RenderEditable(
text: const TextSpan(
style: TextStyle(height: 1.0, fontSize: 10.0, fontFamily: 'Ahem'),
text: '12345',
),
textAlign: TextAlign.start,
textDirection: TextDirection.ltr,
locale: const Locale('en', 'US'),
offset: ViewportOffset.zero(),
textSelectionDelegate: delegate,
hasFocus: true,
);
expect(editable.hasFocus, true);
editable.hasFocus = false;
expect(editable.hasFocus, false);
});
}