使用Redux的Futter中的航线

时间:2018-10-26 15:34:28

标签: redux flutter flutter-redux

当我们使用Redux时,在Flutter App中管理路线的最佳位置在哪里?因为即时消息遵循一些示例,其中一些示例在操作中进行路由,而其他示例则在中间件甚至容器中进行路由。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以调度常规操作以触发导航。

class NavigationService {

 static NavigationService _service;

 NavigationService.__internal();

  factory NavigationService.instance(){
     if(_service == null){
     _service = new NavigationService.__internal();
   }
 return _service;
}

final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();

Future<dynamic> pushNamed(String routeName, {dynamic arguments}) {
  return _navigatorKey.currentState.pushNamed(routeName, arguments:arguments);
}

bool pop() {
    return _navigatorKey.currentState.pop();
}

GlobalKey<NavigatorState> get navigatorKey => this._navigatorKey;
}

RouteMiddleware负责与路由相关的所有操作

class RouteMiddleware<Action> extends TypedMiddleware<AppState, Action> {
  RouteMiddleware() : super(null);

  @override
  void call(Store<AppState> store, action, next) {
    if(action is YourRoutingAction){
      NavigationService.getInstance().pushedNamed(action.pageName);
    }
    next(action);
   }
}

将与路由相关的操作作为常规操作发送

goToHomePageAfterDataLoad(store) {
   dataLoadLoadLogic().then((data){
   store.dispatch(RoutingAction(pageName: "HOME"));
  });
}