Skip to content

Commit 72e00eb

Browse files
committed
[Material] Fix SearchAnchor.bar ignoring SearchBarThemeData.padding
When barPadding is not explicitly provided, SearchAnchor.bar was falling back to a hardcoded EdgeInsets.symmetric(horizontal: 16.0) which bypassed SearchBarThemeData.padding entirely. Fix uses a 3-tier resolution: barPadding → SearchBarThemeData.padding → original 16.0 default, preserving backward compatibility while respecting the theme. Fixes flutter#185889
1 parent cf9e8af commit 72e00eb

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,7 @@ class _SearchAnchorWithSearchBar extends SearchAnchor {
12761276
viewOnClose: onClose,
12771277
viewOnOpen: onOpen,
12781278
builder: (BuildContext context, SearchController controller) {
1279+
final SearchBarThemeData searchBarTheme = SearchBarTheme.of(context);
12791280
return SearchBar(
12801281
constraints: constraints,
12811282
controller: controller,
@@ -1295,8 +1296,8 @@ class _SearchAnchorWithSearchBar extends SearchAnchor {
12951296
overlayColor: barOverlayColor,
12961297
side: barSide,
12971298
shape: barShape,
1298-
padding:
1299-
barPadding ??
1299+
padding: barPadding ??
1300+
searchBarTheme.padding ??
13001301
const MaterialStatePropertyAll<EdgeInsets>(EdgeInsets.symmetric(horizontal: 16.0)),
13011302
leading: barLeading ?? const Icon(Icons.search),
13021303
trailing: barTrailing,

‎packages/flutter/test/material/search_anchor_test.dart‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,69 @@ void main() {
701701
expect(trailingRect.right, barRect.right - 16.0);
702702
});
703703

704+
testWidgets('SearchAnchor.bar respects SearchBarThemeData.padding', (WidgetTester tester) async {
705+
await tester.pumpWidget(
706+
MaterialApp(
707+
home: Center(
708+
child: Material(
709+
child: SearchBarTheme(
710+
data: const SearchBarThemeData(
711+
padding: MaterialStatePropertyAll<EdgeInsets>(EdgeInsets.all(30.0)),
712+
),
713+
child: SearchAnchor.bar(
714+
suggestionsBuilder: (BuildContext context, SearchController controller) {
715+
return <Widget>[];
716+
},
717+
),
718+
),
719+
),
720+
),
721+
),
722+
);
723+
724+
final Rect barRect = tester.getRect(find.byType(SearchBar));
725+
final Rect leadingRect = tester.getRect(find.byIcon(Icons.search));
726+
final Rect textFieldRect = tester.getRect(find.byType(TextField));
727+
728+
// outer padding (30) separates bar edge from leading icon
729+
expect(barRect.left, leadingRect.left - 30.0);
730+
// inner padding (30) separates leading icon from text field
731+
expect(leadingRect.right, textFieldRect.left - 30.0);
732+
// right side: outer (30) + inner (30) = 60 between text field right and bar right
733+
expect(barRect.right, textFieldRect.right + 60.0);
734+
});
735+
736+
testWidgets('SearchAnchor.bar.barPadding overrides SearchBarThemeData.padding', (WidgetTester tester) async {
737+
await tester.pumpWidget(
738+
MaterialApp(
739+
home: Center(
740+
child: Material(
741+
child: SearchBarTheme(
742+
data: const SearchBarThemeData(
743+
padding: MaterialStatePropertyAll<EdgeInsets>(EdgeInsets.all(30.0)),
744+
),
745+
child: SearchAnchor.bar(
746+
barPadding: const MaterialStatePropertyAll<EdgeInsets>(EdgeInsets.all(10.0)),
747+
suggestionsBuilder: (BuildContext context, SearchController controller) {
748+
return <Widget>[];
749+
},
750+
),
751+
),
752+
),
753+
),
754+
),
755+
);
756+
757+
final Rect barRect = tester.getRect(find.byType(SearchBar));
758+
final Rect leadingRect = tester.getRect(find.byIcon(Icons.search));
759+
final Rect textFieldRect = tester.getRect(find.byType(TextField));
760+
761+
expect(barRect.left, leadingRect.left - 10.0);
762+
expect(leadingRect.right, textFieldRect.left - 10.0);
763+
// right side: outer (10) + inner (10) = 20
764+
expect(barRect.right, textFieldRect.right + 20.0);
765+
});
766+
704767
testWidgets('SearchBar respects hintStyle property', (WidgetTester tester) async {
705768
await tester.pumpWidget(
706769
MaterialApp(

0 commit comments

Comments
 (0)