flutter_ajanuw_router 0.3.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 58

A router used on Flutter and Flutter WEB. #

This library can solve most problems (dynamic routing, route interceptor, transparent routing), but not all problems.

run demo #

$ flutter channel master
$ flutter upgrade
$ flutter config --enable-web
$ git clone https://github.com/januwA/flutter_ajanuw_router.git
$ cd flutter_ajanuw_router/example
$ flutter run -d chrome

install #

dependencies:
  flutter_ajanuw_router:

usage #

// main.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/dog.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';

// [printHistory] Purpose of commissioning
AjanuwRouter router = AjanuwRouter(printHistory: true);

// as:
// I/flutter (20260): (/users)
// I/flutter (20260): (/users, /home)
// I/flutter (20260): (/users, /home, /login)
// I/flutter (20260): (/users, /home, /admin)
// I/flutter (20260): (/users, /home)

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',
    opaque: false, // Only effective if [transitionsBuilder] is set
    barrierDismissible: true, // Only effective if [transitionsBuilder] is set
    barrierColor: Colors.black54, // Only effective if [transitionsBuilder] is set
    builder: (context, r) => Dog(id: r.paramMap['id']),
    transitionDurationBuilder: (AjanuwRouting r) {
      final Map arguments = r.arguments;
      final seconds = arguments != null && arguments['seconds'] != null? arguments['seconds'] : 2;
      return Duration(seconds: seconds ?? 2);
    },
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      final tween = Tween(begin: const Offset(0.0, 1.0), end: Offset.zero);
      final curvedAnimation = CurvedAnimation(
        parent: animation,
        curve: const ElasticInOutCurve(),
      );
      return SlideTransition(
        position: tween.animate(curvedAnimation),
        child: child,
      );
    },
  ),
  AjanuwRoute(
    path: 'login',
    title: 'Login',
    builder: (context, r) => Title(
      title: 'Login',
      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: 'Admin',
    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: 'Add User',
        path: 'add-user',
        builder: (context, settings) => AddUser(),
      ),
    ],
  ),
  AjanuwRoute(
    title: 'Users',
    path: 'users',
    builder: (context, r) => Users(),
    children: [
      AjanuwRoute(
        title: 'User details',
        path: ':id',
        canActivate: [
          (AjanuwRouting routing) {
            // No id denied access
            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) {
          // In fact, it is better to parse the parameters on the [User] page, because it can prevent various problems
          int id = int.parse(r.paramMap['id']);
          return User(id: id);
        },
        children: [
          AjanuwRoute(
            title: 'settings',
            path: 'user-settings',
            builder: (context, settings) => UserSettings(),
          ),
        ],
      ),
    ],
  ),
  AjanuwRoute(
    title: 'Page Not Found',
    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',
  ),
];

void main() => runApp(MyApp());

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

      /// If this is set, interception will not work
      // 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]

// or use [Navigator]

Navigator.of(context).pushNamed('/');
Navigator.of(context).pushNamed('/admin');
Navigator.of(context).pushNamed('/dog/1');
Navigator.of(context).pushNamed('/users', arguments: 'x',);
Navigator.of(context).pushNamed(1, arguments: user);
Navigator.of(context).pushNamed('user-settings');
Navigator.of(context).pushNamed('../../3');
Navigator.of(context).pushNamedAndRemoveUntil('/', (_) => false);
Navigator.of(context).pushNamedAndRemoveUntil('/users', ModalRoute.withName('/home'));

// or v0.3.1
router.pushNamed<String>('/dog/1').then((s) {
  print(s);
});

About return value #

// Error, you may get the error: type 'PageRouteBuilder <dynamic>' is not a subtype of type 'Route <String>'
Navigator.of(context).pushNamed<String>('/dog/1').then((s) {
  print(s);
});

// Only in this way
// You have a good way, please tell me.
Navigator.of(context).pushNamed('/dog/1').then((s) {
  print(s as String);
});

// v0.3.1 This version rewrites some methods to fix the above problems
router.pushNamed<String>('/dog/1').then((s) {
  print(s);
});

test #

λ flutter test
λ flutter test ./test/ajanuw_route_test.dart

[0.3.1] - 2020/1/12

  • 修复: 拦截器返回null时,出现的错误
  • 修复: 获取路由返回值时的错误
  • 重写了部分api,没找到的可以在[router.navigator]下获取

[0.3.0] - 2020/1/7

  • 优化代码,增加api,部分api变更
  • 优化readme

[0.2.2] - 2019/10/22

  • 修护路由历史记录

[0.2.1] - 2019/10/15

  • 跳过没有设置builder方法的普通路由

[0.2.0] - 2019/10/12

  • api修改

[0.1.4] - 2019/10/7

  • 优化代码

[0.1.3] - 2019/10/7

  • 解决直接注册动态路由时的BUG, [path: 'dog/:id']

[0.1.2] - 2019/10/7

  • 修复[setting.arguments]丢失BUG.

[0.1.1] - 2019/10/6

  • 将[routing]代替[settings]传递给[buider]

[0.1.0] - 2019/10/5

  • A router used on Flutter and Flutter WEB.

example/README.md

example #

A new Flutter project.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_ajanuw_router: ^0.3.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_ajanuw_router/flutter_ajanuw_router.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
23
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
81
Overall:
Weighted score of the above. [more]
58
Learn more about scoring.

We analyzed this package on Jan 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5

Health suggestions

Format lib/ajanuw_routings.dart.

Run flutter format to format lib/ajanuw_routings.dart.

Format lib/flutter_ajanuw_router.dart.

Run flutter format to format lib/flutter_ajanuw_router.dart.

Maintenance suggestions

The package description is too short. (-19 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
path ^1.6.4 1.6.4
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test