Created
November 13, 2022 02:37
-
-
Save RyouMon/ce0a0091a3b79caeb491cf00382d95c2 to your computer and use it in GitHub Desktop.
Pass args to named route
This file contains hidden or 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: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
routes: { | |
'/': (context) => const RouterTestRoute(), | |
'tips': (context) { | |
return TipRoute( | |
text: ModalRoute.of(context)!.settings.arguments as String); | |
} | |
}, | |
initialRoute: '/', | |
); | |
} | |
} | |
class RouterTestRoute extends StatelessWidget { | |
const RouterTestRoute({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: ElevatedButton( | |
onPressed: () async { | |
var result = await Navigator.of(context) | |
.pushNamed('tips', arguments: 'text'); | |
print("路由返回值:$result"); | |
}, | |
child: const Text("打开提示页")), | |
); | |
} | |
} | |
class TipRoute extends StatelessWidget { | |
final String text; | |
const TipRoute({super.key, required this.text}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text("提示")), | |
body: Padding( | |
padding: const EdgeInsets.all(18), | |
child: Center( | |
child: Column( | |
children: [ | |
Text(text), | |
ElevatedButton( | |
onPressed: () => Navigator.pop(context, "我是返回值"), | |
child: const Text("返回")) | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment