mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
add directionality to CupertinoDialog (#167403)
Fixes `CupertinoDialog` not respecting Directionality for actions Issue refers to AlertDIalog.adaptive, but I have found that problem occurs only in `CupertinoDialog`. ## Related Issues * Fixes [Dialog actions do not respect Directionality #166880](https://github.com/flutter/flutter/issues/166880) ## Tests Added 1 test ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
This commit is contained in:
parent
bee65f3a4f
commit
9218243c61
@ -2307,18 +2307,26 @@ class _AlertDialogActionsLayout extends MultiChildRenderObjectWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
RenderObject createRenderObject(BuildContext context) {
|
RenderObject createRenderObject(BuildContext context) {
|
||||||
return _RenderAlertDialogActionsLayout(dividerThickness: _dividerThickness);
|
return _RenderAlertDialogActionsLayout(
|
||||||
|
dividerThickness: _dividerThickness,
|
||||||
|
textDirection: Directionality.of(context),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void updateRenderObject(BuildContext context, _RenderAlertDialogActionsLayout renderObject) {
|
void updateRenderObject(BuildContext context, _RenderAlertDialogActionsLayout renderObject) {
|
||||||
renderObject.dividerThickness = _dividerThickness;
|
renderObject
|
||||||
|
..dividerThickness = _dividerThickness
|
||||||
|
..textDirection = Directionality.of(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _RenderAlertDialogActionsLayout extends RenderFlex {
|
class _RenderAlertDialogActionsLayout extends RenderFlex {
|
||||||
_RenderAlertDialogActionsLayout({List<RenderBox>? children, required double dividerThickness})
|
_RenderAlertDialogActionsLayout({
|
||||||
: _dividerThickness = dividerThickness,
|
List<RenderBox>? children,
|
||||||
|
required double dividerThickness,
|
||||||
|
super.textDirection,
|
||||||
|
}) : _dividerThickness = dividerThickness,
|
||||||
super(
|
super(
|
||||||
direction: Axis.vertical,
|
direction: Axis.vertical,
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
@ -2405,12 +2413,17 @@ class _RenderAlertDialogActionsLayout extends RenderFlex {
|
|||||||
final double height = getMinIntrinsicHeight(overallWidth);
|
final double height = getMinIntrinsicHeight(overallWidth);
|
||||||
size = Size(overallWidth, height);
|
size = Size(overallWidth, height);
|
||||||
|
|
||||||
|
final bool ltr = textDirection == TextDirection.ltr;
|
||||||
RenderBox slot = firstChild!;
|
RenderBox slot = firstChild!;
|
||||||
double x = 0;
|
double x = ltr ? 0 : (overallWidth - slotWidth);
|
||||||
while (true) {
|
while (true) {
|
||||||
slot.layout(BoxConstraints.tight(Size(slotWidth, height)), parentUsesSize: true);
|
slot.layout(BoxConstraints.tight(Size(slotWidth, height)), parentUsesSize: true);
|
||||||
(slot.parentData! as FlexParentData).offset = Offset(x, 0);
|
(slot.parentData! as FlexParentData).offset = Offset(x, 0);
|
||||||
|
if (ltr) {
|
||||||
x += slot.size.width;
|
x += slot.size.width;
|
||||||
|
} else {
|
||||||
|
x -= slot.size.width;
|
||||||
|
}
|
||||||
|
|
||||||
final RenderBox? divider = childAfter(slot);
|
final RenderBox? divider = childAfter(slot);
|
||||||
if (divider == null) {
|
if (divider == null) {
|
||||||
@ -2418,8 +2431,11 @@ class _RenderAlertDialogActionsLayout extends RenderFlex {
|
|||||||
}
|
}
|
||||||
divider.layout(BoxConstraints.tight(Size(dividerThickness, height)));
|
divider.layout(BoxConstraints.tight(Size(dividerThickness, height)));
|
||||||
(divider.parentData! as FlexParentData).offset = Offset(x, 0);
|
(divider.parentData! as FlexParentData).offset = Offset(x, 0);
|
||||||
|
if (ltr) {
|
||||||
x += dividerThickness;
|
x += dividerThickness;
|
||||||
|
} else {
|
||||||
|
x -= dividerThickness;
|
||||||
|
}
|
||||||
slot = childAfter(divider)!;
|
slot = childAfter(divider)!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2036,6 +2036,33 @@ void main() {
|
|||||||
|
|
||||||
expect(elements.length, 1, reason: 'No DecoratedBox matches the specified criteria.');
|
expect(elements.length, 1, reason: 'No DecoratedBox matches the specified criteria.');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('Check for Directionality', (WidgetTester tester) async {
|
||||||
|
Future<void> pumpWidget({required bool isLTR}) async {
|
||||||
|
await tester.pumpWidget(
|
||||||
|
CupertinoApp(
|
||||||
|
home: Directionality(
|
||||||
|
textDirection: isLTR ? TextDirection.ltr : TextDirection.rtl,
|
||||||
|
child: const CupertinoAlertDialog(
|
||||||
|
actions: <CupertinoDialogAction>[
|
||||||
|
CupertinoDialogAction(isDefaultAction: true, child: Text('No')),
|
||||||
|
CupertinoDialogAction(child: Text('Yes')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await pumpWidget(isLTR: true);
|
||||||
|
Offset yesButton = tester.getCenter(find.text('Yes'));
|
||||||
|
Offset noButton = tester.getCenter(find.text('No'));
|
||||||
|
expect(yesButton.dx > noButton.dx, true);
|
||||||
|
await pumpWidget(isLTR: false);
|
||||||
|
yesButton = tester.getCenter(find.text('Yes'));
|
||||||
|
noButton = tester.getCenter(find.text('No'));
|
||||||
|
expect(yesButton.dx > noButton.dx, false);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderBox findActionButtonRenderBoxByTitle(WidgetTester tester, String title) {
|
RenderBox findActionButtonRenderBoxByTitle(WidgetTester tester, String title) {
|
||||||
@ -2124,6 +2151,7 @@ class _RestorableDialogTestWidget extends StatelessWidget {
|
|||||||
// The `theme` will be applied to the app and determines the background.
|
// The `theme` will be applied to the app and determines the background.
|
||||||
class TestScaffoldApp extends StatefulWidget {
|
class TestScaffoldApp extends StatefulWidget {
|
||||||
const TestScaffoldApp({super.key, required this.theme, required this.dialog});
|
const TestScaffoldApp({super.key, required this.theme, required this.dialog});
|
||||||
|
|
||||||
final CupertinoThemeData theme;
|
final CupertinoThemeData theme;
|
||||||
final Widget dialog;
|
final Widget dialog;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user