diff --git a/lib/definitions.dart b/lib/definitions.dart index 3095c14..b66ee98 100644 --- a/lib/definitions.dart +++ b/lib/definitions.dart @@ -1,5 +1,6 @@ -abstract class IState { +class StateTransition { Branch branch; + bool reverse = false; } typedef void StateSet(); diff --git a/lib/graph/renderGraph.dart b/lib/graph/renderGraph.dart index 6a6dce6..3f107e1 100644 --- a/lib/graph/renderGraph.dart +++ b/lib/graph/renderGraph.dart @@ -3,22 +3,22 @@ import '../node/login.dart'; import 'package:flutter/material.dart'; abstract class Transition { - Widget create(Animation animation, Widget from, Widget to); + Widget create(Animation animation, Widget from, Widget to, bool reverse); } class PageSlideTransition implements Transition { - Widget create(Animation animation, Widget from, Widget to) { + Widget create(Animation animation, Widget from, Widget to, bool reverse) { return Stack(textDirection: TextDirection.ltr, children: [ SlideTransition( position: Tween( 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( - begin: const Offset(1.0, 0.0), + begin: Offset(reverse ? -1.0 : 1.0, 0.0), end: Offset.zero, ).animate(animation), child: to, @@ -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) { @@ -48,11 +48,14 @@ 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; @@ -60,7 +63,7 @@ class RenderGraph { } } - static WidgetState _build(IState state) { + static WidgetState _build(StateTransition state) { // Compiled state management if (state.branch == Branch.login) return WidgetState(LoginNode.render(state), @@ -81,4 +84,4 @@ class WidgetState { final Widget widget; final Transition transition; -} \ No newline at end of file +} diff --git a/lib/graph/stateGraph.dart b/lib/graph/stateGraph.dart index 155bdd1..34545ab 100644 --- a/lib/graph/stateGraph.dart +++ b/lib/graph/stateGraph.dart @@ -4,21 +4,22 @@ import '../repository/api.dart'; class StateGraph { static StateSet _triggerBuild; - static List _state = new List(); + static List _state = new List(); 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(); } @@ -26,7 +27,7 @@ class StateGraph { // Get token from state graph static ApiState apiState() => ApiState(send, "token"); - static IState current() { + static StateTransition current() { return _state[_state.length - 1]; } } \ No newline at end of file diff --git a/lib/node/login.dart b/lib/node/login.dart index 48b0d0f..4454741 100644 --- a/lib/node/login.dart +++ b/lib/node/login.dart @@ -3,7 +3,7 @@ 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; } @@ -11,15 +11,15 @@ 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), @@ -56,7 +56,7 @@ class LoginNode { ]))); } - static IState login( + static StateTransition login( LoginRequest loginRequest, String username, String password) { final result = loginRequest(username, password);