diff --git a/packages/flutter/lib/src/material/search_anchor.dart b/packages/flutter/lib/src/material/search_anchor.dart index 89c69ffbd6..ed3b347ac1 100644 --- a/packages/flutter/lib/src/material/search_anchor.dart +++ b/packages/flutter/lib/src/material/search_anchor.dart @@ -996,6 +996,7 @@ class SearchBar extends StatefulWidget { this.trailing, this.onTap, this.onChanged, + this.onSubmitted, this.constraints, this.elevation, this.backgroundColor, @@ -1044,6 +1045,10 @@ class SearchBar extends StatefulWidget { /// Invoked upon user input. final ValueChanged? onChanged; + /// Called when the user indicates that they are done editing the text in the + /// field. + final ValueChanged? onSubmitted; + /// Optional size constraints for the search bar. /// /// If null, the value of [SearchBarThemeData.constraints] will be used. If @@ -1236,6 +1241,7 @@ class _SearchBarState extends State { child: TextField( focusNode: _focusNode, onChanged: widget.onChanged, + onSubmitted: widget.onSubmitted, controller: widget.controller, style: effectiveTextStyle, decoration: InputDecoration( diff --git a/packages/flutter/test/material/search_anchor_test.dart b/packages/flutter/test/material/search_anchor_test.dart index 622ddf27d1..b86f69a675 100644 --- a/packages/flutter/test/material/search_anchor_test.dart +++ b/packages/flutter/test/material/search_anchor_test.dart @@ -270,6 +270,26 @@ void main() { expect(changeCount, 2); }); + testWidgets('SearchBar respects onSubmitted property', (WidgetTester tester) async { + String submittedQuery = ''; + await tester.pumpWidget( + MaterialApp( + home: Material( + child: SearchBar( + onSubmitted: (String text) { + submittedQuery = text; + }, + ), + ), + ), + ); + + await tester.enterText(find.byType(SearchBar), 'query'); + await tester.testTextInput.receiveAction(TextInputAction.done); + + expect(submittedQuery, equals('query')); + }); + testWidgets('SearchBar respects constraints property', (WidgetTester tester) async { const BoxConstraints constraints = BoxConstraints(maxWidth: 350.0, minHeight: 80); await tester.pumpWidget(