Skip to content

Instantly share code, notes, and snippets.

@buzzySmile
Created June 11, 2021 12:35
Show Gist options
  • Save buzzySmile/46a97b3f55188665d2da2f81e13de3f0 to your computer and use it in GitHub Desktop.
Save buzzySmile/46a97b3f55188665d2da2f81e13de3f0 to your computer and use it in GitHub Desktop.
Flutter clean navigation example
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateRoute: RouteGenerator.generateRoute,
title: 'Navigation with Arguments',
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Screen'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// A button that navigates to a named route.
ElevatedButton(
onPressed: () {
Navigator.pushNamed(
context,
PlainScreen.route,
);
},
child: Text('Navigate to screen without arguments passing'),
),
SizedBox(height: 20),
// A button that navigates to a named route.
// For this route, extract the arguments in
// the onGenerateRoute function and pass them
// to the screen.
ElevatedButton(
onPressed: () {
Navigator.pushNamed(
context,
PassArgumentsScreen.route,
arguments: ScreenArguments(
'Accept Arguments Screen',
'This message is extracted in the onGenerateRoute function.',
),
);
},
child: Text('Navigate to a named that accepts complex arguments'),
),
SizedBox(height: 20),
// When the user taps the button, navigate
// to not existed route (error case)
ElevatedButton(
onPressed: () {
Navigator.pushNamed(
context,
'',
arguments: ScreenArguments(
'Accept Arguments Screen',
'This message is extracted in the onGenerateRoute function.',
),
);
},
child: Text('Navigate to not-existed page'),
),
],
),
),
);
}
}
class PlainScreen extends StatelessWidget {
static const route = '/plainScreen';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Plane Screen'),
),
body: Center(child: Text('Plain screen without any parameters passed')),
);
}
}
// You can pass any object to the arguments parameter.
// In this example, create a class that contains both
// a customizable title and message.
class ScreenArguments {
final String title;
final String message;
ScreenArguments(this.title, this.message);
}
// A Widget that accepts the necessary arguments via the
// constructor.
class PassArgumentsScreen extends StatelessWidget {
static const route = '/passArguments';
final ScreenArguments params;
const PassArgumentsScreen({
Key? key,
required this.params,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(params.title),
),
body: Center(
child: Text(params.message),
),
);
}
}
// ROUTER CLASS. Contains all your navigation scheme
class RouteGenerator {
static Route<dynamic>? generateRoute(RouteSettings settings) {
final Object? args = settings.arguments;
switch (settings.name) {
case PlainScreen.route:
return MaterialPageRoute(builder: (context) => PlainScreen());
case PassArgumentsScreen.route:
if (args is ScreenArguments) {
// extract the required data from
// the arguments and pass the data to the
// correct screen.
return MaterialPageRoute(
builder: (context) {
return PassArgumentsScreen(
params: args,
);
},
);
}
}
return _generateErrorRoute();
}
static Route<dynamic> _generateErrorRoute() {
return MaterialPageRoute(builder: (context) {
return Scaffold(
appBar: AppBar(
title: Text('error'),
),
body: Center(
child: Text('error'),
),
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment