Flutter file based routing - File based routing and nested layouts for Flutter

Overview

github pages

Flutter File Based Routing

I was inspired by the routing in remix.run with nested layouts and server side components so I decided to experiment with flutter.

Since this needs to be at compile time I wrote a generator to parse the pages directory for file based routing path names to define the regex like regex_router.

Demo

Installation

You need to install dart locally on your machine then you can run the following at your project directory:

dart generator/bin/main.dart

This will generate a generated.g.dart which can be used to import the generated widget to run the application.

import 'package:flutter/material.dart';

import 'generated.g.dart';

void main() {
  runApp(GeneratedApp(
    themeMode: ThemeMode.system,
    theme: ThemeData.light(),
    darkTheme: ThemeData.dark(),
  ));
}

I also included a router.dart that is needed by the generator and all local widgets.

Defining a base layout

You can define a base layout with the root name. For example: about.dart

import 'package:flutter/material.dart';

import '../generated.g.dart';

class AboutPage extends UiRoute<void> {
  @override
  Widget builder(BuildContext context, void data, Widget? child) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('About'),
      ),
      body: child,
    );
  }
}

If you notice the child can be null and is used for the nested layout.

This is not a widget but instead a class we can use to optionally load data in.

Defining the index route

You can define the index route for when there are no args needed. For example: about/index.dart

import 'package:flutter/material.dart';

import '../../generated.g.dart';

class AboutDetails extends UiRoute<void> {
  @override
  Widget builder(BuildContext context, void data, Widget? child) {
    return const Center(
      child: Text('About'),
    );
  }
}

Since this is a nested layout all you need to do is provide the component and it will inherit from the parent layout (about.dart).

Defining a named arg

You can define a named arg for a route if there is something that does not need data fetched for. For example: /about/guest.dart

import 'package:flutter/material.dart';

import '../../generated.g.dart';

class GuestPage extends UiRoute<void> {
  @override
  Widget builder(BuildContext context, void data, Widget? child) {
    return const Center(
      child: Text('Guest'),
    );
  }
}

This is also just a component.

Defining a dynamic arg

Sometimes the arg is generated at runtime or needs to be pulled from a database. For example: about/:id.dart

import 'package:flutter/material.dart';

import '../../generated.g.dart';

class AccountPage extends UiRoute<Map<String, String>> {
  @override
  loader(route, args) => args;

  @override
  Widget builder(
      BuildContext context, Map<String, String> data, Widget? child) {
    return Center(
      child: Text('ID: ${data['id']}'),
    );
  }
}

You can see we set the file name with a prefix of : to define an arg to look for and match against. This will be provided in a map.

The loader can be used to pull data from a database but in this case it returns the arg map. By default it returns null.

The loader runs before the widget is built.

Routing

To navigate to another page, instead of using Navigator.of(context) you will need to dispatch the following event:

RoutingRequest('ROUTE_HERE').dispatch(context)

ROUTE_HERE should be the named of your route like /about/30.

Storing state

Everything should be stateless, but in the example you can see that even bottom tab navigation index can be done just with the route.

Conclusion

This solves a variety of layout issues and can provide pretty urls while also only loading the data once and caching if needed.

You might also like...

A simple flutter app that downloads a file from the internet, shows a custom-made download progress dialog and saves the file to device's internal storage

http_downloader A simple flutter app that downloads a file from the internet using the http plugin. It has a custom-designed progress dialog which dis

Apr 6, 2021

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 routing package that lets you navigate through guarded page stacks and URLs using the Router and Navigator's Pages API, aka

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

Jan 7, 2023

Flutter getx template - A Flutter Template using GetX package for State management, routing and Dependency Injection

Flutter getx template - A Flutter Template using GetX package for State management, routing and Dependency Injection

Flutter GetX Template (GetX, Dio, MVVM) This Flutter Template using GetX package

Aug 27, 2022

This project is a rebuild of the existing movie colony https://github.com/debbsefe/Movie-Colony. Here's also a link to the figma file https://www.figma.com/file/XpLFNEsROiN1z6lwnNHMrU/Movie-app?node-id=2956%3A10161

Tvfiy Generated by the Very Good CLI 🤖 A Very Good Project created by Very Good CLI. Getting Started 🚀 This project contains 3 flavors: development

Nov 12, 2022

Doctor Consultation App in Flutter containing splash screen on boarding screen Routing state management Dash board Bottom navigation Decorated Drawer and Doctors Screen in the last.

Doctor Consultation App in Flutter containing splash screen on boarding screen  Routing  state management Dash board Bottom navigation Decorated Drawer and Doctors Screen in the last.

Online doctor Consultation App UI in Flutter Doctor Consultation App UI in Flutter Visit Website Features State Management Navigation Bar Responsive D

Jan 1, 2023

A flutter application for Navigation and routing issues

A flutter application for Navigation and routing issues.. The Application is about the meals, in which description of the meals is provided and the proper management of routes and navigation has been implemented.

May 22, 2022

Generates a new Flutter app with http client, theme, routing and localization features.

Starter Template Generates a new Flutter app with http client, theme, routing and localization features. Brick Uses: dio as http client pretty_dio_log

Nov 3, 2022

Let's create a selectable Flutter Navigation Drawer with routing that highlights the current item within the Flutter Sidebar Menu.

Let's create a selectable Flutter Navigation Drawer with routing that highlights the current item within the Flutter Sidebar Menu.

Flutter Tutorial - Sidebar Menu & Selectable Navigation Drawer Let's create a selectable Flutter Navigation Drawer with routing that highlights the cu

Dec 26, 2022

ToDo App made with flutter which stores your todos based on their categories. The data is stored in external application storage in your device in JSON file.

ToDo App made with flutter which stores your todos based on their categories. The data is stored in external application storage in your device in JSON file.

⭐ My ToDo ⭐ Built with ❤︎ by Akash Debnath This is my second project on Flutter. This app hepls you to keep record of your ToDos. You can create your

Dec 25, 2022
Owner
Rody Davis
Developer Advocate for @material-components at @Google
Rody Davis
Widget to count the amount of nested widget tree, useful in the dynamic construction of the interface when it is important to know the depth of widget.

widget_tree_depth_counter Widget Tree Depth Counter WidgetTreeDepthCounter is a simple widget to count the amount of nested widget tree, useful in the

Riccardo Cucia 4 Aug 1, 2022
Collection of cool Layouts built with Flutter to Inspire Other UI developers and explore the possibilities of Flutter.

Awesome_Flutter_Layouts Would you like to Contribute your Designs? Please refer the Contribution guidelines before you dive In Need help? or Looks Som

Mahesh Jamdade 103 Nov 22, 2022
Responsive Layouts and Text for Flutter

About flutter_responsive plugin This plugin provides a easy and productive way to work with responsive layouts for Flutter Applications in mobile, des

Rafael Setragni 11 Aug 15, 2021
Super Useful Flutter Layouts - Right in Your Pocket. 😉

Super Useful Flutter Layouts - Right in Your Pocket. ?? Update: Flutter web app preview here: https://flutter-layouts-demo.web.app/ YouTube video walk

Andrea Bizzotto 1.2k Jan 8, 2023
MetaFlutter - A tool to build Flutter layouts on-device

MetaFlutter MetaFlutter is a project to create Flutter layouts on device. Learn, explore and experiment with Flutter widgets directly on your phone. M

Deven Joshi 162 Nov 22, 2022
Additional alignments to help make your layouts more readable (TopLeft, TopRight, etc)

extra alignments Why should Center get all the love? This package adds additional alignments to help make your layouts more readable. The full set inc

gskinner team 13 Jan 6, 2023
Package to select layout per orientation or device size like mobile vs tablet layouts or portrait vs landscape

proxy_layout Package to select layout per orientation or device size like mobile vs tablet layouts or portrait vs landscape Usage You have two widgets

Jimmy Aumard 8 Apr 18, 2021
Helper for building advanced multi child layouts.

About Boxy is designed to overcome the limitations of Flutter's built-in layout widgets, it provides utilities for flex, custom multi-child layouts, d

Andre 329 Dec 12, 2022
null 0 Feb 16, 2022
Another breakpoint framework. Aims to simplify as much as possible building adaptive layouts.

Another breakpoint framework. Aims to simplify as much as possible building adaptive layouts. Features Really simple implementation Works with and wit

null 3 Sep 26, 2022