This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Support for settings fly-in animation #70
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,12 @@ | |
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:sky/animation/animation_performance.dart'; | ||
import 'package:sky/animation/curves.dart'; | ||
import 'package:sky/widgets/animated_component.dart'; | ||
import 'package:sky/widgets/animation_builder.dart'; | ||
import 'package:sky/widgets/basic.dart'; | ||
import 'package:vector_math/vector_math.dart'; | ||
|
||
typedef Widget Builder(Navigator navigator, RouteBase route); | ||
|
||
|
@@ -27,31 +32,125 @@ class RouteState extends RouteBase { | |
RouteBase route; | ||
Function callback; | ||
|
||
Widget build(Navigator navigator, _) => route.build(navigator, this); | ||
Widget build(Navigator navigator, RouteBase route) => null; | ||
|
||
void popState() { | ||
if (callback != null) | ||
callback(this); | ||
} | ||
} | ||
|
||
// TODO(jackson): Refactor this into its own file | ||
// and support multiple transition types | ||
const Duration _kTransitionDuration = const Duration(milliseconds: 200); | ||
const Point _kTransitionStartPoint = const Point(0.0, 100.0); | ||
enum TransitionDirection { forward, reverse } | ||
class Transition extends AnimatedComponent { | ||
Transition({ | ||
String key, | ||
this.content, | ||
this.direction, | ||
this.onDismissed, | ||
this.interactive | ||
}) : super(key: key); | ||
Widget content; | ||
TransitionDirection direction; | ||
bool interactive; | ||
Function onDismissed; | ||
|
||
AnimatedType<Point> _position; | ||
AnimatedType<double> _opacity; | ||
AnimationPerformance _performance; | ||
|
||
void initState() { | ||
_position = new AnimatedType<Point>( | ||
_kTransitionStartPoint, | ||
end: Point.origin, | ||
curve: easeOut | ||
); | ||
_opacity = new AnimatedType<double>(0.0, end: 1.0) | ||
..curve = easeOut; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
_performance = new AnimationPerformance() | ||
..duration = _kTransitionDuration | ||
..variable = new AnimatedList([_position, _opacity]) | ||
..addListener(_checkDismissed); | ||
if (direction == TransitionDirection.reverse) | ||
_performance.progress = 1.0; | ||
watch(_performance); | ||
_start(); | ||
} | ||
|
||
void _start() { | ||
_dismissed = false; | ||
switch (direction) { | ||
case TransitionDirection.forward: | ||
_performance.play(); | ||
break; | ||
case TransitionDirection.reverse: | ||
_performance.reverse(); | ||
break; | ||
} | ||
} | ||
|
||
void syncFields(Transition source) { | ||
content = source.content; | ||
if (direction != source.direction) { | ||
direction = source.direction; | ||
_start(); | ||
} | ||
interactive = source.interactive; | ||
onDismissed = source.onDismissed; | ||
super.syncFields(source); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interactive ? |
||
} | ||
|
||
bool _dismissed = false; | ||
void _checkDismissed() { | ||
if (!_dismissed && | ||
direction == TransitionDirection.reverse && | ||
_performance.isDismissed) { | ||
if (onDismissed != null) | ||
onDismissed(); | ||
_dismissed = true; | ||
} | ||
} | ||
|
||
Widget build() { | ||
Matrix4 transform = new Matrix4.identity() | ||
..translate(_position.value.x, _position.value.y); | ||
// TODO(jackson): Hit testing should ignore transform | ||
// TODO(jackson): Block input unless content is interactive | ||
return new Transform( | ||
transform: transform, | ||
child: new Opacity( | ||
opacity: _opacity.value, | ||
child: content | ||
) | ||
); | ||
} | ||
} | ||
|
||
class HistoryEntry { | ||
HistoryEntry(this.route); | ||
final RouteBase route; | ||
// TODO(jackson): Keep track of the requested transition | ||
} | ||
|
||
class NavigationState { | ||
|
||
NavigationState(List<Route> routes) { | ||
for (Route route in routes) { | ||
if (route.name != null) | ||
namedRoutes[route.name] = route; | ||
} | ||
history.add(routes[0]); | ||
history.add(new HistoryEntry(routes[0])); | ||
} | ||
|
||
List<RouteBase> history = new List<RouteBase>(); | ||
List<HistoryEntry> history = new List<HistoryEntry>(); | ||
int historyIndex = 0; | ||
Map<String, RouteBase> namedRoutes = new Map<String, RouteBase>(); | ||
|
||
RouteBase get currentRoute => history[historyIndex]; | ||
RouteBase get currentRoute => history[historyIndex].route; | ||
bool hasPrevious() => historyIndex > 0; | ||
bool hasNext() => history.length > historyIndex + 1; | ||
|
||
void pushNamed(String name) { | ||
Route route = namedRoutes[name]; | ||
|
@@ -60,16 +159,15 @@ class NavigationState { | |
} | ||
|
||
void push(RouteBase route) { | ||
// Discard future history | ||
history.removeRange(historyIndex + 1, history.length); | ||
historyIndex = history.length; | ||
history.add(route); | ||
HistoryEntry historyEntry = new HistoryEntry(route); | ||
history.insert(historyIndex + 1, historyEntry); | ||
historyIndex++; | ||
} | ||
|
||
void pop() { | ||
if (historyIndex > 0) { | ||
history[historyIndex].popState(); | ||
history.removeLast(); | ||
HistoryEntry entry = history[historyIndex]; | ||
entry.route.popState(); | ||
historyIndex--; | ||
} | ||
} | ||
|
@@ -115,6 +213,31 @@ class Navigator extends StatefulComponent { | |
} | ||
|
||
Widget build() { | ||
return state.currentRoute.build(this, state.currentRoute); | ||
List<Widget> visibleRoutes = new List<Widget>(); | ||
for (int i = 0; i < state.history.length; i++) { | ||
// TODO(jackson): Avoid building routes that are not visible | ||
HistoryEntry historyEntry = state.history[i]; | ||
Widget content = historyEntry.route.build(this, historyEntry.route); | ||
if (i == 0) { | ||
visibleRoutes.add(content); | ||
continue; | ||
} | ||
if (content == null) | ||
continue; | ||
String key = historyEntry.route.key; | ||
Transition transition = new Transition( | ||
key: key, | ||
content: content, | ||
direction: (i <= state.historyIndex) ? TransitionDirection.forward : TransitionDirection.reverse, | ||
interactive: (i == state.historyIndex), | ||
onDismissed: () { | ||
setState(() { | ||
state.history.remove(historyEntry); | ||
}); | ||
} | ||
); | ||
visibleRoutes.add(transition); | ||
} | ||
return new Stack(visibleRoutes); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ends up not aligned. I think you really want the children indented 4 from the 'child', then the other arguments (and the closing "]") indented 2.