Center Floating Snackbar with custom width when direction is RTL (#140215)
## Description This PR fixes the positionning of a floating snackbar when the text direction is RTL. ## Related Issue Fixes https://github.com/flutter/flutter/issues/140125. ## Tests Adds 1 test.
This commit is contained in:
@@ -1183,7 +1183,13 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate {
|
||||
: contentBottom;
|
||||
}
|
||||
|
||||
final double xOffset = hasCustomWidth ? (size.width - snackBarWidth!) / 2 : 0.0;
|
||||
double xOffset = 0.0;
|
||||
if (hasCustomWidth) {
|
||||
xOffset = switch (textDirection) {
|
||||
TextDirection.rtl => (snackBarWidth! - size.width) / 2,
|
||||
TextDirection.ltr => (size.width - snackBarWidth!) / 2,
|
||||
};
|
||||
}
|
||||
positionChild(_ScaffoldSlot.snackBar, Offset(xOffset, snackBarYOffsetBase - snackBarSize.height));
|
||||
|
||||
assert((){
|
||||
|
||||
@@ -2604,6 +2604,49 @@ void main() {
|
||||
expect(snackBarTopRight.dx - actionTopRight.dx, 8.0 + 15.0); // button margin + horizontal scaffold outside margin
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('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(
|
||||
MaterialApp(
|
||||
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(
|
||||
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('SnackBars hero across transitions when using ScaffoldMessenger', (WidgetTester tester) async {
|
||||
|
||||
Reference in New Issue
Block a user