diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 23a512c74f..dccacc3290 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -1217,13 +1217,7 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate { : contentBottom; } - double xOffset = 0.0; - if (hasCustomWidth) { - xOffset = switch (textDirection) { - TextDirection.rtl => (snackBarWidth! - size.width) / 2, - TextDirection.ltr => (size.width - snackBarWidth!) / 2, - }; - } + final double xOffset = hasCustomWidth ? (size.width - snackBarWidth!) / 2 : 0.0; positionChild(_ScaffoldSlot.snackBar, Offset(xOffset, snackBarYOffsetBase - snackBarSize.height)); assert((){ diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index 78e1b2eb81..ac4d4b2e87 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -819,7 +819,7 @@ class _SnackBarState extends State { animation: heightM3Animation, builder: (BuildContext context, Widget? child) { return Align( - alignment: AlignmentDirectional.bottomStart, + alignment: Alignment.bottomLeft, heightFactor: heightM3Animation.value, child: child, ); diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index c35f1e26d5..105d197d7c 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -2601,7 +2601,7 @@ void main() { }, ); - testWidgets('Floating snackbar with custom width is centered when text direction is rtl', (WidgetTester tester) async { + testWidgets('Material3 - Floating snackbar with custom width is centered when text direction is rtl', (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/140125. const double customWidth = 400.0; await tester.pumpWidget( @@ -2631,7 +2631,51 @@ void main() { ); await tester.tap(find.text('X')); - await tester.pump(); // start animation + await tester.pump(); // Start animation. + await tester.pump(const Duration(milliseconds: 750)); + + final Finder materialFinder = find.descendant( + of: find.byType(SnackBar), + matching: find.byType(Material), + ); + final Offset snackBarBottomLeft = tester.getBottomLeft(materialFinder); + final Offset snackBarBottomRight = tester.getBottomRight(materialFinder); + expect(snackBarBottomLeft.dx, (800 - customWidth) / 2); // Device width is 800. + expect(snackBarBottomRight.dx, (800 + customWidth) / 2); // Device width is 800. + }); + + testWidgets('Material2 - Floating snackbar with custom width is centered when text direction is rtl', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/147838. + const double customWidth = 400.0; + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(useMaterial3: false), + home: Directionality( + textDirection: TextDirection.rtl, + child: Scaffold( + body: Builder( + builder: (BuildContext context) { + return GestureDetector( + onTap: () { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + behavior: SnackBarBehavior.floating, + width: customWidth, + content: Text('Feeling super snackish'), + ), + ); + }, + child: const Text('X'), + ); + }, + ), + ), + ), + ), + ); + + await tester.tap(find.text('X')); + await tester.pump(); // Start animation. await tester.pump(const Duration(milliseconds: 750)); final Finder materialFinder = find.descendant(