Visitar URL original
Material Fade Transition Configuration; showModal implementation by shihaohong · Pull Request #60 · flutter/packages · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
11d2729
WIP - Fade transition for dialogs
Jan 6, 2020
7b276fe
WIP - Refactor to extend PopupRoute
Jan 6, 2020
b652352
Implement fade_transition.dart
Jan 10, 2020
f043bff
Simple test
Jan 10, 2020
4f32441
Newline
Jan 10, 2020
2c726e0
Remove unnecessary `settings` param, API wordsmithing
Jan 10, 2020
2acc1eb
dialog -> modal
Jan 10, 2020
b28aaf5
Minor modifications to FlutterLogoModal
Jan 10, 2020
88670c2
Make Dart formatter happy
Jan 10, 2020
e8a4530
WIP - generalize showModal function
Jan 13, 2020
b3030eb
API docs and code cleanup
Jan 13, 2020
7ff4a02
Address code review feedback
Jan 14, 2020
fd4b545
Fix doc to use SizedBox instead of Column and ConstrainedBox
Jan 14, 2020
b23ca9a
Remove erroneous comma and replace with semicolon
Jan 14, 2020
b16e820
Make formatter happy
Jan 14, 2020
733084b
WIP - test scaffolding
Jan 14, 2020
33a2f35
Add fade portion of forwards transition test
Jan 14, 2020
f9b17fe
Implement forwards and reverse test for fade transition
Jan 14, 2020
9733e0f
Implement animation interrupt test - Fix bug found from writing inter…
Jan 14, 2020
5845b3f
Implement state test for fade transition
Jan 14, 2020
8474d44
modal.dart tests, comments on fade_transition_test.dart
Jan 14, 2020
69a4191
Newline at end of modal_test.dart
Jan 14, 2020
3b50849
Make formatter happy
Jan 14, 2020
0aa7422
Merge branch 'master' of github.com:flutter/packages into fade-dialog
shihaohong Jan 16, 2020
45dd4bf
Code review feedback
shihaohong Jan 16, 2020
b812d34
Code cleanup
shihaohong Jan 16, 2020
64846d8
Make formatter happy
shihaohong Jan 16, 2020
614687c
abstract getters over constructor parameters
shihaohong Jan 16, 2020
a9968c7
Abstract getter -> constructor params
shihaohong Jan 16, 2020
ae78df8
Fix incorrect reverse transition duration in modal_test.dart
shihaohong Jan 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/animations/lib/animations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

export 'src/fade_through_transition.dart';
export 'src/fade_transition.dart';
export 'src/open_container.dart';
export 'src/page_transition_switcher.dart';
export 'src/shared_z_axis_transition.dart';
232 changes: 232 additions & 0 deletions packages/animations/lib/src/fade_transition.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// Copyright 2019 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:animations/src/utils/curves.dart';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this should probably be a relative import.

import 'package:flutter/material.dart';

/// Displays a modal above the current contents of the app.
///
/// This function displays the [FadeModalRoute], which transitions in
/// with the Material fade transition.
///
/// Content below the modal is dimmed with a [ModalBarrier].
///
/// ```dart
/// /// Sample widget that uses [showModalWithFadeTransition].
/// class MyHomePage extends StatelessWidget {
/// @override
/// Widget build(BuildContext context) {
/// return Scaffold(
/// body: Center(
/// child: RaisedButton(
/// onPressed: () {
/// showModalWithFadeTransition(
/// context: context,
/// child: FlutterLogoModal(),
/// );
/// },
/// child: Icon(Icons.add),
/// ),
/// ),
/// );
/// }
/// }
///
/// /// Displays a modal with the FlutterLogo on it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is kinda strange, nothing in this class seems to imply that it is a modal. It's only one because it was rendered with showModalWithFadeTransition, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that makes sense. I'll rename the widget

/// class FlutterLogoModal extends StatelessWidget {
/// const _FlutterLogoModal();
///
/// @override
/// Widget build(BuildContext context) {
/// return Column(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the Column? Seems like it only has one child?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the Column widget, the ConstrainedBox somehow does not respect the maximum width constraints and takes up the entire width of the screen instead. My guess is this is because it's the root widget of a new page, but I'm not entirely sure. I could investigate this and see if I could substitute in a widget that might make more sense here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does seem odd. I would expect a Center(child: SizedBox(...)) to work as expected. Can you show me what it looks like without?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SizedBox worked, but ConstrainedBox didn't when I tried. I just swapped it out

/// mainAxisAlignment: MainAxisAlignment.center,
/// children: <Widget>[
/// Center(
/// child: ConstrainedBox(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use a SizedBox here since you're giving tight constraints?

/// constraints: const BoxConstraints(
/// maxHeight: 300,
/// maxWidth: 300,
/// minHeight: 250,
/// minWidth: 250,
/// ),
/// child: const Material(
/// child: Center(child: FlutterLogo(size: 250)),
/// ),
/// ),
/// ),
/// ],
/// );
/// }
/// }
/// ```
///
/// The `context` argument is used to look up the [Navigator] for the
/// modal. It is only used when the method is called. Its corresponding widget
/// can be safely removed from the tree before the modal is closed.
///
/// The `useRootNavigator` argument is used to determine whether to push the
/// modal to the [Navigator] furthest from or nearest to the given `context`.
/// By default, `useRootNavigator` is `true` and the modal route created by
/// this method is pushed to the root navigator.
///
/// If the application has multiple [Navigator] objects, it may be necessary to

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it seems like this paragraph should just be a continuation of the previous one?

/// call `Navigator.of(context, rootNavigator: true).pop(result)` to close the
/// modal rather than just `Navigator.pop(context, result)`.
///
/// The `barrierDismissible` argument is used to determine whether this route
/// can be dismissed by tapping the modal barrier. This argument defaults
/// to true. If `barrierDismissible` is true, a non-null `barrierLabel` must be
/// provided.
///
/// The `barrierLabel` argument is the semantic label used for a dismissible
/// barrier. This argument defaults to "Dismiss".
///
/// Returns a [Future] that resolves to the value (if any) that was passed to
/// [Navigator.pop] when the modal was closed.
///
/// See also:
///
/// * [FadeModalRoute], which is the route that is built by this function.
Future<T> showModalWithFadeTransition<T>({

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From an API point of view: Would it be maybe better to have a showModal function that can be configured with different transitions, e.g. by passing in a transition object of some kind?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into it. I think it'd take some refactoring of the existing FadeModalRoute into something more generic and have some "route configurations" class that passes along all information used by the new general ModalRoute, such as transition duration, barrier label, etc.

@required BuildContext context,
bool barrierDismissible = true,
String barrierLabel,
bool useRootNavigator = true,
Widget child,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe in the other showDialog type methods we deprecated the child in favor of a builder. We should probably just have a builder argument here?

}) {
barrierLabel = barrierLabel ??
MaterialLocalizations.of(context).modalBarrierDismissLabel;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid looking up the string if the barrier is not dismissible?

assert(useRootNavigator != null);
assert(!barrierDismissible || barrierLabel != null);
return Navigator.of(context, rootNavigator: useRootNavigator).push<T>(
FadeModalRoute<T>(
barrierDismissible: barrierDismissible,
barrierLabel: barrierLabel,
child: child,
),
);
}

/// A modal route that overlays a widget on the current route with the Material
/// fade transition.
///
/// The fade pattern is used for UI elements that enter or exit from within
/// the screen bounds. Elements that enter use a quick fade in and scale from
/// 80% to 100%. Elements that exit simply fade out. The scale animation is
/// only applied to entering elements to emphasize new content over old.
///
/// See also:
///
/// * [showModalWithFadeTransition], which displays the modal popup.
class FadeModalRoute<T> extends PopupRoute<T> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we should just keep this class private and only make showModalWithFadeTransition the entry point?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my original thought process here was that I felt that the Material library's _DialogRoute would have been nice to use here if it were public and more extensible.

Thinking about it now, I guess it wouldn't really matter since we're taking a similar approach as _DialogRoute with the _FadeModalRoute. I'll change this back to private.

/// Creates a [FadeModalRoute] route with the Material fade transition.
///
/// [barrierDismissible] is true by default.
FadeModalRoute({
bool barrierDismissible = true,
String barrierLabel,
@required this.child,
}) : assert(barrierDismissible != null),
_barrierDismissible = barrierDismissible,
_barrierLabel = barrierLabel;

@override
bool get barrierDismissible => _barrierDismissible;
final bool _barrierDismissible;

@override
String get barrierLabel => _barrierLabel;
final String _barrierLabel;

@override
Color get barrierColor => Colors.black54;

@override
Duration get transitionDuration => const Duration(milliseconds: 150);

@override
Duration get reverseTransitionDuration => const Duration(milliseconds: 75);

/// The primary contents of the modal.
final Widget child;

@override
Widget buildPage(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
final ThemeData theme = Theme.of(context);
return Semantics(
child: SafeArea(
child: Builder(
builder: (BuildContext context) {
return theme != null ? Theme(data: theme, child: child) : child;
},
),
),
scopesRoute: true,
explicitChildNodes: true,
);
}

@override
Widget buildTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
return AnimatedBuilder(
animation: animation,
builder: (BuildContext context, Widget child) {
switch (animation.status) {
case AnimationStatus.forward:
return _EnterTransition(
animation: animation,
child: child,
);
case AnimationStatus.dismissed:
case AnimationStatus.reverse:
case AnimationStatus.completed:
return FadeTransition(
opacity: animation,
child: child,
);
}
return null; // unreachable
},
child: child,
);
}
}

class _EnterTransition extends StatelessWidget {
const _EnterTransition({
this.animation,
this.child,
});

final Animation<double> animation;
final Widget child;

static Animatable<double> fadeInTransition = CurveTween(
curve: const Interval(0.0, 0.3),
);
static Animatable<double> scaleInTransition = Tween<double>(
begin: 0.80,
end: 1.00,
).chain(CurveTween(curve: decelerateEasing));

@override
Widget build(BuildContext context) {
return FadeTransition(
opacity: fadeInTransition.animate(animation),
child: ScaleTransition(
scale: scaleInTransition.animate(animation),
child: child,
),
);
}
}
64 changes: 64 additions & 0 deletions packages/animations/test/fade_transition_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2019 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:animations/src/fade_transition.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';

void main() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will probably need a few more tests?

@shihaohong shihaohong Jan 14, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! It's still a draft PR so it's not quite there yet -- I'll also need to write tests for modal.dart and address the earlier code review comments

testWidgets(
'showModalWithFadeTransition builds a new route',
(WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(builder: (BuildContext context) {
return Center(
child: RaisedButton(
onPressed: () {
showModalWithFadeTransition(
context: context,
child: const _FlutterLogoModal(),
);
},
child: Icon(Icons.add),
),
);
}),
),
),
);
await tester.tap(find.byType(RaisedButton));
await tester.pumpAndSettle();
expect(find.byType(_FlutterLogoModal), findsOneWidget);
},
);
}

class _FlutterLogoModal extends StatelessWidget {
const _FlutterLogoModal();

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: ConstrainedBox(
constraints: const BoxConstraints(
maxHeight: 300,
maxWidth: 300,
minHeight: 250,
minWidth: 250,
),
child: const Material(
child: Center(child: FlutterLogo(size: 250)),
),
),
),
],
);
}
}