I'm trying to declare my apps navigation. As I am not using MaterialApp
wrapper or styling, all my imports come from package:flutter/widgets.dart
.
As per docs, to use Navigation properly I am doing this:
import "package:flutter/widgets.dart";
import "package:myapp/routes/home.dart";
void main() {
runApp(
new WidgetsApp(/* stuck here */);
)
}
And I am stuck at this step, I am trying to follow Navigator docs and code comments in ide, but I can't figure out how to use onGenerateRoute
property of WidgetsApp
, nor can find full example of this online, hence the question.
Similarly to new MaterialApp()
how do I define my home path and rest of the routes within new WidgetsApp()
?
As a follow up question to this, am I allowed to to use wildcard routes like /onboarding/**
or /dashboard/**
inside new WidgetsApp()
somehow to route into layouts which then have switch statement checking for a route and display pages?
Introduction. A Navigator widget in Flutter is what we use to maintain a stack of routes and it plays a huge role in helping us to navigate between routes.
For example: The push() method adds a Route to the stack of routes managed by the Navigator. Now to return back from that new screen you use the Navigator. pop() method. The pop() method removes the current Route from the stack of routes managed by the Navigator.
WidgetsApp class Null safety. A convenience widget that wraps a number of widgets that are commonly required for an application. One of the primary roles that WidgetsApp provides is binding the system back button to popping the Navigator or quitting the application.
You can use onGenerateRoute
property to generate route for widgets app.
Here is a very minimal implementation of the same:
import 'package:flutter/widgets.dart';
void main() => runApp(new MyWidgetsApp());
class MyWidgetsApp extends StatelessWidget {
Route generate(RouteSettings settings){
Route page;
switch(settings.name){
case "/":
page = new PageRouteBuilder(
pageBuilder: (BuildContext context,Animation<double> animation,
Animation<double> secondaryAnimation){
return new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text("Home Page",textDirection: TextDirection.ltr,),
const Padding(padding: const EdgeInsets.all(10.0)),
new GestureDetector(
onTap: () => Navigator.of(context).pushNamed("/first"),
child: new Container(
padding: const EdgeInsets.all(10.0),
color:Colors.blue,
child: const Text("Go to First Page"),
),
),
const Padding(padding: const EdgeInsets.all(10.0)),
new GestureDetector(
onTap: () => Navigator.of(context).pushNamed("/abcd"),
child: new Container(
padding: const EdgeInsets.all(10.0),
color:Colors.blue,
child: const Text("Unkown Route"),
),
)
],
);
},
transitionsBuilder: (_, Animation<double> animation, Animation<double> second, Widget child) {
return new FadeTransition(
opacity: animation,
child: new FadeTransition(
opacity: new Tween<double>(begin: 1.0, end: 0.0).animate(second),
child: child,
),
);
}
);
break;
case "/first":
page = new PageRouteBuilder(
pageBuilder: (BuildContext context,Animation<double> animation,
Animation<double> secondaryAnimation){
return new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text("First Page",textDirection: TextDirection.ltr,),
const Padding(padding: const EdgeInsets.all(10.0)),
new GestureDetector(
onTap: () => Navigator.of(context).pop(),
child: new Container(
padding: const EdgeInsets.all(10.0),
color:Colors.blue,
child: const Text("Back"),
),
)
]
);
},
transitionsBuilder: (_, Animation<double> animation, Animation<double> second, Widget child) {
return new FadeTransition(
opacity: animation,
child: new FadeTransition(
opacity: new Tween<double>(begin: 1.0, end: 0.0).animate(second),
child: child,
),
);
}
);
break;
}
return page;
}
Route unKnownRoute(RouteSettings settings){
return new PageRouteBuilder(
pageBuilder: (BuildContext context,Animation<double> animation,
Animation<double> secondaryAnimation){
return new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text("First Page",textDirection: TextDirection.ltr,),
const Padding(padding: const EdgeInsets.all(10.0)),
new GestureDetector(
onTap: () => Navigator.of(context).pop(),
child: new Container(
padding: const EdgeInsets.all(10.0),
color:Colors.blue,
child: const Text("Back"),
),
)
]
);
}
);
}
@override
Widget build(BuildContext context) {
return new WidgetsApp(
onGenerateRoute: generate,
onUnknownRoute: unKnownRoute,
textStyle: const TextStyle(),
initialRoute: "/",
color: Colors.red
);
}
}
Hope that helped!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With