diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index 4911b7b96c..dd6263f2d7 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -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 { 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); }, diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index 703ee43459..e5bfb32ea7 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -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 completer = Completer(); + + 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: [ + 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".