Reputation: 314
Thanks in advance, As we all know that we can assign names to pages for route and use Navigator.pushNamed() with this, but what is the best approach?
What I do is to put the route name as a static string in the class itself,
class Home extends StatelessWidget {
static const String id = "/home";
@override
Widget build(BuildContext context) {
return widget;
}
}
So now when I'm assign route in MaterialApp as
routes: {
Home.id: (context) => Home(),
}
and now whenever I'm routing the page, I know which page I'm routing to
Navigator.pushNamed(context, Home.id);
But I've seen people having a different file of routes as
class Routes {
static const String home = "/home";
static Map<String, Widget Function(BuildContext) routes = {
home: (context) => Home(),
}
}
//MaterialApp
routes: Routes.routes
So now can anyone tell me which is the best approach and which one should I use, which is best with performance.
And any better way to manage routing?
Upvotes: 0
Views: 983
Reputation: 11
For me, I use getX package which can be found at: https://pub.dev/packages/get
This is very efficient and easy to use for routing and State Management as well.
You can import getX package and use in place of material space:
GetMaterialApp( // Before: MaterialApp(
home: MyHome(),
)
Here Routes of Screens can be defined as:
void main() {
runApp(
GetMaterialApp(
initialRoute: '/',
getPages: [
GetPage(name: '/', page: () => MyHomePage()),
GetPage(name: '/second', page: () => Second()),
GetPage(
name: '/third',
page: () => Third(),
transition: Transition.zoom
),
],
)
);
}
Named routing can be used for this.
Get.toNamed("/NextScreen", arguments: 'Get is the best');
Routing without getting back to previous screen.
Get.offNamed("/NextScreen");
Upvotes: 0