Skip to content

Commit fa19af3

Browse files
hany-achrafvictorsanni
authored andcommitted
Change snack bar default hitTestBehavior to deferToChild when SnackBarThemeData.insetPadding is not null (flutter#148568)
The PR changes the default value of hitTestBehavior in snack bars to `HitTestBehavior.deferToChild` when snackBarTheme.insetPadding is not null, so that widgets behind snack bars affected by the value set to insetPadding, remain interactive even while a snack bar is visible. This PR can be considered as an extension to what have been done in PR flutter#127959 which fixes the same problem but for individual snack bars with margin not being null. This PR works on the theme level. *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* flutter#148566
1 parent bb480a9 commit fa19af3

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

packages/flutter/lib/src/material/snack_bar.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ class SnackBar extends StatefulWidget {
387387

388388
/// Defines how the snack bar area, including margin, will behave during hit testing.
389389
///
390-
/// If this property is null and [margin] is not null, then [HitTestBehavior.deferToChild] is used by default.
390+
/// If this property is null, and [margin] is not null or [SnackBarThemeData.insetPadding] of
391+
/// [ThemeData.snackBarTheme] is not null, then [HitTestBehavior.deferToChild] is used by default.
391392
///
392393
/// Please refer to [HitTestBehavior] for a detailed explanation of every behavior.
393394
final HitTestBehavior? hitTestBehavior;
@@ -815,7 +816,7 @@ class _SnackBarState extends State<SnackBar> {
815816
key: const Key('dismissible'),
816817
direction: dismissDirection,
817818
resizeDuration: null,
818-
behavior: widget.hitTestBehavior ?? (widget.margin != null ? HitTestBehavior.deferToChild : HitTestBehavior.opaque),
819+
behavior: widget.hitTestBehavior ?? (widget.margin != null || snackBarTheme.insetPadding != null ? HitTestBehavior.deferToChild : HitTestBehavior.opaque),
819820
onDismissed: (DismissDirection direction) {
820821
ScaffoldMessenger.of(context).removeCurrentSnackBar(reason: SnackBarClosedReason.swipe);
821822
},

packages/flutter/test/material/snack_bar_test.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4078,6 +4078,66 @@ testWidgets('SnackBarAction backgroundColor works as a Color', (WidgetTester tes
40784078
theme.colorScheme.inversePrimary.withOpacity(0.08),
40794079
);
40804080
});
4081+
4082+
testWidgets('Can interact with widgets behind SnackBar when insetPadding is set in SnackBarThemeData', (WidgetTester tester) async {
4083+
// Regression test for https://github.com/flutter/flutter/issues/148566.
4084+
tester.view.physicalSize = const Size.square(200);
4085+
tester.view.devicePixelRatio = 1;
4086+
addTearDown(tester.view.resetPhysicalSize);
4087+
addTearDown(tester.view.resetDevicePixelRatio);
4088+
4089+
const String buttonText = 'Show snackbar';
4090+
const String snackbarContent = 'Snackbar';
4091+
const String buttonText2 = 'Try press me';
4092+
4093+
final Completer<void> completer = Completer<void>();
4094+
4095+
await tester.pumpWidget(MaterialApp(
4096+
theme: ThemeData(
4097+
snackBarTheme: const SnackBarThemeData(
4098+
insetPadding: EdgeInsets.only(left: 100),
4099+
),
4100+
),
4101+
home: Scaffold(
4102+
body: Builder(
4103+
builder: (BuildContext context) {
4104+
return Column(
4105+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
4106+
crossAxisAlignment: CrossAxisAlignment.stretch,
4107+
children: <Widget>[
4108+
ElevatedButton(
4109+
onPressed: () {
4110+
ScaffoldMessenger.of(context).showSnackBar(
4111+
const SnackBar(
4112+
behavior: SnackBarBehavior.floating,
4113+
content: Text(snackbarContent),
4114+
),
4115+
);
4116+
},
4117+
child: const Text(buttonText),
4118+
),
4119+
ElevatedButton(
4120+
onPressed: () {
4121+
completer.complete();
4122+
},
4123+
child: const Text(buttonText2),
4124+
),
4125+
],
4126+
);
4127+
},
4128+
),
4129+
),
4130+
));
4131+
4132+
await tester.tap(find.text(buttonText));
4133+
await tester.pumpAndSettle();
4134+
4135+
expect(find.text(snackbarContent), findsOneWidget);
4136+
await tester.tapAt(tester.getTopLeft(find.text(buttonText2)));
4137+
expect(find.text(snackbarContent), findsOneWidget);
4138+
4139+
expect(completer.isCompleted, true);
4140+
});
40814141
}
40824142

40834143
/// Start test for "SnackBar dismiss test".

0 commit comments

Comments
 (0)