flutter_ajanuw_router 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new50

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]

[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.2.0

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]
8
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]
50
Learn more about scoring.

We analyzed this package on Oct 12, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/ajanuw_route.dart.

Run flutter format to format lib/ajanuw_route.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.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test