Skip to content

Instantly share code, notes, and snippets.

@omishah
Created March 24, 2022 04:46
Show Gist options
  • Save omishah/1b72dac6835750a5f41d2d7c0ace492d to your computer and use it in GitHub Desktop.
Save omishah/1b72dac6835750a5f41d2d7c0ace492d to your computer and use it in GitHub Desktop.
Flutter - Define/include/initialize/declare routes from different dart files
// @author OMi Shah
// @email omi@codecyan.com
// @organization CodeCyan
// @website www.codecyan.com
import 'package:flutter/material.dart';
void main() {
runApp(CodeCyanApp());
}
class CodeCyanApp extends StatelessWidget {
// route 2
final Map<String, WidgetBuilder> routes2 = {
MyWidget2.id: (context) => MyWidget2(),
MyWidget3.id: (context) => MyWidget3()
};
// route 3
final Map<String, WidgetBuilder> routes3 = {
MyWidget4.id: (context) => MyWidget2(),
MyWidget5.id: (context) => MyWidget3()
};
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: MyWidget.id,
routes: {
MyWidget.id: (context) => MyWidget(),
MyWidget1.id: (context) => MyWidget1(),
...routes2,
...routes3,
},
debugShowCheckedModeBanner: false,
home: Scaffold(body: MyWidget()),
);
}
}
class MyWidget extends StatelessWidget {
static const id = "mywidget";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("MyWidget")),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, MyWidget1.id);
},
child: const Text('Go to MyWidget1'))));
}
}
class MyWidget1 extends StatelessWidget {
static const id = "mywidget1";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("MyWidget1")),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, MyWidget2.id);
},
child: const Text('Go to MyWidget2'))));
}
}
class MyWidget2 extends StatelessWidget {
static const id = "mywidget2";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("MyWidget2")),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, MyWidget3.id);
},
child: const Text('Go to MyWidget3'))));
}
}
class MyWidget3 extends StatelessWidget {
static const id = "mywidget3";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("MyWidget3")),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, MyWidget4.id);
},
child: const Text('Go to MyWidget4'))));
}
}
class MyWidget4 extends StatelessWidget {
static const id = "mywidget4";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("MyWidget4")),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, MyWidget5.id);
},
child: const Text('Go to MyWidget5'))));
}
}
class MyWidget5 extends StatelessWidget {
static const id = "mywidget5";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("MyWidget5")),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, MyWidget.id);
},
child: const Text('Go to MyWidget'))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment