Skip to content

Commit 7d79472

Browse files
authored
SearchAnchor should dispose created FocusNode and SearchController. (#136120)
1 parent e0b8ec0 commit 7d79472

File tree

2 files changed

+245
-82
lines changed

2 files changed

+245
-82
lines changed

packages/flutter/lib/src/material/search_anchor.dart

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,12 @@ class _SearchAnchorState extends State<SearchAnchor> {
310310
bool _anchorIsVisible = true;
311311
final GlobalKey _anchorKey = GlobalKey();
312312
bool get _viewIsOpen => !_anchorIsVisible;
313-
late SearchController? _internalSearchController;
314-
SearchController get _searchController => widget.searchController ?? _internalSearchController!;
313+
SearchController? _internalSearchController;
314+
SearchController get _searchController => widget.searchController ?? (_internalSearchController ??= SearchController());
315315

316316
@override
317317
void initState() {
318318
super.initState();
319-
if (widget.searchController == null) {
320-
_internalSearchController = SearchController();
321-
}
322319
_searchController._attach(this);
323320
}
324321

@@ -334,11 +331,21 @@ class _SearchAnchorState extends State<SearchAnchor> {
334331
_screenSize = updatedScreenSize;
335332
}
336333

334+
@override
335+
void didUpdateWidget(SearchAnchor oldWidget) {
336+
super.didUpdateWidget(oldWidget);
337+
if (oldWidget.searchController != widget.searchController) {
338+
oldWidget.searchController?._detach(this);
339+
_searchController._attach(this);
340+
}
341+
}
342+
337343
@override
338344
void dispose() {
339345
super.dispose();
340-
_searchController._detach(this);
341-
_internalSearchController = null;
346+
widget.searchController?._detach(this);
347+
_internalSearchController?._detach(this);
348+
_internalSearchController?.dispose();
342349
}
343350

344351
void _openView() {
@@ -680,12 +687,6 @@ class _ViewContentState extends State<_ViewContent> {
680687
}
681688
}
682689

683-
@override
684-
void dispose(){
685-
_controller.removeListener(updateSuggestions);
686-
super.dispose();
687-
}
688-
689690
@override
690691
void didUpdateWidget(covariant _ViewContent oldWidget) {
691692
super.didUpdateWidget(oldWidget);
@@ -710,6 +711,13 @@ class _ViewContentState extends State<_ViewContent> {
710711
unawaited(updateSuggestions());
711712
}
712713

714+
@override
715+
void dispose() {
716+
_controller.removeListener(updateSuggestions);
717+
_focusNode.dispose();
718+
super.dispose();
719+
}
720+
713721
Widget viewBuilder(Iterable<Widget> suggestions) {
714722
if (widget.viewBuilder == null) {
715723
return MediaQuery.removePadding(
@@ -949,15 +957,18 @@ class SearchController extends TextEditingController {
949957
// it controls.
950958
_SearchAnchorState? _anchor;
951959

960+
/// Whether this controller has associated search anchor.
961+
bool get isAttached => _anchor != null;
962+
952963
/// Whether or not the associated search view is currently open.
953964
bool get isOpen {
954-
assert(_anchor != null);
965+
assert(isAttached);
955966
return _anchor!._viewIsOpen;
956967
}
957968

958969
/// Opens the search view that this controller is associated with.
959970
void openView() {
960-
assert(_anchor != null);
971+
assert(isAttached);
961972
_anchor!._openView();
962973
}
963974

@@ -966,7 +977,7 @@ class SearchController extends TextEditingController {
966977
/// If `selectedText` is given, then the text value of the controller is set to
967978
/// `selectedText`.
968979
void closeView(String? selectedText) {
969-
assert(_anchor != null);
980+
assert(isAttached);
970981
_anchor!._closeView(selectedText);
971982
}
972983

@@ -1166,7 +1177,8 @@ class SearchBar extends StatefulWidget {
11661177

11671178
class _SearchBarState extends State<SearchBar> {
11681179
late final MaterialStatesController _internalStatesController;
1169-
late final FocusNode _focusNode;
1180+
FocusNode? _internalFocusNode;
1181+
FocusNode get _focusNode => widget.focusNode ?? (_internalFocusNode ??= FocusNode());
11701182

11711183
@override
11721184
void initState() {
@@ -1175,15 +1187,12 @@ class _SearchBarState extends State<SearchBar> {
11751187
_internalStatesController.addListener(() {
11761188
setState(() {});
11771189
});
1178-
_focusNode = widget.focusNode ?? FocusNode();
11791190
}
11801191

11811192
@override
11821193
void dispose() {
11831194
_internalStatesController.dispose();
1184-
if (widget.focusNode == null) {
1185-
_focusNode.dispose();
1186-
}
1195+
_internalFocusNode?.dispose();
11871196
super.dispose();
11881197
}
11891198

0 commit comments

Comments
 (0)