Created
March 23, 2023 04:05
-
-
Save corrupt952/e988394fbf9049c3a60495687e6fb8ea to your computer and use it in GitHub Desktop.
show current path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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