Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 95d1fab

Browse files
authored
Simplify SafeArea test for maintainBottomViewPadding to ensure maintainBottomViewPadding is always respected (#97646)
1 parent f63e0c8 commit 95d1fab

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

packages/flutter/lib/src/widgets/safe_area.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ class SafeArea extends StatelessWidget {
7171
/// The greater of the minimum insets and the media padding will be applied.
7272
final EdgeInsets minimum;
7373

74-
/// Specifies whether the [SafeArea] should maintain the
75-
/// [MediaQueryData.viewPadding] instead of the [MediaQueryData.padding] when
76-
/// consumed by the [MediaQueryData.viewInsets] of the current context's
77-
/// [MediaQuery], defaults to false.
74+
/// Specifies whether the [SafeArea] should maintain the bottom
75+
/// [MediaQueryData.viewPadding] instead of the bottom [MediaQueryData.padding],
76+
/// defaults to false.
7877
///
7978
/// For example, if there is an onscreen keyboard displayed above the
8079
/// SafeArea, the padding can be maintained below the obstruction rather than
@@ -98,7 +97,7 @@ class SafeArea extends StatelessWidget {
9897
final MediaQueryData data = MediaQuery.of(context);
9998
EdgeInsets padding = data.padding;
10099
// Bottom padding has been consumed - i.e. by the keyboard
101-
if (data.padding.bottom == 0.0 && data.viewInsets.bottom != 0.0 && maintainBottomViewPadding)
100+
if (maintainBottomViewPadding)
102101
padding = padding.copyWith(bottom: data.viewPadding.bottom);
103102

104103
return Padding(

packages/flutter/test/widgets/safe_area_test.dart

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,39 @@ void main() {
147147
expect(initialPoint, finalPoint);
148148
});
149149

150+
testWidgets('SafeArea alone - partial ViewInsets consume Padding', (WidgetTester tester) async {
151+
final Widget child = boilerplate(SafeArea(
152+
maintainBottomViewPadding: true,
153+
child: Column(
154+
children: const <Widget>[
155+
Expanded(child: Placeholder()),
156+
],
157+
),
158+
));
159+
160+
await tester.pumpWidget(
161+
MediaQuery(
162+
data: const MediaQueryData(
163+
viewPadding: EdgeInsets.only(bottom: 20.0),
164+
),
165+
child: child,
166+
),
167+
);
168+
final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
169+
// Consume bottom padding - as if by the keyboard opening
170+
await tester.pumpWidget(
171+
MediaQuery(
172+
data: const MediaQueryData(
173+
viewPadding: EdgeInsets.only(bottom: 20.0),
174+
viewInsets: EdgeInsets.only(bottom: 10.0),
175+
),
176+
child: child,
177+
),
178+
);
179+
final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
180+
expect(initialPoint, finalPoint);
181+
});
182+
150183
testWidgets('SafeArea with nested Scaffold', (WidgetTester tester) async {
151184
final Widget child = boilerplate(SafeArea(
152185
maintainBottomViewPadding: true,
@@ -184,6 +217,42 @@ void main() {
184217
final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
185218
expect(initialPoint, finalPoint);
186219
});
220+
221+
testWidgets('SafeArea with nested Scaffold - partial ViewInsets consume Padding', (WidgetTester tester) async {
222+
final Widget child = boilerplate(SafeArea(
223+
maintainBottomViewPadding: true,
224+
child: Scaffold(
225+
resizeToAvoidBottomInset: false,
226+
body: Column(
227+
children: const <Widget>[
228+
Expanded(child: Placeholder()),
229+
],
230+
),
231+
),
232+
));
233+
234+
await tester.pumpWidget(
235+
MediaQuery(
236+
data: const MediaQueryData(
237+
viewPadding: EdgeInsets.only(bottom: 20.0),
238+
),
239+
child: child,
240+
),
241+
);
242+
final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
243+
// Consume bottom padding - as if by the keyboard opening
244+
await tester.pumpWidget(
245+
MediaQuery(
246+
data: const MediaQueryData(
247+
viewPadding: EdgeInsets.only(bottom: 20.0),
248+
viewInsets: EdgeInsets.only(bottom: 10.0),
249+
),
250+
child: child,
251+
),
252+
);
253+
final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
254+
expect(initialPoint, finalPoint);
255+
});
187256
});
188257
});
189258

0 commit comments

Comments
 (0)