Skip to content

Instantly share code, notes, and snippets.

@lukepighetti
Last active June 11, 2019 14:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukepighetti/5bcc3bf50a22ef55a0da2bf3065d4a2a to your computer and use it in GitHub Desktop.
Save lukepighetti/5bcc3bf50a22ef55a0da2bf3065d4a2a to your computer and use it in GitHub Desktop.
Simple string router

A simple way to do string based routing using Dart's excellent Uri parser.

  1. Create a Router with routes.
  2. Push routes with strings that contain query parameters, or push the parameters as a map via route arguments.

This is meant to be very simple and allow deep linking, eg: onDeepLink(url)=>Navigator.of(context).pushNamed(url)

import 'package:flutter/material.dart';
import 'router.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final _router = new Router({
"/": (params) => MyHomePage(
title: params["title"],
),
"home": (params) => MyHomePage(
title: params["title"],
),
"test": (params) => MyHomePage(
title: params["title"],
),
});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
onUnknownRoute: _router.handleRoute,
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title ?? "No title"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
child: Text("/"),
onPressed: () => Navigator.of(context).pushNamed("/"),
),
RaisedButton(
child: Text("home"),
onPressed: () => Navigator.of(context).pushNamed("home"),
),
RaisedButton(
child: Text("test"),
onPressed: () => Navigator.of(context).pushNamed("test"),
),
RaisedButton(
child: Text("test?title=My Test"),
onPressed: () =>
Navigator.of(context).pushNamed("test?title=My Test"),
),
RaisedButton(
child: Text("test?title=My Test, {'thing': true}"),
onPressed: () => Navigator.of(context).pushNamed(
"test?title=My Test",
arguments: {'thing': true}),
),
],
),
));
}
}
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart' show MaterialPageRoute;
typedef RoutedWidget = Widget Function(Map<String, dynamic> parameters);
class Router {
Map<String, RoutedWidget> routes;
Router(this.routes);
Route<dynamic> handleRoute(RouteSettings routeSettings) {
final uri = Uri.parse(routeSettings.name);
final arguments = routeSettings.arguments is Map<String, dynamic>
? routeSettings.arguments as Map<String, dynamic>
: Map<String, dynamic>();
final route = uri.path;
final parameters = Map<String, dynamic>()
..addAll(arguments)
..addAll(uri.queryParameters);
print({
"route": route,
"parameters": parameters,
});
final builder = routes[route];
return MaterialPageRoute(builder: (_) => builder(parameters));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment