Router_generator is a flutter library for router generation

Overview

router_generator is a flutter library for router generation.

中文文档

pub package

install

dev_dependencies:
 router_generator: ^0.1.2
 build_runner:

import

import 'package:router_generator/router_generator.dart';

mark page

@Router('third')
class ThirdPage extends StatefulWidget {

Mark the page widget with Router annotation,provide the page name as key.

mark argumengs

@inject
class ThirdPageState extends State {
 @RouterParam(required: true)
 Person person;
 @RouterParam(key: 'set_key')
 bool setKey = false;
 @routerParam
 Map map;

Annotated the state needs dependencies with inject.Add arguments in state directly, and annotated them with RouterParamRouterParam has two args:

  • key: The key of the param, use the field name instead if not provided
  • required:If true, the arguments from route must contains this param If neither params needed,userouterParam is recommended. From the example above,every types in dart are supported, including custom type

code generation

This library builds on top of source_gen, so you can run the command:flutter packages pub run build_runner build, refer to build_runner for more detail. Generated dart files including:

  • A $root_file.router_table.dart, has a list of page names and a method for creating widget by page name,root_file is the router table file's prefix.
  • several $page.inject.dart,the page is your file that contains the marked state, like foo.dart to foo.inject.dart main.dart is the default root file of router_tablefile, it can be configured in build.yaml:
targets:
  $default:
    builders:
      router_generator|router_combining:
        options:
          router_table_root_file: "router.dart"

Use it 

GenerateRoute

MaterialApp(
  ...
  onGenerateRoute: (RouteSettings settings) {
    String pageName = settings.name;
    var arguments = settings.arguments;
    if (arguments is Map) {
      deliverParams(pageName, arguments);
    }
    return MaterialPageRoute(builder: (_) {
      return getWidgetByPageName(pageName);
    });
  },
);

In the onGenerateRoute , call $root_file.router_table.dart's getWidgetByPageName,get the page widget,and use deliverParams() to deliver params. For the embedding App:

onGenerateRoute: (RouteSettings settings) {
    String route = settings.name;
    Uri uri = Uri.parse(route);
    var pageName = uri.path.replaceFirst(RegExp('/'), '');
    lastRouteParams = uri.queryParameters;
    return PageRouteBuilder(pageBuilder: (BuildContext context,
        Animation animation, Animation secondaryAnimation) {
      return getWidgetByPageName(pageName);
    });
  },

The params in URI, Map is supported, no needs for extra transform.

dependency injection

In the state needs dependencies, import the corresponding inject file, and inject before use them:

@override
void initState() {
 super.initState();
 injectDependencies(this);
 doSometing();
}

pass argumens

Navigator.of(context).pushNamed('second',
 arguments: {'name': 'bar', 'count': 666});
 

You can create the arguments by hand,but the method in inject file is better

Navigator.of(context).pushNamed('second',
 arguments: createRouteArgs(name: 'bar', count: 666));
You might also like...

Flutter-Animated-Library-of-Books - Flutter App - Animated Book Library

bookshelf A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if thi

Dec 4, 2022

Flutter book library - Book Library Application with Flutter and Google Book API

Book Library Application Flutter iOS, Android and Web with Google Book API Demo

Jan 25, 2022

Flutter Control is complex library to maintain App and State management. Library merges multiple functionality under one hood. This approach helps to tidily bound separated logic into complex solution.

Flutter Control is complex library to maintain App and State management. Library merges multiple functionality under one hood. This approach helps to tidily bound separated logic into complex solution.

Flutter Control is complex library to maintain App and State management. Library merges multiple functionality under one hood. This approach helps to

Feb 23, 2022

[Flutter Library] Flamingo is a firebase firestore model framework library. 🐤

[Flutter Library] Flamingo is a firebase firestore model framework library. 🐤

Flamingo Flamingo is a firebase firestore model framework library. https://pub.dev/packages/flamingo 日本語ドキュメント Example code See the example directory

Sep 22, 2022

This library provides the easiest and powerful Dart/Flutter library for Mastodon API 🎯

This library provides the easiest and powerful Dart/Flutter library for Mastodon API 🎯

The Easiest and Powerful Dart/Flutter Library for Mastodon API 🎯 1. Guide 🌎 1.1. Features 💎 1.2. Getting Started ⚡ 1.2.1. Install Library 1.2.2. Im

Jul 27, 2023

A middleware library for Dart's http library.

http_middleware A middleware library for Dart http library. Getting Started http_middleware is a module that lets you build middleware for Dart's http

Oct 23, 2021

🎯 This library automatically generates object classes from JSON files that can be parsed by the freezed library.

🎯 This library automatically generates object classes from JSON files that can be parsed by the freezed library.

The Most Powerful Way to Automatically Generate Model Objects from JSON Files ⚡ 1. Guide 🌎 1.1. Features 💎 1.1.1. From 1.1.2. To 1.2. Getting Starte

Nov 9, 2022

QR.Flutter is a Flutter library for simple and fast QR code rendering via a Widget or custom painter.

QR.Flutter is a Flutter library for simple and fast QR code rendering via a Widget or custom painter.

QR.Flutter is a Flutter library for simple and fast QR code rendering via a Widget or custom painter. Need help? Please do not submit an issue for a "

Jan 8, 2023

Web3-demo-flutter - A demo for the flutter web3 library.

web3_demo_flutter A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you starte

Oct 7, 2022
Owner
Jelly Bean
Stay Simple Stay Naive
Jelly Bean
A wrapper around Navigator 2.0 and Router/Pages to make their use a easier.

APS Navigator - App Pagination System This library is just a wrapper around Navigator 2.0 and Router/Pages API that tries to make their use easier: ??

Guilherme Silva 14 Oct 17, 2022
flutter_ssf是一个推崇使用Provider、Custom Router、dio结合的MVVM开发模式设计的Flutter应用生产级开发脚手架。

?? ?? flutter_ssf ?? ?? flutter_ssf是一个推崇使用Provider、Custom Router、dio结合的MVVM开发模式设计的Flutter应用生产级开发脚手架。 flutter_ssf只提供基本的参照组件,所以具备几乎所有业务场景中拿来即用的特性。 flutt

Assrce 11 Nov 8, 2022
Declaratively switch child widgets based on the current `Router` location.

Features Declaratively switch child widgets based on the current Router location. class SideBar extends StatelessWidget { Widget build(_){ re

gskinner team 7 Dec 12, 2022
A routing package that lets you navigate through guarded page stacks and URLs using the Router and Navigator's Pages API, aka "Navigator 2.0".

A Flutter package to help you handle your application routing and synchronize it with browser URL. Beamer uses the power of Router and implements all

Sandro Lovnički 485 Jan 7, 2023
💘 This is my Youtube tutorial of my Social Media App Generation Made in Flutter 💘

?? Generation Tutorial ?? ⌛ This is the project source code of my youtube video tutorial of ⌛ ?? Flutter Social Media App Tutorial 2021 ?? ?? Tutorial

Samarpan Dasgupta 17 Nov 24, 2022
This is an opinionated code-generation tool from GraphQL to Dart/Flutter.

GraphQL Codegen This is an opinionated code-generation tool from GraphQL to Dart/Flutter. It'll allow you to generate Dart serializers and client help

United Traders 1 Dec 29, 2021
Create dart data classes easily, fast and without writing boilerplate or running code generation.

Dart Data Class Generator Create dart data classes easily, fast and without writing boilerplate or running code generation. Features The generator can

null 186 Feb 28, 2022
Automatic source code generation for Dart

Overview source_gen provides utilities for automated source code generation for Dart: A framework for writing Builders that consume and produce Dart c

Dart 418 Dec 30, 2022
POC fit-generation

fit_generation POC for fit-generation Table of content Table of content Project Description Getting started Git commit rules Navigation Deep Linking N

Andy 2 Jun 3, 2022
Simple Dart package with build-in code generation. It simplifies and speedup creation of cache mechanism for dart classes.

Simple Dart package with build-in code generation. It simplifies and speedup creation of cache mechanism for dart classes.

iteo 37 Jan 2, 2023