Unverified Commit 7d794725 authored by Kostia Sokolovskyi's avatar Kostia Sokolovskyi Committed by GitHub

SearchAnchor should dispose created FocusNode and SearchController. (#136120)

parent e0b8ec07
...@@ -310,15 +310,12 @@ class _SearchAnchorState extends State<SearchAnchor> { ...@@ -310,15 +310,12 @@ class _SearchAnchorState extends State<SearchAnchor> {
bool _anchorIsVisible = true; bool _anchorIsVisible = true;
final GlobalKey _anchorKey = GlobalKey(); final GlobalKey _anchorKey = GlobalKey();
bool get _viewIsOpen => !_anchorIsVisible; bool get _viewIsOpen => !_anchorIsVisible;
late SearchController? _internalSearchController; SearchController? _internalSearchController;
SearchController get _searchController => widget.searchController ?? _internalSearchController!; SearchController get _searchController => widget.searchController ?? (_internalSearchController ??= SearchController());
@override @override
void initState() { void initState() {
super.initState(); super.initState();
if (widget.searchController == null) {
_internalSearchController = SearchController();
}
_searchController._attach(this); _searchController._attach(this);
} }
...@@ -334,11 +331,21 @@ class _SearchAnchorState extends State<SearchAnchor> { ...@@ -334,11 +331,21 @@ class _SearchAnchorState extends State<SearchAnchor> {
_screenSize = updatedScreenSize; _screenSize = updatedScreenSize;
} }
@override
void didUpdateWidget(SearchAnchor oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.searchController != widget.searchController) {
oldWidget.searchController?._detach(this);
_searchController._attach(this);
}
}
@override @override
void dispose() { void dispose() {
super.dispose(); super.dispose();
_searchController._detach(this); widget.searchController?._detach(this);
_internalSearchController = null; _internalSearchController?._detach(this);
_internalSearchController?.dispose();
} }
void _openView() { void _openView() {
...@@ -680,12 +687,6 @@ class _ViewContentState extends State<_ViewContent> { ...@@ -680,12 +687,6 @@ class _ViewContentState extends State<_ViewContent> {
} }
} }
@override
void dispose(){
_controller.removeListener(updateSuggestions);
super.dispose();
}
@override @override
void didUpdateWidget(covariant _ViewContent oldWidget) { void didUpdateWidget(covariant _ViewContent oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
...@@ -710,6 +711,13 @@ class _ViewContentState extends State<_ViewContent> { ...@@ -710,6 +711,13 @@ class _ViewContentState extends State<_ViewContent> {
unawaited(updateSuggestions()); unawaited(updateSuggestions());
} }
@override
void dispose() {
_controller.removeListener(updateSuggestions);
_focusNode.dispose();
super.dispose();
}
Widget viewBuilder(Iterable<Widget> suggestions) { Widget viewBuilder(Iterable<Widget> suggestions) {
if (widget.viewBuilder == null) { if (widget.viewBuilder == null) {
return MediaQuery.removePadding( return MediaQuery.removePadding(
...@@ -949,15 +957,18 @@ class SearchController extends TextEditingController { ...@@ -949,15 +957,18 @@ class SearchController extends TextEditingController {
// it controls. // it controls.
_SearchAnchorState? _anchor; _SearchAnchorState? _anchor;
/// Whether this controller has associated search anchor.
bool get isAttached => _anchor != null;
/// Whether or not the associated search view is currently open. /// Whether or not the associated search view is currently open.
bool get isOpen { bool get isOpen {
assert(_anchor != null); assert(isAttached);
return _anchor!._viewIsOpen; return _anchor!._viewIsOpen;
} }
/// Opens the search view that this controller is associated with. /// Opens the search view that this controller is associated with.
void openView() { void openView() {
assert(_anchor != null); assert(isAttached);
_anchor!._openView(); _anchor!._openView();
} }
...@@ -966,7 +977,7 @@ class SearchController extends TextEditingController { ...@@ -966,7 +977,7 @@ class SearchController extends TextEditingController {
/// If `selectedText` is given, then the text value of the controller is set to /// If `selectedText` is given, then the text value of the controller is set to
/// `selectedText`. /// `selectedText`.
void closeView(String? selectedText) { void closeView(String? selectedText) {
assert(_anchor != null); assert(isAttached);
_anchor!._closeView(selectedText); _anchor!._closeView(selectedText);
} }
...@@ -1166,7 +1177,8 @@ class SearchBar extends StatefulWidget { ...@@ -1166,7 +1177,8 @@ class SearchBar extends StatefulWidget {
class _SearchBarState extends State<SearchBar> { class _SearchBarState extends State<SearchBar> {
late final MaterialStatesController _internalStatesController; late final MaterialStatesController _internalStatesController;
late final FocusNode _focusNode; FocusNode? _internalFocusNode;
FocusNode get _focusNode => widget.focusNode ?? (_internalFocusNode ??= FocusNode());
@override @override
void initState() { void initState() {
...@@ -1175,15 +1187,12 @@ class _SearchBarState extends State<SearchBar> { ...@@ -1175,15 +1187,12 @@ class _SearchBarState extends State<SearchBar> {
_internalStatesController.addListener(() { _internalStatesController.addListener(() {
setState(() {}); setState(() {});
}); });
_focusNode = widget.focusNode ?? FocusNode();
} }
@override @override
void dispose() { void dispose() {
_internalStatesController.dispose(); _internalStatesController.dispose();
if (widget.focusNode == null) { _internalFocusNode?.dispose();
_focusNode.dispose();
}
super.dispose(); super.dispose();
} }
......
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