Created
November 12, 2022 15:11
-
-
Save RyouMon/2b53c5abc0ee9b7e38c2876175cb96cb to your computer and use it in GitHub Desktop.
Pass value between 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, | |
), | |
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