fix: "show dialog" functions should allow setting an AnimationStyle (#164002)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

fixes issue #154744

## Pre-launch Checklist

- [] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [] I signed the [CLA].
- [] I listed at least one issue that this PR fixes in the description
above.
- [] I updated/added relevant documentation (doc comments with `///`).
- [] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

---------

Co-authored-by: Tong Mu <dkwingsmt@users.noreply.github.com>
This commit is contained in:
Ujjwal Pratap Singh 2025-03-19 11:10:18 +05:30 committed by GitHub
parent 403404ccd1
commit 42d4f69566
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 3 deletions

View File

@ -1466,6 +1466,7 @@ Future<T?> showDialog<T>({
Offset? anchorPoint,
TraversalEdgeBehavior? traversalEdgeBehavior,
bool? requestFocus,
AnimationStyle? animationStyle,
}) {
assert(_debugIsActive(context));
assert(debugCheckHasMaterialLocalizations(context));
@ -1492,6 +1493,7 @@ Future<T?> showDialog<T>({
anchorPoint: anchorPoint,
traversalEdgeBehavior: traversalEdgeBehavior ?? TraversalEdgeBehavior.closedLoop,
requestFocus: requestFocus,
animationStyle: animationStyle,
),
);
}
@ -1516,6 +1518,7 @@ Future<T?> showAdaptiveDialog<T>({
Offset? anchorPoint,
TraversalEdgeBehavior? traversalEdgeBehavior,
bool? requestFocus,
AnimationStyle? animationStyle,
}) {
final ThemeData theme = Theme.of(context);
switch (theme.platform) {
@ -1535,6 +1538,7 @@ Future<T?> showAdaptiveDialog<T>({
anchorPoint: anchorPoint,
traversalEdgeBehavior: traversalEdgeBehavior,
requestFocus: requestFocus,
animationStyle: animationStyle,
);
case TargetPlatform.iOS:
case TargetPlatform.macOS:
@ -1628,7 +1632,9 @@ class DialogRoute<T> extends RawDialogRoute<T> {
super.requestFocus,
super.anchorPoint,
super.traversalEdgeBehavior,
}) : super(
AnimationStyle? animationStyle,
}) : _animationStyle = animationStyle,
super(
pageBuilder: (
BuildContext buildContext,
Animation<double> animation,
@ -1642,16 +1648,21 @@ class DialogRoute<T> extends RawDialogRoute<T> {
return dialog;
},
barrierLabel: barrierLabel ?? MaterialLocalizations.of(context).modalBarrierDismissLabel,
transitionDuration: const Duration(milliseconds: 150),
transitionDuration: animationStyle?.duration ?? const Duration(milliseconds: 150),
transitionBuilder: _buildMaterialDialogTransitions,
);
CurvedAnimation? _curvedAnimation;
final AnimationStyle? _animationStyle;
void _setAnimation(Animation<double> animation) {
if (_curvedAnimation?.parent != animation) {
_curvedAnimation?.dispose();
_curvedAnimation = CurvedAnimation(parent: animation, curve: Curves.easeOut);
_curvedAnimation = CurvedAnimation(
parent: animation,
curve: _animationStyle?.curve ?? Curves.easeOut,
reverseCurve: _animationStyle?.reverseCurve ?? Curves.easeOut,
);
}
}

View File

@ -2682,6 +2682,39 @@ void main() {
expect(find.text('Dialog2'), findsOneWidget);
});
testWidgets('Applies AnimationStyle to showAdaptiveDialog', (WidgetTester tester) async {
const AnimationStyle animationStyle = AnimationStyle(
duration: Duration(seconds: 1),
curve: Curves.easeInOut,
);
await tester.pumpWidget(
const MaterialApp(
home: Material(child: Center(child: ElevatedButton(onPressed: null, child: Text('Go')))),
),
);
final BuildContext context = tester.element(find.text('Go'));
showAdaptiveDialog<void>(
context: context,
builder: (BuildContext context) {
return Container(
width: 100.0,
height: 100.0,
alignment: Alignment.center,
child: const Text('Dialog1'),
);
},
animationStyle: animationStyle,
);
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(find.text('Dialog1'), findsOneWidget);
await tester.tapAt(const Offset(10.0, 10.0));
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(find.text('Dialog1'), findsNothing);
});
testWidgets('Uses open focus traversal when overridden', (WidgetTester tester) async {
final FocusNode okNode = FocusNode();
addTearDown(okNode.dispose);