diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index eb0190bae5..78a5ecc9a3 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -196,7 +196,6 @@ class RenderEditable extends RenderBox { _cursorColor = cursorColor, _backgroundCursorColor = backgroundCursorColor, _showCursor = showCursor ?? ValueNotifier(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(); } diff --git a/packages/flutter/test/rendering/editable_test.dart b/packages/flutter/test/rendering/editable_test.dart index 63b6368d92..d5e28bc89e 100644 --- a/packages/flutter/test/rendering/editable_test.dart +++ b/packages/flutter/test/rendering/editable_test.dart @@ -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); + }); }