Skip to content
This repository has been archived by the owner on Aug 10, 2020. It is now read-only.

Commit

Permalink
State Transitions and Reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
adamped committed Sep 14, 2018
1 parent ab0d1f8 commit b35d75a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/definitions.dart
@@ -1,5 +1,6 @@
abstract class IState {
class StateTransition {
Branch branch;
bool reverse = false;
}

typedef void StateSet();
Expand Down
19 changes: 11 additions & 8 deletions lib/graph/renderGraph.dart
Expand Up @@ -3,22 +3,22 @@ import '../node/login.dart';
import 'package:flutter/material.dart';

abstract class Transition {
Widget create(Animation<double> animation, Widget from, Widget to);
Widget create(Animation<double> animation, Widget from, Widget to, bool reverse);
}

class PageSlideTransition implements Transition {
Widget create(Animation<double> animation, Widget from, Widget to) {
Widget create(Animation<double> animation, Widget from, Widget to, bool reverse) {
return Stack(textDirection: TextDirection.ltr, children: <Widget>[
SlideTransition(
position: Tween<Offset>(
begin: Offset.zero,
end: const Offset(-1.0, 0.0),
end: Offset(reverse ? 1.0 : -1.0, 0.0),
).animate(animation),
child: from,
),
SlideTransition(
position: Tween<Offset>(
begin: const Offset(1.0, 0.0),
begin: Offset(reverse ? -1.0 : 1.0, 0.0),
end: Offset.zero,
).animate(animation),
child: to,
Expand All @@ -36,7 +36,7 @@ class RenderGraph {
_state = state;
}

static Widget build(IState state) {
static Widget build(StateTransition state) {
var buildState = _build(state);

if (_current == null) {
Expand All @@ -48,19 +48,22 @@ class RenderGraph {
vsync: _state, duration: const Duration(milliseconds: 1000));

var transition = (buildState.transition as PageSlideTransition)
.create(_animationController, _current, buildState.widget);
.create(_animationController, _current, buildState.widget, state.reverse);

_current = buildState.widget;

_animationController.forward();

state.reverse = false;

return transition;
} else {
return buildState.widget;
}
}
}

static WidgetState _build(IState state) {
static WidgetState _build(StateTransition state) {
// Compiled state management
if (state.branch == Branch.login)
return WidgetState(LoginNode.render(state),
Expand All @@ -81,4 +84,4 @@ class WidgetState {

final Widget widget;
final Transition transition;
}
}
9 changes: 5 additions & 4 deletions lib/graph/stateGraph.dart
Expand Up @@ -4,29 +4,30 @@ import '../repository/api.dart';
class StateGraph {
static StateSet _triggerBuild;

static List<IState> _state = new List<IState>();
static List<StateTransition> _state = new List<StateTransition>();
static initialize(StateSet setState) {
_triggerBuild = setState;
}

static setInitialState(IState state) {
static setInitialState(StateTransition state) {
_state.add(state);
}

static reverse() {
_state.removeAt(_state.length - 1);
_state[_state.length - 1].reverse = true;
_triggerBuild();
}

static apply(IState state) {
static apply(StateTransition state) {
_state.add(state);
_triggerBuild();
}

// Get token from state graph
static ApiState apiState() => ApiState(send, "token");

static IState current() {
static StateTransition current() {
return _state[_state.length - 1];
}
}
10 changes: 5 additions & 5 deletions lib/node/login.dart
Expand Up @@ -3,23 +3,23 @@ import 'package:flutter/material.dart';
import '../graph/stateGraph.dart';
import '../repository/account.dart';

class DefaultLoginState implements IState {
class DefaultLoginState extends StateTransition {
Branch branch = Branch.login;
}

class LoginErrorState extends DefaultLoginState {
String loginErrorMessage = "There was an error";
}

typedef IState LoginFunction(
typedef StateTransition LoginFunction(
LoginRequest loginRequest, String username, String password);

class LoginNode {
static Widget render(IState state) {
static Widget render(StateTransition state) {
return _render(state);
}

static Widget _render(IState state) {
static Widget _render(StateTransition state) {
if (state is LoginErrorState)
return Container(
padding: EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
Expand Down Expand Up @@ -56,7 +56,7 @@ class LoginNode {
])));
}

static IState login(
static StateTransition login(
LoginRequest loginRequest, String username, String password) {
final result = loginRequest(username, password);

Expand Down

0 comments on commit b35d75a

Please sign in to comment.