Skip to content

Commit 5140b0f

Browse files
authored
Add onSubmitted property to SearchBar (#129365)
Fixes #126551 This PR is to add `onSubmitted` property to `SearchBar`.
1 parent 042c036 commit 5140b0f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ class SearchBar extends StatefulWidget {
996996
this.trailing,
997997
this.onTap,
998998
this.onChanged,
999+
this.onSubmitted,
9991000
this.constraints,
10001001
this.elevation,
10011002
this.backgroundColor,
@@ -1044,6 +1045,10 @@ class SearchBar extends StatefulWidget {
10441045
/// Invoked upon user input.
10451046
final ValueChanged<String>? onChanged;
10461047

1048+
/// Called when the user indicates that they are done editing the text in the
1049+
/// field.
1050+
final ValueChanged<String>? onSubmitted;
1051+
10471052
/// Optional size constraints for the search bar.
10481053
///
10491054
/// If null, the value of [SearchBarThemeData.constraints] will be used. If
@@ -1236,6 +1241,7 @@ class _SearchBarState extends State<SearchBar> {
12361241
child: TextField(
12371242
focusNode: _focusNode,
12381243
onChanged: widget.onChanged,
1244+
onSubmitted: widget.onSubmitted,
12391245
controller: widget.controller,
12401246
style: effectiveTextStyle,
12411247
decoration: InputDecoration(

packages/flutter/test/material/search_anchor_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,26 @@ void main() {
270270
expect(changeCount, 2);
271271
});
272272

273+
testWidgets('SearchBar respects onSubmitted property', (WidgetTester tester) async {
274+
String submittedQuery = '';
275+
await tester.pumpWidget(
276+
MaterialApp(
277+
home: Material(
278+
child: SearchBar(
279+
onSubmitted: (String text) {
280+
submittedQuery = text;
281+
},
282+
),
283+
),
284+
),
285+
);
286+
287+
await tester.enterText(find.byType(SearchBar), 'query');
288+
await tester.testTextInput.receiveAction(TextInputAction.done);
289+
290+
expect(submittedQuery, equals('query'));
291+
});
292+
273293
testWidgets('SearchBar respects constraints property', (WidgetTester tester) async {
274294
const BoxConstraints constraints = BoxConstraints(maxWidth: 350.0, minHeight: 80);
275295
await tester.pumpWidget(

0 commit comments

Comments
 (0)