方法
    Navigator.of(context).push 跳转(页面栈存在,可返回)
    Navigator.of(context).pop(); 返回上一级
    Navigator.of(context).pushReplacementNamed() 替换路由
    Navigator.of(context).pushAndRemoveUntil(); 返回到根路由
    1. import 'package:flutter/material.dart';
    2. import 'package:learn_flutter/pages/detail.dart';
    3. class HomeWidget extends StatefulWidget {
    4. @override
    5. _HomeWidgetState createState() => _HomeWidgetState();
    6. }
    7. class _HomeWidgetState extends State<HomeWidget> {
    8. @override
    9. Widget build(BuildContext context) {
    10. return Container(
    11. child: Center(
    12. child: ElevatedButton(
    13. child: Text('跳转到详情页面'),
    14. onPressed: () {
    15. // Navigator push 跳转路由
    16. Navigator.of(context).push(
    17. MaterialPageRoute(
    18. // title 就是传递过去的参数
    19. builder: (context) => DetailWidget(title: '我是路由传值'),
    20. ),
    21. );
    22. },
    23. ),
    24. ),
    25. );
    26. }
    27. }
    28. // detail.dart
    29. import 'package:flutter/material.dart';
    30. class DetailWidget extends StatefulWidget {
    31. // 设置默认参数
    32. DetailWidget({ this.title = '详情页面' });
    33. final String title;
    34. @override
    35. _DetailWidgetState createState() => _DetailWidgetState(title: this.title); // 传递给子类
    36. }
    37. class _DetailWidgetState extends State<DetailWidget> {
    38. // 子类接受参数
    39. _DetailWidgetState({ this.title });
    40. final String title;
    41. @override
    42. Widget build(BuildContext context) {
    43. return Scaffold(
    44. floatingActionButton: FloatingActionButton(
    45. child: Text('返回'),
    46. onPressed: () {
    47. Navigator.of(context).pop();
    48. },
    49. ),
    50. appBar: AppBar(
    51. title: Text(this.title),
    52. ),
    53. body: Center(
    54. child: ElevatedButton(
    55. child: Text('跳转到首页'),
    56. onPressed: () {
    57. // Navigator pop 是返回上一级路由
    58. Navigator.of(context).pop();
    59. },
    60. ),
    61. ),
    62. );
    63. }
    64. }

    路由替换:

    1. import 'package:flutter/material.dart';
    2. class HomePage extends StatefulWidget {
    3. @override
    4. _HomePageState createState() => _HomePageState();
    5. }
    6. class _HomePageState extends State<HomePage> {
    7. @override
    8. Widget build(BuildContext context) {
    9. return Center(
    10. child: ElevatedButton(
    11. child: Text('跳转到详情页面'),
    12. onPressed: () {
    13. // Navigator.pushNamed(context, '/detail', arguments: { "id": 123 });
    14. Navigator.pushReplacementNamed(context, '/detail', arguments: { "id": 123 }); // 替换路由,如果你想用户跳转后无法回退到刚刚那个页面
    15. },
    16. ),
    17. );
    18. }
    19. }

    返回根路由:

    1. import 'package:flutter/material.dart';
    2. import './tabs.dart'; // 引入 根文件
    3. class MePage extends StatelessWidget {
    4. MePage({this.arguments});
    5. final arguments;
    6. @override
    7. Widget build(BuildContext context) {
    8. return Scaffold(
    9. appBar: AppBar(
    10. title: Text('我的'),
    11. ),
    12. body: Center(
    13. child: Column(
    14. children: [
    15. Text("我的${arguments['id']}"),
    16. ElevatedButton(
    17. onPressed: () {
    18. // pushAndRemoveUntil 回到根路由方法
    19. Navigator.pushAndRemoveUntil(
    20. context,
    21. MaterialPageRoute(
    22. // MyBottomNavigationBar 传递参数, tabs 构造函数中接收参数, 注意, 这里必须引入 tabs.dart 文件
    23. builder: (context) => MyBottomNavigationBar(index: 1)),
    24. (route) => route == null);
    25. },
    26. child: Text('回到根路由下'))
    27. ],
    28. ),
    29. ),
    30. );
    31. }
    32. }
    33. // tabs.dart
    34. import 'package:flutter/material.dart';
    35. import 'home.dart';
    36. import 'search.dart';
    37. class MyBottomNavigationBar extends StatefulWidget {
    38. // 定义接收的参数
    39. final int index;
    40. MyBottomNavigationBar({this.index = 0});
    41. @override
    42. _MyBottomNavigationBarState createState() =>
    43. _MyBottomNavigationBarState(this.index);
    44. }
    45. class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {
    46. // 子类接收赋值
    47. _MyBottomNavigationBarState(index) {
    48. this._currentIndex = index;
    49. }
    50. int _currentIndex = 0;
    51. List _pageList = [
    52. HomePage(),
    53. SearchPage(),
    54. ];
    55. @override
    56. Widget build(BuildContext context) {
    57. return Scaffold(
    58. appBar: AppBar(
    59. title: Text('Hello Flutter'),
    60. ),
    61. body: this._pageList[this._currentIndex],
    62. bottomNavigationBar: BottomNavigationBar(
    63. currentIndex: _currentIndex,
    64. onTap: (int index) {
    65. setState(() {
    66. this._currentIndex = index;
    67. });
    68. },
    69. // iconSize: 20,
    70. // fixedColor: Colors.pink,
    71. type: BottomNavigationBarType.fixed, // 配置多个tabItem
    72. items: [
    73. BottomNavigationBarItem(
    74. icon: Icon(Icons.home),
    75. label: '首页',
    76. ),
    77. BottomNavigationBarItem(
    78. icon: Icon(Icons.search),
    79. label: '搜索',
    80. ),
    81. ],
    82. ),
    83. );
    84. }
    85. }