路由命名

  1. class MyApp extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MaterialApp(
  5. title: 'Flutter Demo',
  6. theme: ThemeData(
  7. primarySwatch: Colors.blue,
  8. ),
  9. routes: { // 注册路由表
  10. 'new_page': (context) => NewRoute(),
  11. '/': (context) => MyHomePage(title: 'Flutter Demo Home page')
  12. },
  13. );
  14. }
  15. }
  16. //根据name跳转
  17. Navigator.pushNamed(context, 'new_page');
  18. //不使用name
  19. class RouterTestRoute extends StatelessWidget {
  20. @override
  21. Widget build(BuildContext context) {
  22. return Center(
  23. child: ElevatedButton(
  24. onPressed: () async {
  25. var result = await Navigator.push(context,
  26. MaterialPageRoute(builder: (context) {
  27. return TipRoute(text: '我是提示xxx');
  28. }));
  29. print('路由返回值: $result');
  30. },
  31. child: Text('打开提示页'),
  32. ),
  33. );
  34. }
  35. }
  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. return MaterialApp(
  9. title: 'Flutter Demo',
  10. theme: ThemeData(
  11. primarySwatch: Colors.blue,
  12. ),
  13. home: MyHomePage(title: 'Flutter Demo Home Page'),
  14. );
  15. }
  16. }
  17. class MyHomePage extends StatefulWidget {
  18. MyHomePage({Key key, this.title}) : super(key: key);
  19. final String title;
  20. @override
  21. _MyHomePageState createState() => _MyHomePageState();
  22. }
  23. class _MyHomePageState extends State<MyHomePage> {
  24. int _counter = 0;
  25. void _incrementCounter() {
  26. setState(() {
  27. _counter++;
  28. });
  29. }
  30. @override
  31. Widget build(BuildContext context) {
  32. return Scaffold(
  33. appBar: AppBar(
  34. title: Text(widget.title),
  35. ),
  36. body: Center(
  37. child: Column(
  38. mainAxisAlignment: MainAxisAlignment.center,
  39. children: <Widget>[
  40. Text(
  41. 'You have pushed the button this many times:',
  42. ),
  43. Text(
  44. '$_counter',
  45. style: Theme.of(context).textTheme.headline4,
  46. ),
  47. TextButton(
  48. onPressed: () {
  49. Navigator.push(context, MaterialPageRoute(builder: (context) {
  50. return RouterTestRoute();
  51. }));
  52. },
  53. child: Text('open new route'),
  54. )
  55. ],
  56. ),
  57. ),
  58. floatingActionButton: FloatingActionButton(
  59. onPressed: _incrementCounter,
  60. tooltip: 'Increment',
  61. child: Icon(Icons.add),
  62. ),
  63. );
  64. }
  65. }
  66. // 创建一个新路由
  67. class NewRoute extends StatelessWidget {
  68. @override
  69. Widget build(BuildContext context) {
  70. return Scaffold(
  71. appBar: AppBar(
  72. title: Text('New route'),
  73. ),
  74. body: Center(
  75. child: Text("This is new Route"),
  76. ),
  77. );
  78. }
  79. }
  80. class TipRoute extends StatelessWidget {
  81. // 接收一个text参数
  82. TipRoute({Key key, @required this.text}) : super(key: key);
  83. final String text;
  84. @override
  85. Widget build(BuildContext context) {
  86. return Scaffold(
  87. appBar: AppBar(
  88. title: Text('提示'),
  89. ),
  90. body: Padding(
  91. padding: EdgeInsets.all(18),
  92. child: Center(
  93. child: Column(
  94. children: <Widget>[
  95. Text(text),
  96. ElevatedButton(
  97. onPressed: () {
  98. Navigator.pop(context, '我是返回值');
  99. },
  100. child: Text('返回'))
  101. ],
  102. ),
  103. ),
  104. ),
  105. );
  106. }
  107. }
  108. class RouterTestRoute extends StatelessWidget {
  109. @override
  110. Widget build(BuildContext context) {
  111. return Center(
  112. child: ElevatedButton(
  113. onPressed: () async {
  114. var result = await Navigator.push(context,
  115. MaterialPageRoute(builder: (context) {
  116. return TipRoute(text: '我是提示xxx');
  117. }));
  118. print('路由返回值: $result');
  119. },
  120. child: Text('打开提示页'),
  121. ),
  122. );
  123. }
  124. }