Skip to content

Commit

Permalink
Reverted some 'fixes'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Salby committed Aug 26, 2019
1 parent 2fc77cf commit 560fe24
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 60 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.2.0+3] - 2019-08-26

### Changed
- Reverted some earlier "fixes".

## [1.2.0+2] - 2019-08-26

### Fixes
Expand Down
39 changes: 39 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class MyApp extends StatelessWidget {
settings: settings,
);
break;
case '/settings/cookies':
return MorpheusPageRoute(
builder: (_) => CookiesScreen(),
settings: settings,
);
break;
case '/create':
return MorpheusPageRoute(
builder: (_) => CreateScreen(),
Expand Down Expand Up @@ -115,12 +121,29 @@ class ProfileScreen extends StatelessWidget {
}

class SettingsScreen extends StatelessWidget {
final cookiesKey = GlobalKey();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Settings'),
),
body: ListView(
children: <Widget>[
ListTile(
key: cookiesKey,
title: Text('Cookies'),
onTap: () => Navigator.of(context).pushNamed(
'/settings/cookies',
arguments: MorpheusRouteArguments(
parentKey: cookiesKey,
),
),
),
Divider(height: 1.0),
],
),
);
}
}
Expand All @@ -140,3 +163,19 @@ class CreateScreen extends StatelessWidget {
);
}
}

class CookiesScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
title: Text('Cookies'),
expandedHeight: 224.0,
),
],
),
);
}
}
47 changes: 24 additions & 23 deletions lib/page_routes/morpheus_page_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,29 @@ class MorpheusPageRoute<T> extends PageRoute<T> {
/// This only affects bidirectional transitions.
final bool scaleChild;

/// Returns a [RenderBox] from a [GlobalKey] if one is provided, either
/// through the [parentKey] parameter or [settings.arguments].
///
/// If no [GlobalKey] is provided, this method returns null.
RenderBox _getRenderBox() {
// Return the stored [RenderBox] if it exists.
if (_renderBox != null) return _renderBox;
RenderBox _findRenderBox() {

final arguments = settings.arguments as MorpheusRouteArguments;
final key = parentKey ?? arguments?.parentKey;

// Return null if [key] is null.
if (key == null) return null;

// Get the [RenderBox].
// Find the [RenderBox] attached to [key].
final renderBox = key.currentContext.findRenderObject();

// Store the [RenderBox] for later.
_renderBox = renderBox;
// If [renderBox] is null but [_renderBox] isn't, return [_renderBox].
if (renderBox == null) {
if (_renderBox != null) {
return _renderBox;
} else {
return null;
}
} else {
_renderBox = renderBox;
return renderBox;
}

return renderBox;
}

RenderBox _renderBox;
Expand All @@ -106,20 +108,20 @@ class MorpheusPageRoute<T> extends PageRoute<T> {

@override
Widget buildPage(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return Builder(builder: builder);
}

@override
Widget buildTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
final routeSettings = settings.arguments as MorpheusRouteArguments;

// Define transition settings.
Expand All @@ -134,13 +136,12 @@ class MorpheusPageRoute<T> extends PageRoute<T> {

// Return page transition.
return MorpheusPageTransition(
size: _getRenderBox().size,
offset: _getRenderBox().localToGlobal(Offset.zero),
renderBox: _findRenderBox(),
context: context,
animation: animation,
secondaryAnimation: secondaryAnimation,
child: child,
settings: transitionSettings,
);
}
}
}
80 changes: 44 additions & 36 deletions lib/page_routes/morpheus_page_transition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import 'package:morpheus/tweens/page_transition_opacity_tween.dart';
/// Builds a parent-child material design navigation transition.
class MorpheusPageTransition extends StatelessWidget {
MorpheusPageTransition({
@required this.size,
@required this.offset,
this.renderBox,
@required BuildContext context,
@required this.animation,
@required this.secondaryAnimation,
Expand All @@ -20,39 +19,46 @@ class MorpheusPageTransition extends StatelessWidget {
assert(child != null),
assert(settings != null),
transitionContext = context,
hasRenderBox = size == null || offset == null;
renderBoxSize = renderBox?.size,
renderBoxOffset = renderBox?.localToGlobal(Offset.zero);

/// Defines the initial [Size] of a parent-child transition.
final Size size;

/// Defines the initial [Offset] of a parent-child transition.
final Offset offset;

final bool hasRenderBox;
/// The [RenderBox] used to calculate the origin of the parent-child
/// transition.
final RenderBox renderBox;

final BuildContext transitionContext;
final Animation<double> animation;
final Animation<double> secondaryAnimation;
final Widget child;
final MorpheusRouteArguments settings;

/// The size of [renderBox].
///
/// Returns null if [renderBox] is null.
final Size renderBoxSize;

/// The calculated [Offset] of [renderBox].
///
/// Returns null if [renderBox] is null.
final Offset renderBoxOffset;

/// Returns true if the parent widget spans the entire width of the screen.
///
/// returns false if [renderBox] is null.
bool get useVerticalTransition {
// Return null if [renderBox] is null.
if (!hasRenderBox) return false;
if (renderBox == null) return false;

final screenWidth = MediaQuery.of(transitionContext).size.width;
return size.width == screenWidth && offset.dx == 0.0;
return renderBoxSize.width == screenWidth && renderBoxOffset.dx == 0.0;
}

/// Returns an [Animation] used to animate the transition from the parent widget
/// to the child widget.
Animation<double> get parentToChildAnimation {
// Define when the animation should end.
final animationEnd =
transitionWidget == null ? 0.3 : useVerticalTransition ? 0.1 : 0.2;
transitionWidget == null ? 0.3 : useVerticalTransition ? 0.1 : 0.2;

return Tween<double>(
begin: 0.0,
Expand All @@ -75,16 +81,16 @@ class MorpheusPageTransition extends StatelessWidget {
/// Returns a [BorderRadiusTween] with an initial value of
/// [settings.borderRadius].
BorderRadiusTween get borderRadius => BorderRadiusTween(
begin: settings.borderRadius ?? BorderRadius.circular(0.0),
end: BorderRadius.circular(0.0),
);
begin: settings.borderRadius ?? BorderRadius.circular(0.0),
end: BorderRadius.circular(0.0),
);

/// Returns a curve that can be applied to all transitions that are
/// synchronized with [positionTween].
Animation<double> get positionAnimationCurve {
// Define where on the timeline the animation should start.
final animationStart =
transitionWidget != null ? 0.0 : useVerticalTransition ? 0.1 : 0.2;
transitionWidget != null ? 0.0 : useVerticalTransition ? 0.1 : 0.2;

return CurvedAnimation(
parent: animation,
Expand All @@ -104,7 +110,7 @@ class MorpheusPageTransition extends StatelessWidget {
/// Returns a [RelativeRectTween] that is used to animate from the origin
/// of [renderBox] to the size of the screen.
RelativeRectTween positionTween(BoxConstraints constraints) {
final origin = offset & size;
final origin = renderBoxOffset & renderBoxSize;
return RelativeRectTween(
begin: RelativeRect.fromLTRB(
origin.left,
Expand All @@ -126,7 +132,7 @@ class MorpheusPageTransition extends StatelessWidget {
// Define at which point during the transition the scrim will start to
// show.
final scrimAnimationStart =
useVerticalTransition ? 0.0 : transitionWidget != null ? 0.0 : 0.2;
useVerticalTransition ? 0.0 : transitionWidget != null ? 0.0 : 0.2;

// Define the scrim animation.
final Animation<Color> scrimAnimation = ColorTween(
Expand All @@ -146,7 +152,7 @@ class MorpheusPageTransition extends StatelessWidget {
),
));

if (!hasRenderBox) {
if (renderBox == null) {
return buildDefaultTransition();
}

Expand All @@ -167,7 +173,7 @@ class MorpheusPageTransition extends StatelessWidget {
// [renderBox] to fill the entire screen.
PositionedTransition(
rect:
positionTween(constraints).animate(positionAnimationCurve),
positionTween(constraints).animate(positionAnimationCurve),
child: AnimatedBuilder(
animation: positionAnimationCurve,
child: OverflowBox(
Expand All @@ -190,8 +196,8 @@ class MorpheusPageTransition extends StatelessWidget {
),
settings.transitionToChild
? useVerticalTransition
? buildVerticalTransition(child)
: buildBidirectionalTransition(child)
? buildVerticalTransition(child)
: buildBidirectionalTransition(child)
: child,
],
),
Expand Down Expand Up @@ -220,9 +226,9 @@ class MorpheusPageTransition extends StatelessWidget {
end: childScreen,
)
.animate(CurvedAnimation(
parent: animation,
curve: Curves.fastOutSlowIn,
))
parent: animation,
curve: Curves.fastOutSlowIn,
))
.value,
);
}
Expand All @@ -231,18 +237,18 @@ class MorpheusPageTransition extends StatelessWidget {
Widget buildBidirectionalTransition(Widget childScreen) {
final Animation<double> scaleParentAnimation = Tween<double>(
begin: 1.0,
end: MediaQuery.of(transitionContext).size.width / size.width,
end: MediaQuery.of(transitionContext).size.width / renderBoxSize.width,
).animate(positionAnimationCurve);
final Animation<double> scaleChildAnimation = Tween<double>(
begin: size.width / MediaQuery.of(transitionContext).size.width,
begin: renderBoxSize.width / MediaQuery.of(transitionContext).size.width,
end: 1.0,
).animate(positionAnimationCurve);
final parentWidget = transitionWidget != null
? ScaleTransition(
alignment: Alignment.center,
scale: scaleParentAnimation,
child: transitionWidget,
)
alignment: Alignment.center,
scale: scaleParentAnimation,
child: transitionWidget,
)
: Container();
return FadeTransition(
opacity: PageTransitionOpacityTween(
Expand All @@ -261,7 +267,9 @@ class MorpheusPageTransition extends StatelessWidget {
: ConstantTween<double>(1.0).animate(animation),
child: childScreen,
),
).animate(positionAnimationCurve).value,
)
.animate(positionAnimationCurve)
.value,
);
}

Expand Down Expand Up @@ -338,8 +346,8 @@ class MorpheusPageTransition extends StatelessWidget {
padding: widget.padding,
decoration: widget.decoration,
foregroundDecoration: widget.foregroundDecoration,
width: size.width,
height: size.height,
width: renderBoxSize.width,
height: renderBoxSize.height,
constraints: widget.constraints,
margin: widget.margin,
transform: widget.transform,
Expand Down Expand Up @@ -369,4 +377,4 @@ class MorpheusPageTransition extends StatelessWidget {
break;
}
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: morpheus
description: A Flutter package for easily implementing Material Design navigation transitions.
version: 1.2.0+2
version: 1.2.0+3
author: Sander Dalby Larsen <[email protected]>
homepage: https://github.com/salby/morpheus

Expand Down

0 comments on commit 560fe24

Please sign in to comment.