Skip to content

Instantly share code, notes, and snippets.

@RyouMon
Created November 12, 2022 15:11
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/2b53c5abc0ee9b7e38c2876175cb96cb to your computer and use it in GitHub Desktop.
Save RyouMon/2b53c5abc0ee9b7e38c2876175cb96cb to your computer and use it in GitHub Desktop.
Pass value between 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,
),
home: const RouterTestRoute(),
);
}
}
class RouterTestRoute extends StatelessWidget {
const RouterTestRoute({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () async {
var result = await Navigator.push(context,
MaterialPageRoute(builder: (context) {
return const TipRoute(text: "我是提示xxxx");
}));
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