Skip to content

Commit ce15e3b

Browse files
authored
added enabled to search anchor (flutter#153256)
Added the same parameter from flutter#137388 to the `SearchAnchor` This PR will fix: flutter#150331
1 parent d9dda9d commit ce15e3b

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class SearchAnchor extends StatefulWidget {
137137
required this.suggestionsBuilder,
138138
this.textInputAction,
139139
this.keyboardType,
140+
this.enabled = true,
140141
});
141142

142143
/// Create a [SearchAnchor] that has a [SearchBar] which opens a search view.
@@ -350,6 +351,13 @@ class SearchAnchor extends StatefulWidget {
350351
/// Defaults to the default value specified in [TextField].
351352
final TextInputType? keyboardType;
352353

354+
/// Whether or not this widget is currently interactive.
355+
///
356+
/// When false, the widget will ignore taps and appear dimmed.
357+
///
358+
/// Defaults to true.
359+
final bool enabled;
360+
353361
@override
354362
State<SearchAnchor> createState() => _SearchAnchorState();
355363
}
@@ -450,15 +458,25 @@ class _SearchAnchorState extends State<SearchAnchor> {
450458
};
451459
}
452460

461+
double _getOpacity() {
462+
if (widget.enabled) {
463+
return _anchorIsVisible ? 1.0 : 0.0;
464+
}
465+
return _kDisableSearchBarOpacity;
466+
}
467+
453468
@override
454469
Widget build(BuildContext context) {
455470
return AnimatedOpacity(
456471
key: _anchorKey,
457-
opacity: _anchorIsVisible ? 1.0 : 0.0,
472+
opacity: _getOpacity(),
458473
duration: _kAnchorFadeDuration,
459-
child: GestureDetector(
460-
onTap: _openView,
461-
child: widget.builder(context, _searchController),
474+
child: IgnorePointer(
475+
ignoring: !widget.enabled,
476+
child: GestureDetector(
477+
onTap: _openView,
478+
child: widget.builder(context, _searchController),
479+
),
462480
),
463481
);
464482
}
@@ -1317,7 +1335,11 @@ class SearchBar extends StatefulWidget {
13171335
/// {@macro flutter.widgets.editableText.textCapitalization}
13181336
final TextCapitalization? textCapitalization;
13191337

1320-
/// If false the text field is "disabled" so the SearchBar will ignore taps.
1338+
/// Whether or not this widget is currently interactive.
1339+
///
1340+
/// When false, the widget will ignore taps and appear dimmed.
1341+
///
1342+
/// Defaults to true.
13211343
final bool enabled;
13221344

13231345
/// {@macro flutter.widgets.editableText.autofocus}

packages/flutter/test/material/search_anchor_test.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,56 @@ void main() {
30163016
expect(opacityWidget.opacity, 0.38);
30173017
});
30183018

3019+
testWidgets('Check SearchAnchor opacity when disabled', (WidgetTester tester) async {
3020+
await tester.pumpWidget(MaterialApp(
3021+
home: Center(
3022+
child: Material(
3023+
child: SearchAnchor(
3024+
enabled: false,
3025+
builder: (BuildContext context, SearchController controller) {
3026+
return const Icon(Icons.search);
3027+
},
3028+
suggestionsBuilder: (BuildContext context, SearchController controller) {
3029+
return <Widget>[];
3030+
},
3031+
),
3032+
),
3033+
),
3034+
));
3035+
3036+
final Finder searchBarFinder = find.byType(SearchAnchor);
3037+
expect(searchBarFinder, findsOneWidget);
3038+
final Finder opacityFinder = find.descendant(
3039+
of: searchBarFinder,
3040+
matching: find.byType(AnimatedOpacity),
3041+
);
3042+
expect(opacityFinder, findsOneWidget);
3043+
final AnimatedOpacity opacityWidget = tester.widget<AnimatedOpacity>(opacityFinder);
3044+
expect(opacityWidget.opacity, 0.38);
3045+
});
3046+
3047+
testWidgets('SearchAnchor tap failed when disabled', (WidgetTester tester) async {
3048+
await tester.pumpWidget(MaterialApp(
3049+
home: Center(
3050+
child: Material(
3051+
child: SearchAnchor(
3052+
enabled: false,
3053+
builder: (BuildContext context, SearchController controller) {
3054+
return const Icon(Icons.search);
3055+
},
3056+
suggestionsBuilder: (BuildContext context, SearchController controller) {
3057+
return <Widget>[];
3058+
},
3059+
),
3060+
),
3061+
),
3062+
));
3063+
3064+
final Finder searchBarFinder = find.byType(SearchAnchor);
3065+
expect(searchBarFinder, findsOneWidget);
3066+
expect(searchBarFinder.hitTestable().tryEvaluate(), false);
3067+
});
3068+
30193069
testWidgets('SearchAnchor respects headerHeight', (WidgetTester tester) async {
30203070
await tester.pumpWidget(MaterialApp(
30213071
home: Center(

0 commit comments

Comments
 (0)