Change snack bar default hitTestBehavior to deferToChild when SnackBarThemeData.insetPadding is not null (#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 #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.*
#148566
This commit is contained in:
Hany Mohamed
2024-05-29 08:31:32 +04:00
committed by GitHub
parent 886de22182
commit 9ea9726b21
2 changed files with 63 additions and 2 deletions

View File

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

View File

@@ -4078,6 +4078,66 @@ testWidgets('SnackBarAction backgroundColor works as a Color', (WidgetTester tes
theme.colorScheme.inversePrimary.withOpacity(0.08),
);
});
testWidgets('Can interact with widgets behind SnackBar when insetPadding is set in SnackBarThemeData', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/148566.
tester.view.physicalSize = const Size.square(200);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
const String buttonText = 'Show snackbar';
const String snackbarContent = 'Snackbar';
const String buttonText2 = 'Try press me';
final Completer<void> completer = Completer<void>();
await tester.pumpWidget(MaterialApp(
theme: ThemeData(
snackBarTheme: const SnackBarThemeData(
insetPadding: EdgeInsets.only(left: 100),
),
),
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ElevatedButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
behavior: SnackBarBehavior.floating,
content: Text(snackbarContent),
),
);
},
child: const Text(buttonText),
),
ElevatedButton(
onPressed: () {
completer.complete();
},
child: const Text(buttonText2),
),
],
);
},
),
),
));
await tester.tap(find.text(buttonText));
await tester.pumpAndSettle();
expect(find.text(snackbarContent), findsOneWidget);
await tester.tapAt(tester.getTopLeft(find.text(buttonText2)));
expect(find.text(snackbarContent), findsOneWidget);
expect(completer.isCompleted, true);
});
}
/// Start test for "SnackBar dismiss test".