A router used on Flutter and Flutter WEB.

This library can solve most problems, but it can't solve all problems.

There may be many bugs, please use them carefully on important projects.

install

dependencies:
  flutter_ajanuw_router:

usage

// main.dart
import 'package:example/pages/dog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ajanuw_router/ajanuw_route.dart';
import 'package:flutter_ajanuw_router/ajanuw_routing.dart';
import 'package:flutter_ajanuw_router/flutter_ajanuw_router.dart';

import 'pages/add_user.dart';
import 'pages/admin.dart';
import 'pages/home.dart';
import 'pages/login.dart';
import 'pages/not-found.dart';
import 'pages/user-settings.dart';
import 'pages/users.dart';
import 'pages/user.dart';
import 'service/auth.service.dart';

AjanuwRouter router = AjanuwRouter();

final List<AjanuwRoute> routes = [
  AjanuwRoute(
    path: '',
    redirectTo: '/home',
  ),
  AjanuwRoute(
    path: 'aa',
    redirectTo: '/users/2',
  ),
  AjanuwRoute(
    path: 'home',
    title: 'home',
    builder: (context, r) => Home(),
  ),
  AjanuwRoute(
    path: 'dog/:id',
    builder: (context, r) => Dog(id: r.paramMap['id']),
  ),
  AjanuwRoute(
    path: 'login',
    title: '登陆',
    builder: (context, r) => Title(
      title: '登陆',
      color: Theme.of(context).primaryColor,
      child: Login(),
    ),
    transitionDuration: Duration(seconds: 2),
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      var begin = Offset(1.0, 1.0);
      var end = Offset.zero;
      var curve = Curves.ease;

      var tween = Tween(begin: begin, end: end);
      var curvedAnimation = CurvedAnimation(
        parent: animation,
        curve: curve,
      );
      return SlideTransition(
        position: tween.animate(curvedAnimation),
        child: child,
      );
    },
  ),
  AjanuwRoute(
    path: 'admin',
    title: '控制台',
    builder: (context, r) => Admin(),
    canActivate: [
      (AjanuwRouting routing) {
        if (authService.islogin) return true;
        print(routing.url);
        authService.redirectTo = routing.url;
        router.navigator.pushNamed('/login');
        return false;
      }
    ],
    children: [
      AjanuwRoute(
        title: '添加用户',
        path: 'add-user',
        builder: (context, settings) => AddUser(),
      ),
    ],
  ),
  AjanuwRoute(
    title: '用户组',
    path: 'users',
    builder: (context, r) => Users(),
    children: [
      AjanuwRoute(
        title: '用户详情',
        path: ':id',
        canActivate: [
          (AjanuwRouting routing) {
            // 没有id拒绝访问
            final paramMap = routing.settings.paramMap;
            if (paramMap['id'] == null) {
              router.navigator.pushReplacementNamed('/users');
              return false;
            }

            try {
              int.parse(paramMap['id']);
              return true;
            } catch (e) {
              return false;
            }
          }
        ],
        builder: (BuildContext context, r) {
          // 其实解析参数,放在[User]页面解析比较好,因为可以预防各种问题
          int id = int.parse(r.paramMap['id']);
          return User(id: id);
        },
        children: [
          AjanuwRoute(
            title: '设置',
            path: 'user-settings',
            builder: (context, settings) => UserSettings(),
          ),
        ],
      ),
    ],
  ),
  AjanuwRoute(
    title: '页面未找到',
    path: 'not-found',
    builder: (context, r) => NotFound(),
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      var begin = 0.0;
      var end = 1.0;
      var curve = Curves.ease;
      var tween = Tween<double>(begin: begin, end: end);
      var curvedAnimation = CurvedAnimation(
        parent: animation,
        curve: curve,
      );

      return ScaleTransition(
        scale: tween.animate(curvedAnimation),
        child: child,
      );
    },
  ),
  AjanuwRoute(
    path: "**",
    redirectTo: '/not-found',
  ),
];

var onGenerateRoute = router.forRoot(routes);
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: 'users',
      navigatorObservers: [router.navigatorObserver],
      navigatorKey: router.navigatorKey,
      onGenerateRoute: onGenerateRoute,

      /// 如果设置了这个,拦截将无效
      // onUnknownRoute: (s) {
      //   return MaterialPageRoute(
      //     builder: (_) => Scaffold(
      //       body: Center(child: Text('data'),),
      //     ),
      //   );
      // },
    );
  }
}

How to navigate?

router.navigator.pushNamed('/');
router.navigator.pushNamed('/admin');

router.navigator.pushNamed('/dog/1');


router.navigator.pushNamed( '/users', arguments: 'x',); // /users
router.navigator.pushNamed(1, arguments: user); // /users/1
router.navigator.pushNamed('user-settings'); // /users/1/user-settings
router.navigator.pushNamed('../../3'); // /users/3

router.navigator.pushNamedAndRemoveUntil('/', (_) => false); // [ /home ]
router.navigator.pushNamedAndRemoveUntil('/users', ModalRoute.withName('/'));// error: There is no / in the history, because / is the redirect route
router.navigator.pushNamedAndRemoveUntil('/users', ModalRoute.withName('/home'));// success: [/home, /users]

Libraries

ajanuw_route
ajanuw_route_settings
ajanuw_routing
flutter_ajanuw_router
path
remove_first_string