Flutter 路由

550 阅读1分钟

路由

import 'package:flutter/material.dart';  
import 'package:flutter_example/model/video_model.dart';  
import 'package:flutter_example/page/home_page.dart';  
import 'package:flutter_example/page/video_detail_page.dart';  
  
void main() {  
  runApp(MyApp());  
}  
  
class MyApp extends StatefulWidget {  
  @override  
  State<StatefulWidget> createState() => _MyAppState();  
}  
  
class _MyAppState extends State<MyApp> {  
  ARouterDelegate _routerDelegate = ARouterDelegate();  
  ARouteInformationParser _routeInformationParser =  
  ARouteInformationParser();  
  
  @override  
  Widget build(BuildContext context) {  
    print('_MyAppState:build');  
    //定义route  
  var widget = Router(  
      routeInformationParser: _routeInformationParser,  
      routerDelegate: _routerDelegate,  
  
      ///routeInformationParser为null时可缺省,routeInformation提供者  
      routeInformationProvider: PlatformRouteInformationProvider(  
          initialRouteInformation: RouteInformation(location: '/')),  
    );  
    return MaterialApp(home: widget);  
  }  
}  
  
class ARouterDelegate extends RouterDelegate<ARoutePath>  
    with ChangeNotifier, PopNavigatorRouterDelegateMixin<ARoutePath> {  
  final GlobalKey<NavigatorState> navigatorKey;  
  ARoutePath path;  
  List<MaterialPage> pages = [];  
  VideoModel videoModel;  
  
  //为Navigator设置一个key,必要的时候可以通过navigatorKey.currentState来获取到NavigatorState对象  
  ARouterDelegate() : navigatorKey = GlobalKey<NavigatorState>();  
  
  @override  
  Widget build(BuildContext context) {  
    //构建路由栈  
    pages = [  
      pageWrap(HomePage(  
        onJumpToDetail: (mo) {  
          this.videoModel = mo;  
          notifyListeners();  
        },  
      )),  
      if (videoModel != null)  
        pageWrap(  
          VideoDetailPage(videoModel),  
        )  
    ];  
    print('ARouterDelegate:build');  
    return Navigator(  
      key: navigatorKey,  
      pages: pages,  
      onPopPage: (route, result) {  
        print('Navigator:onPopPage');  
        //在这里可以控制是否可以返回  
  if (!route.didPop(result)) {  
          return false;  
        }  
        return true;  
      },  
    );  
  }  
  
  //路由初始化时,Router 会调用setNewRoutePath 方法来更新应用程序的路由状态:  
  @override  
  Future<void> setNewRoutePath(ARoutePath path) async {  
    print('ARouterDelegate:setNewRoutePath:$path');  
    this.path = path;  
  }  
}  
  
///可缺省,主要应用与web,持有RouteInformationProvider 提供的 RouteInformation ,可以将其解析为我们定义的数据类型。  
class ARouteInformationParser extends RouteInformationParser<ARoutePath> {  
  @override  
  Future<ARoutePath> parseRouteInformation(  
      RouteInformation routeInformation) async {  
    final uri = Uri.parse(routeInformation.location);  
    print('ARouteInformationParser:parseRouteInformation:uri:$uri');  
    if (uri.pathSegments.length == 0) {  
      return ARoutePath.home();  
    }  
    return ARoutePath.detail();  
  }  
}  
  
///定义路由path  
class ARoutePath {  
  final String location;  
  
  ARoutePath.home() : location = "/";  
  
  ARoutePath.detail() : location = "/detail";  
}  
  
///创建Page  
pageWrap(Widget child) {  
  return MaterialPage(  
    key: ValueKey(child.hashCode),  
    child: child,  
  );  
}