static_route

Static page navigation router template library.

pub package

Getting Started

在每个可导航页面中增加StaticRoute类型的静态route变量

class HomePage extends StatelessWidget {

  static final route = StaticRoute(
    name: 'HomePage',
    builder: (RouteSettings settings) {
      return MaterialPageRoute(
        settings: settings,
        builder: (context) => HomePage(),
      );
    },
  );

  ...
}

class NextPage extends StatefulWidget {
  NextPage(this.content);

  final String content;

  static final route = StaticRoute(
    name: 'NextPage',
    builder: (RouteSettings settings) {
      return MaterialPageRoute(
        settings: settings,
        builder: (context) => NextPage(settings.arguments),
      );
    },
  );

  @override
  State createState() => _NextPageState();
}

在某个dart文件中,如pages.dart中注册每个可导航页面的route列表

final routeTable = <StaticRoute>[
  HomePage.route,
  NextPage.route,
  ...
];

在应用入口点MaterialApponGenerateRoute中调用Iterable<StaticRoute>的扩展方法generateRoute注册routeTable中的路由器

MaterialApp(
      ...
      onGenerateRoute: routeTable.generateRoute,
      ...
    );

Usage

 Navigator.pushNamed(context, NextPage.route.name, arguments: 'from HomePage');

Libraries

static_route