From 5140b0f0c60ad17c8267b15b0521419f87d7bf35 Mon Sep 17 00:00:00 2001 From: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com> Date: Thu, 22 Jun 2023 21:19:01 +0000 Subject: [PATCH] Add `onSubmitted` property to `SearchBar` (#129365) Fixes #126551 This PR is to add `onSubmitted` property to `SearchBar`. --- .../lib/src/material/search_anchor.dart | 6 ++++++ .../test/material/search_anchor_test.dart | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+) 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(