Skip to content

Instantly share code, notes, and snippets.

@RyouMon
Created November 13, 2022 02:37
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 RyouMon/ce0a0091a3b79caeb491cf00382d95c2 to your computer and use it in GitHub Desktop.
Save RyouMon/ce0a0091a3b79caeb491cf00382d95c2 to your computer and use it in GitHub Desktop.
Pass args to named route
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