diff --git a/packages/flutter/lib/src/cupertino/search_field.dart b/packages/flutter/lib/src/cupertino/search_field.dart index 8434a7fc2af..b4b4b364394 100644 --- a/packages/flutter/lib/src/cupertino/search_field.dart +++ b/packages/flutter/lib/src/cupertino/search_field.dart @@ -116,6 +116,9 @@ class CupertinoSearchTextField extends StatefulWidget { /// default fields were determined using the comparison tool in /// https://github.com/flutter/platform_tests/. /// + /// To customize the prefix icon, pass a [Widget] to [prefixIcon]. This + /// defaults to the search icon. + /// /// To customize the suffix icon, pass an [Icon] to [suffixIcon]. This /// defaults to the X-Mark. /// @@ -141,6 +144,7 @@ class CupertinoSearchTextField extends StatefulWidget { this.itemColor = CupertinoColors.secondaryLabel, this.itemSize = 20.0, this.prefixInsets = const EdgeInsetsDirectional.fromSTEB(6, 0, 0, 4), + this.prefixIcon = const Icon(CupertinoIcons.search), this.suffixInsets = const EdgeInsetsDirectional.fromSTEB(0, 0, 5, 2), this.suffixIcon = const Icon(CupertinoIcons.xmark_circle_fill), this.suffixMode = OverlayVisibilityMode.editing, @@ -246,6 +250,11 @@ class CupertinoSearchTextField extends StatefulWidget { /// the comparison tool in https://github.com/flutter/platform_tests/. final EdgeInsetsGeometry prefixInsets; + /// Sets a prefix widget. + /// + /// Cannot be null. Defaults to an [Icon] widget with the [CupertinoIcons.search] icon. + final Widget prefixIcon; + /// Sets the padding insets for the prefix. /// /// Cannot be null. Defaults to padding that replicates the @@ -287,7 +296,7 @@ class CupertinoSearchTextField extends StatefulWidget { /// Disables the text field when false. /// /// Text fields in disabled states have a light grey background and don't - /// respond to touch events including the [suffixIcon] and the search button. + /// respond to touch events including the [prefixIcon] and [suffixIcon] button. final bool? enabled; @override @@ -387,7 +396,7 @@ class _CupertinoSearchTextFieldState extends State final Widget prefix = Padding( child: IconTheme( data: iconThemeData, - child: const Icon(CupertinoIcons.search), + child: widget.prefixIcon, ), padding: widget.prefixInsets, ); diff --git a/packages/flutter/test/cupertino/search_field_test.dart b/packages/flutter/test/cupertino/search_field_test.dart index 2a8b2bf31d3..e5907320faf 100644 --- a/packages/flutter/test/cupertino/search_field_test.dart +++ b/packages/flutter/test/cupertino/search_field_test.dart @@ -257,6 +257,35 @@ void main() { }, ); + testWidgets('prefix widget visibility', (WidgetTester tester) async { + const Key prefixIcon = Key('prefix'); + + await tester.pumpWidget( + const CupertinoApp( + home: Center( + child: CupertinoSearchTextField( + prefixIcon: SizedBox( + key: prefixIcon, + width: 50, + height: 50, + ), + ), + ), + ), + ); + + expect(find.byIcon(CupertinoIcons.search), findsNothing); + expect(find.byKey(prefixIcon), findsOneWidget); + + await tester.enterText( + find.byType(CupertinoSearchTextField), 'text input'); + await tester.pump(); + + expect(find.text('text input'), findsOneWidget); + expect(find.byIcon(CupertinoIcons.search), findsNothing); + expect(find.byKey(prefixIcon), findsOneWidget); + }); + testWidgets( 'suffix widget respects visibility mode', (WidgetTester tester) async {