Unverified Commit 5140b0f0 authored by Qun Cheng's avatar Qun Cheng Committed by GitHub

Add `onSubmitted` property to `SearchBar` (#129365)

Fixes #126551

This PR is to add `onSubmitted` property to `SearchBar`.
parent 042c0366
...@@ -996,6 +996,7 @@ class SearchBar extends StatefulWidget { ...@@ -996,6 +996,7 @@ class SearchBar extends StatefulWidget {
this.trailing, this.trailing,
this.onTap, this.onTap,
this.onChanged, this.onChanged,
this.onSubmitted,
this.constraints, this.constraints,
this.elevation, this.elevation,
this.backgroundColor, this.backgroundColor,
...@@ -1044,6 +1045,10 @@ class SearchBar extends StatefulWidget { ...@@ -1044,6 +1045,10 @@ class SearchBar extends StatefulWidget {
/// Invoked upon user input. /// Invoked upon user input.
final ValueChanged<String>? onChanged; final ValueChanged<String>? onChanged;
/// Called when the user indicates that they are done editing the text in the
/// field.
final ValueChanged<String>? onSubmitted;
/// Optional size constraints for the search bar. /// Optional size constraints for the search bar.
/// ///
/// If null, the value of [SearchBarThemeData.constraints] will be used. If /// If null, the value of [SearchBarThemeData.constraints] will be used. If
...@@ -1236,6 +1241,7 @@ class _SearchBarState extends State<SearchBar> { ...@@ -1236,6 +1241,7 @@ class _SearchBarState extends State<SearchBar> {
child: TextField( child: TextField(
focusNode: _focusNode, focusNode: _focusNode,
onChanged: widget.onChanged, onChanged: widget.onChanged,
onSubmitted: widget.onSubmitted,
controller: widget.controller, controller: widget.controller,
style: effectiveTextStyle, style: effectiveTextStyle,
decoration: InputDecoration( decoration: InputDecoration(
......
...@@ -270,6 +270,26 @@ void main() { ...@@ -270,6 +270,26 @@ void main() {
expect(changeCount, 2); 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 { testWidgets('SearchBar respects constraints property', (WidgetTester tester) async {
const BoxConstraints constraints = BoxConstraints(maxWidth: 350.0, minHeight: 80); const BoxConstraints constraints = BoxConstraints(maxWidth: 350.0, minHeight: 80);
await tester.pumpWidget( await tester.pumpWidget(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment