Skip to content

Instantly share code, notes, and snippets.

@corrupt952
Created March 23, 2023 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corrupt952/e988394fbf9049c3a60495687e6fb8ea to your computer and use it in GitHub Desktop.
Save corrupt952/e988394fbf9049c3a60495687e6fb8ea to your computer and use it in GitHub Desktop.
show current path
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(title: 'MyApp', theme: ThemeData.light(), routes: {
HomePage.routeName(): (context) => const HomePage(),
SecondPage.routeName(): (context) => const SecondPage(),
});
}
}
class HomePage extends StatelessWidget {
static String routeName() => '/';
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text('HomePage'),
const CurrentRouteText(),
ElevatedButton(
child: const Text('Next'),
onPressed: () =>
Navigator.of(context).pushNamed(SecondPage.routeName()),
),
],
),
),
);
}
}
class SecondPage extends StatelessWidget {
static String routeName() => '/second';
const SecondPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Text('SecondPage'),
CurrentRouteText(),
],
),
),
);
}
}
class CurrentRouteText extends StatelessWidget {
const CurrentRouteText({super.key});
@override
Widget build(BuildContext context) {
String path = ModalRoute.of(context)!.settings.name ?? '';
return Text('Current: $path');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment