A quick routing mechanism for Flutter to handle deep linking on a hurry.
Import the package and just create as many QuickRoute widgets as you need, they can be nested in which case they will start analazing the path where their parent left of.
flutter pub add quick_routerIn this example onGenerateRoute is being used, but the QuickRoute just needs a path (a url) and it will cascade down while analyzing the path's segments.
void main() {
runApp(MaterialApp(
onGenerateRoute: (settings) {
String url = settings.name; // url to use as base...
return QuickRoute(path: url, match:"/library", child:Column(children:[
QuickRoute(math:"/horror", child:Text("Horror books")), // will run on /library/horror
QuickRoute(math:"/drama", child:Text("Drama books")), // will run on /library/drama
]));
}
));
}QuickRoute is a widget that IF it has a path it will act as the main route. Any children QuickRoute will start analizing the path after the parent eat it's match from the path and so on...
Each route creates a context that can be accessed calling context.quickRouteContext? and exposes a method param to get the parameters (if any)
The method T param<T>(String paramKey) allows you to cast the param to a specific type.
You get them from the build context of your widget, like so:
@override
Widget build(BuildContext context) {
// if the url is /some/url/:someKey/etc/:count
// and the real url is: /some/url/magic/etc/123
var myParamValue = context.quickRouteContext?.param<String>("someKey"); // = magic
var myParamValue2 = context.quickRouteContext?.param<int>("count"); // = 123
return Text("child param: $myParamValue;");
}There are 3 types of parameters:
- URL SEGMENT that look like this
/some/:wildcard/pathin this case wildcard will be a parameter that will match anything between the slashes.
// let's the match pattern was: /some/awesome/:wildcard/path
// and path is: /some/awesome/impressive/path
var myParamValue = context.quickRouteContext?.param<String>("wildcard"); //== "impressive"- QUERY STRING anything in the querystring portion will be added to the params bag with the
?prefix. So/some/url?nice=string&foo=barwill add?niceand?footo the params.
// let's the match pattern was: /some/awesome/path
// and path is: /some/awesome/path?and=some&query=value
var myParamValue = context.quickRouteContext?.param<String>("?query"); //== "value"- HASH same as with querystring but for hash values:
/some/path#foo=bar&batman=foreverwill have#fooand#batmanin the params
// let's the match pattern was: /some/awesome/path
// and path is: /some/awesome/path#and=some&query=value
var myParamValue = context.quickRouteContext?.param<String>("#query"); //== "value"