Skip to content

Commit be0a4e0

Browse files
authored
[material_ui] SearchAnchor overlay expands to full screen when viewport size changes (flutter#12466)
**Port over flutter#186537 from flutter/flutter.** Fixes flutter#186154 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 1785501 commit be0a4e0

3 files changed

Lines changed: 259 additions & 100 deletions

File tree

‎packages/material_ui/lib/src/search_anchor.dart‎

Lines changed: 112 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,114 +1138,126 @@ class _ViewContentState extends State<_ViewContent> {
11381138
child: const Divider(height: 1),
11391139
);
11401140

1141-
return Align(
1142-
alignment: Alignment.topLeft,
1143-
child: Transform.translate(
1144-
offset: _viewRect.topLeft,
1145-
child: ConstrainedBox(
1146-
constraints: BoxConstraints(
1147-
minWidth: minWidth,
1148-
maxWidth: _viewRect.width,
1149-
minHeight: minHeight,
1150-
maxHeight: _viewRect.height,
1151-
),
1152-
child: Padding(
1153-
padding: widget.showFullScreenView
1154-
? EdgeInsets.zero
1155-
: (effectivePadding ?? EdgeInsets.zero),
1156-
child: Material(
1157-
clipBehavior: Clip.antiAlias,
1158-
shape: effectiveShape,
1159-
color: effectiveBackgroundColor,
1160-
surfaceTintColor: effectiveSurfaceTint,
1161-
elevation: effectiveElevation,
1162-
child: OverflowBox(
1163-
alignment: Alignment.topLeft,
1164-
maxWidth: math.min(widget.viewMaxWidth, _screenSize!.width),
1165-
minWidth: 0,
1166-
fit: OverflowBoxFit.deferToChild,
1167-
child: FadeTransition(
1168-
opacity: viewIconsFadeCurve,
1169-
child: Column(
1170-
mainAxisSize: MainAxisSize.min,
1171-
crossAxisAlignment: CrossAxisAlignment.stretch,
1172-
children: <Widget>[
1173-
Padding(
1174-
padding: EdgeInsets.only(top: widget.topPadding),
1175-
child: SafeArea(
1176-
top: false,
1177-
bottom: false,
1178-
child: SearchBar(
1179-
autoFocus: true,
1180-
constraints:
1181-
headerConstraints ??
1182-
(widget.showFullScreenView
1183-
? BoxConstraints(
1184-
minHeight: _SearchViewDefaultsM3.fullScreenBarHeight,
1185-
)
1186-
: null),
1187-
padding: WidgetStatePropertyAll<EdgeInsetsGeometry?>(
1188-
effectiveBarPadding,
1189-
),
1190-
leading: widget.viewLeading ?? defaultLeading,
1191-
trailing: widget.viewTrailing ?? defaultTrailing,
1192-
hintText: widget.viewHintText,
1193-
backgroundColor: const MaterialStatePropertyAll<Color>(
1194-
Colors.transparent,
1141+
return LayoutBuilder(
1142+
builder: (BuildContext context, BoxConstraints constraints) {
1143+
return Align(
1144+
alignment: Alignment.topLeft,
1145+
child: Transform.translate(
1146+
offset: _viewRect.topLeft,
1147+
child: ConstrainedBox(
1148+
constraints: BoxConstraints(
1149+
minWidth: minWidth,
1150+
maxWidth: widget.showFullScreenView
1151+
? math.max(constraints.maxWidth, minWidth)
1152+
: _viewRect.width,
1153+
minHeight: minHeight,
1154+
maxHeight: widget.showFullScreenView
1155+
? math.max(constraints.maxHeight, minHeight)
1156+
: _viewRect.height,
1157+
),
1158+
child: Padding(
1159+
padding: widget.showFullScreenView
1160+
? EdgeInsets.zero
1161+
: (effectivePadding ?? EdgeInsets.zero),
1162+
child: Material(
1163+
clipBehavior: Clip.antiAlias,
1164+
shape: effectiveShape,
1165+
color: effectiveBackgroundColor,
1166+
surfaceTintColor: effectiveSurfaceTint,
1167+
elevation: effectiveElevation,
1168+
child: OverflowBox(
1169+
alignment: Alignment.topLeft,
1170+
maxWidth: widget.showFullScreenView
1171+
? constraints.maxWidth
1172+
: math.min(widget.viewMaxWidth, constraints.maxWidth),
1173+
minWidth: 0,
1174+
fit: OverflowBoxFit.deferToChild,
1175+
child: FadeTransition(
1176+
opacity: viewIconsFadeCurve,
1177+
child: Column(
1178+
mainAxisSize: MainAxisSize.min,
1179+
crossAxisAlignment: CrossAxisAlignment.stretch,
1180+
children: <Widget>[
1181+
Padding(
1182+
padding: EdgeInsets.only(top: widget.topPadding),
1183+
child: SafeArea(
1184+
top: false,
1185+
bottom: false,
1186+
child: SearchBar(
1187+
autoFocus: true,
1188+
constraints:
1189+
headerConstraints ??
1190+
(widget.showFullScreenView
1191+
? BoxConstraints(
1192+
minHeight: _SearchViewDefaultsM3.fullScreenBarHeight,
1193+
)
1194+
: null),
1195+
padding: WidgetStatePropertyAll<EdgeInsetsGeometry?>(
1196+
effectiveBarPadding,
1197+
),
1198+
leading: widget.viewLeading ?? defaultLeading,
1199+
trailing: widget.viewTrailing ?? defaultTrailing,
1200+
hintText: widget.viewHintText,
1201+
backgroundColor: const MaterialStatePropertyAll<Color>(
1202+
Colors.transparent,
1203+
),
1204+
overlayColor: const MaterialStatePropertyAll<Color>(
1205+
Colors.transparent,
1206+
),
1207+
elevation: const MaterialStatePropertyAll<double>(0.0),
1208+
textStyle: MaterialStatePropertyAll<TextStyle?>(effectiveTextStyle),
1209+
hintStyle: MaterialStatePropertyAll<TextStyle?>(effectiveHintStyle),
1210+
controller: _controller,
1211+
onChanged: (String value) {
1212+
widget.viewOnChanged?.call(value);
1213+
updateSuggestions();
1214+
},
1215+
onSubmitted: widget.viewOnSubmitted,
1216+
textCapitalization: widget.textCapitalization,
1217+
textInputAction: widget.textInputAction,
1218+
keyboardType: widget.keyboardType,
1219+
smartDashesType: widget.smartDashesType,
1220+
smartQuotesType: widget.smartQuotesType,
1221+
),
11951222
),
1196-
overlayColor: const MaterialStatePropertyAll<Color>(Colors.transparent),
1197-
elevation: const MaterialStatePropertyAll<double>(0.0),
1198-
textStyle: MaterialStatePropertyAll<TextStyle?>(effectiveTextStyle),
1199-
hintStyle: MaterialStatePropertyAll<TextStyle?>(effectiveHintStyle),
1200-
controller: _controller,
1201-
onChanged: (String value) {
1202-
widget.viewOnChanged?.call(value);
1203-
updateSuggestions();
1204-
},
1205-
onSubmitted: widget.viewOnSubmitted,
1206-
textCapitalization: widget.textCapitalization,
1207-
textInputAction: widget.textInputAction,
1208-
keyboardType: widget.keyboardType,
1209-
smartDashesType: widget.smartDashesType,
1210-
smartQuotesType: widget.smartQuotesType,
12111223
),
1212-
),
1224+
if (!effectiveShrinkWrap ||
1225+
minHeight > 0 ||
1226+
widget.showFullScreenView ||
1227+
result.isNotEmpty) ...<Widget>[
1228+
FadeTransition(opacity: viewDividerFadeCurve, child: viewDivider),
1229+
Flexible(
1230+
fit: (effectiveShrinkWrap && !widget.showFullScreenView)
1231+
? FlexFit.loose
1232+
: FlexFit.tight,
1233+
child: FadeTransition(
1234+
opacity: viewListFadeOnIntervalCurve,
1235+
child: widget.viewBuilder == null
1236+
? MediaQuery.removePadding(
1237+
context: context,
1238+
removeTop: true,
1239+
child: ListView(
1240+
padding: EdgeInsets.only(
1241+
bottom: MediaQuery.viewInsetsOf(context).bottom,
1242+
),
1243+
shrinkWrap: effectiveShrinkWrap,
1244+
children: result.toList(),
1245+
),
1246+
)
1247+
: widget.viewBuilder!(result),
1248+
),
1249+
),
1250+
],
1251+
],
12131252
),
1214-
if (!effectiveShrinkWrap ||
1215-
minHeight > 0 ||
1216-
widget.showFullScreenView ||
1217-
result.isNotEmpty) ...<Widget>[
1218-
FadeTransition(opacity: viewDividerFadeCurve, child: viewDivider),
1219-
Flexible(
1220-
fit: (effectiveShrinkWrap && !widget.showFullScreenView)
1221-
? FlexFit.loose
1222-
: FlexFit.tight,
1223-
child: FadeTransition(
1224-
opacity: viewListFadeOnIntervalCurve,
1225-
child: widget.viewBuilder == null
1226-
? MediaQuery.removePadding(
1227-
context: context,
1228-
removeTop: true,
1229-
child: ListView(
1230-
padding: EdgeInsets.only(
1231-
bottom: MediaQuery.viewInsetsOf(context).bottom,
1232-
),
1233-
shrinkWrap: effectiveShrinkWrap,
1234-
children: result.toList(),
1235-
),
1236-
)
1237-
: widget.viewBuilder!(result),
1238-
),
1239-
),
1240-
],
1241-
],
1253+
),
12421254
),
12431255
),
12441256
),
12451257
),
12461258
),
1247-
),
1248-
),
1259+
);
1260+
},
12491261
);
12501262
}
12511263
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
changelog: |
2+
- SearchAnchor overlay expands to full-screen on viewport size change (e.g., on device rotation).
3+
- Fixes https://github.com/flutter/flutter/issues/186154.
4+
version: patch

‎packages/material_ui/test/search_anchor_test.dart‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,6 +4380,143 @@ void main() {
43804380
await tester.pump();
43814381
expect(find.text('X'), findsOne);
43824382
});
4383+
4384+
// Regression test for https://github.com/flutter/flutter/issues/186154.
4385+
testWidgets('SearchAnchor full-screen view expands to fit screen when rotated', (
4386+
WidgetTester tester,
4387+
) async {
4388+
addTearDown(tester.view.reset);
4389+
4390+
// Start in portrait mode.
4391+
const portraitModeWidth = 360.0;
4392+
const portraitModeHeight = 800.0;
4393+
tester.view.physicalSize = const Size(portraitModeWidth, portraitModeHeight);
4394+
tester.view.devicePixelRatio = 1.0;
4395+
4396+
await tester.pumpWidget(
4397+
MaterialApp(
4398+
home: Center(
4399+
child: SearchAnchor(
4400+
isFullScreen: true,
4401+
builder: (BuildContext context, SearchController controller) {
4402+
return IconButton(
4403+
icon: const Icon(Icons.search),
4404+
onPressed: () {
4405+
controller.openView();
4406+
},
4407+
);
4408+
},
4409+
suggestionsBuilder: (BuildContext context, SearchController controller) {
4410+
return <Widget>[];
4411+
},
4412+
),
4413+
),
4414+
),
4415+
);
4416+
4417+
// Open search view.
4418+
await tester.tap(find.byIcon(Icons.search));
4419+
await tester.pumpAndSettle();
4420+
4421+
// Verify starting sizes match portrait mode.
4422+
final Size startingSize = getSearchViewSize(tester);
4423+
expect(startingSize.width, portraitModeWidth);
4424+
expect(startingSize.height, portraitModeHeight);
4425+
4426+
// Rotate to landscape mode.
4427+
const landscapeModeWidth = portraitModeHeight;
4428+
const landscapeModeHeight = portraitModeWidth;
4429+
tester.view.physicalSize = const Size(landscapeModeWidth, landscapeModeHeight);
4430+
await tester.pumpAndSettle();
4431+
4432+
// Verify the view expands to match landscape mode.
4433+
final Size rotatedSize = getSearchViewSize(tester);
4434+
expect(rotatedSize.width, landscapeModeWidth);
4435+
expect(rotatedSize.height, landscapeModeHeight);
4436+
});
4437+
4438+
// Regression test for https://github.com/flutter/flutter/issues/186154.
4439+
testWidgets('SearchAnchor resizes itself to match Navigator parent', (WidgetTester tester) async {
4440+
addTearDown(tester.view.reset);
4441+
4442+
var parentHeight = 400.0;
4443+
var parentWidth = 600.0;
4444+
late StateSetter setState;
4445+
4446+
await tester.pumpWidget(
4447+
MaterialApp(
4448+
home: Scaffold(
4449+
body: StatefulBuilder(
4450+
builder: (BuildContext context, StateSetter stateSetter) {
4451+
setState = stateSetter;
4452+
return SizedBox(
4453+
height: parentHeight,
4454+
width: parentWidth,
4455+
child: Navigator(
4456+
onGenerateRoute: (RouteSettings settings) {
4457+
return MaterialPageRoute<void>(
4458+
builder: (BuildContext context) {
4459+
return Scaffold(
4460+
body: SearchAnchor(
4461+
isFullScreen: true,
4462+
builder: (BuildContext context, SearchController controller) {
4463+
return IconButton(
4464+
icon: const Icon(Icons.search),
4465+
onPressed: () {
4466+
controller.openView();
4467+
},
4468+
);
4469+
},
4470+
suggestionsBuilder:
4471+
(BuildContext context, SearchController controller) {
4472+
return <Widget>[];
4473+
},
4474+
),
4475+
);
4476+
},
4477+
);
4478+
},
4479+
),
4480+
);
4481+
},
4482+
),
4483+
),
4484+
),
4485+
);
4486+
4487+
// Open search view.
4488+
await tester.tap(find.byIcon(Icons.search));
4489+
await tester.pumpAndSettle();
4490+
4491+
// Verify search view size matches parent.
4492+
Size size = getSearchViewSize(tester);
4493+
expect(size.height, 400.0);
4494+
expect(size.width, 600.0);
4495+
4496+
// Resize the parent container larger.
4497+
setState(() {
4498+
parentHeight = 500.0;
4499+
parentWidth = 700.0;
4500+
});
4501+
await tester.pumpAndSettle();
4502+
4503+
// Verify the view expands to match parent.
4504+
size = getSearchViewSize(tester);
4505+
expect(size.height, 500.0);
4506+
expect(size.width, 700.0);
4507+
4508+
// Resize the parent container smaller.
4509+
setState(() {
4510+
parentHeight = 300.0;
4511+
parentWidth = 400.0;
4512+
});
4513+
await tester.pumpAndSettle();
4514+
4515+
// Verify the view shrinks to match parent.
4516+
size = getSearchViewSize(tester);
4517+
expect(size.height, 300.0);
4518+
expect(size.width, 400.0);
4519+
});
43834520
}
43844521

43854522
Future<void> checkSearchBarDefaults(
@@ -4480,3 +4617,9 @@ Material getSearchViewMaterial(WidgetTester tester) {
44804617
find.descendant(of: findViewContent(), matching: find.byType(Material)).first,
44814618
);
44824619
}
4620+
4621+
Size getSearchViewSize(WidgetTester tester) {
4622+
return tester.getSize(
4623+
find.descendant(of: findViewContent(), matching: find.byType(ConstrainedBox)).first,
4624+
);
4625+
}

0 commit comments

Comments
 (0)