{"id":997,"date":"2022-06-11T16:38:00","date_gmt":"2022-06-11T08:38:00","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=997"},"modified":"2022-06-15T16:20:13","modified_gmt":"2022-06-15T08:20:13","slug":"flutter%e5%9f%ba%e7%a1%80%e6%95%99%e7%a8%8bnavigator%e5%b8%83%e5%b1%80","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=997","title":{"rendered":"flutter\u57fa\u7840\u6559\u7a0b(navigator\u5e03\u5c40)"},"content":{"rendered":"\n<p id=\"block-030dfeed-c84e-4fe1-a8a9-e141498a739e\"><br>\u5206\u4eab\u51e0\u4e2a\u5b66\u4e60flutter\u7684\u5b66\u4e60\u7f51\u7ad9\u4e0e\u8d44\u6e90<\/p>\n\n\n\n<p id=\"block-f1339a6b-7e25-45b8-8069-510d5c150a9b\">flutter\u5b9e\u6218\u7b2c\u4e8c\u7248<a href=\"https:\/\/book.flutterchina.club\/chapter6\/gridview.html#_6-6-1-%E9%BB%98%E8%AE%A4%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0\">https:\/\/book.flutterchina.club\/chapter6\/gridview.html#_6-6-1-%E9%BB%98%E8%AE%A4%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0<\/a><\/p>\n\n\n\n<p id=\"block-ce954fb4-619d-4f9d-acfd-5b4395e2016c\">flutter\u4e2d\u6587\u6587\u6863<a href=\"https:\/\/flutter.cn\/docs\/get-started\/flutter-for\/web-devs\">https:\/\/flutter.cn\/docs\/get-started\/flutter-for\/web-devs<\/a><\/p>\n\n\n\n<p><strong>main_navigator.dart<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter\/material.dart';\n\nvoid main(){\n  runApp(MaterialApp(\n    title:'\u5bfc\u822a\u6f14\u793a1',\n    home:new FirstScreen()\n  ));\n}\n\nclass FirstScreen extends StatelessWidget{\n  @override\n  Widget build(BuildContext context){\n    return new Scaffold(\n      appBar: AppBar(title:Text('\u5bfc\u822a\u9875\u9762')),\n      body:Center(\n        child:RaisedButton(\n          child:Text('\u67e5\u770b\u5546\u54c1\u8be6\u60c5\u9875\u9762'),\n          onPressed: (){\n            Navigator.push(context,new  MaterialPageRoute(\n              builder:(context) =>new SecondScreen())\n            );\n          },\n        )\n      )\n    );\n  }\n\/\/ }\n\/\/ Navigator.push\uff1a\u662f\u8df3\u8f6c\u5230\u4e0b\u4e00\u4e2a\u9875\u9762\uff0c\u5b83\u8981\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u4e00\u4e2a\u662f\u4e0a\u4e0b\u6587context\uff0c\u53e6\u4e00\u4e2a\u662f\u8981\u8df3\u8f6c\u7684\u51fd\u6570\u3002\n\n\/\/ Navigator.pop\uff1a\u662f\u8fd4\u56de\u5230\u4e0a\u4e00\u4e2a\u9875\u9762\uff0c\u4f7f\u7528\u65f6\u4f20\u9012\u4e00\u4e2acontext\uff08\u4e0a\u4e0b\u6587\uff09\u53c2\u6570\uff0c\u4f7f\u7528\u65f6\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f60\u5fc5\u987b\u662f\u6709\u4e0a\u7ea7\u9875\u9762\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0a\u7ea7\u9875\u9762\u4f7f\u7528\u4e86Navigator.push\u3002\n\nclass SecondScreen extends StatelessWidget{\n  @override\n  Widget build(BuildContext context){\n    return Scaffold(\n      appBar:AppBar(title:Text('\u5546\u54c1\u8be6\u60c5\u9875')),\n      body:Center(child:RaisedButton(\n        child:RaisedButton(\n          child:Text('\u8fd4\u56de'),\n          onPressed: (){\n            Navigator.pop(context);\n          },\n        )\n      ))\n    );\n\n  }\n}\n\n<\/code><\/pre>\n\n\n\n<p><strong>main_natigator2.dart<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter\/material.dart';\n\n\/\/\u4f20\u9012\u7684\u6570\u636e\u7ed3\u6784,\u4e5f\u53ef\u4ee5\u7406\u89e3\u4e3a\u5bf9\u5546\u54c1\u6570\u636e\u7684\u62bd\u8c61\nclass Product{\n  final String title;  \/\/\u5546\u54c1\u6807\u9898\n  final String description;  \/\/\u5546\u54c1\u63cf\u8ff0\n  Product(this.title,this.description);\n}\n\nvoid main(){\n  runApp(MaterialApp(\n    title:'\u6570\u636e\u4f20\u9012\u6848\u4f8b',\n    home:ProductList(\n      products:List.generate(\n        20, \n        (i)=>Product('\u5546\u54c1 $i','\u8fd9\u662f\u4e00\u4e2a\u5546\u54c1\u8be6\u60c5\uff0c\u7f16\u53f7\u4e3a:$i')\n      ),\n    )\n  ));\n}\n\nclass ProductList extends StatelessWidget{\n  final List&lt;Product> products;\n  ProductList({Key key,@required this.products}):super(key:key);\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      appBar: AppBar(title:Text('\u5546\u54c1\u5217\u8868')),\n      body:ListView.builder(\n        itemCount:products.length,\n        itemBuilder: (context,index){\n          return ListTile(\n            title:Text(products&#91;index].title),\n            onTap:(){\n              Navigator.push(\n                context, \n                MaterialPageRoute(\n                  builder:(context)=>new ProductDetail(product:products&#91;index])\n                )\n              );\n            }\n          );\n        },\n      )\n    );\n  }\n}\n\nclass ProductDetail extends StatelessWidget {\n  final Product product;\n  ProductDetail({Key key ,@required this.product}):super(key:key);\n\n\n  @override\n  Widget build(BuildContext context) {\n    return new Scaffold(\n      appBar: AppBar(\n        title:Text('${product.title}'),\n      ),\n      body:Center(child: Text('${product.description}'),)\n    );\n  }\n}\n\n\n\n<\/code><\/pre>\n\n\n\n<p><strong>main_navigator3.dart<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter\/material.dart';\n\nvoid main(){\n  runApp(MaterialApp(\n    title:'\u9875\u9762\u8df3\u8f6c\u8fd4\u56de\u6570\u636e',\n    home:FirstPage()\n  ));\n}\n\nclass FirstPage extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      appBar:AppBar(title:Text(\"\u627e\u5c0f\u59d0\u59d0\u8981\u7535\u8bdd\")),\n      body:Center(\n        child: RouteButton(),\n      )\n    );\n  }\n}\n\n\/\/\u8df3\u8f6c\u7684Button\nclass RouteButton extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return RaisedButton(\n      onPressed:(){\n          _navigateToXiaoJieJie(context);\n      },\n      child: Text('\u53bb\u627e\u5c0f\u59d0\u59d0'),\n    );\n  }\n\n  _navigateToXiaoJieJie(BuildContext context) async{ \/\/async\u662f\u542f\u7528\u5f02\u6b65\u65b9\u6cd5\n\n    final result = await Navigator.push(\/\/\u7b49\u5f85\n      context, \n      MaterialPageRoute(builder: (context)=> XiaoJieJie())\n      );\n\n      Scaffold.of(context).showSnackBar(SnackBar(content:Text('$result')));\n  }\n}\n\nclass XiaoJieJie extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      appBar:AppBar(\n        title:Text('\u6211\u662f\u5c0f\u59d0\u59d0')\n      ),\n      body:Center(\n        child:Column(\n          children: &lt;Widget>&#91;\n            RaisedButton(\n              child: Text('\u5927\u957f\u817f\u5c0f\u59d0\u59d0'),\n              onPressed: (){\n                Navigator.pop(context,'\u5927\u957f\u817f:1511008888');\n              },\n            ) ,\n            RaisedButton(\n              child: Text('\u5c0f\u86ee\u8170\u5c0f\u59d0\u59d0'),\n              onPressed: (){\n                Navigator.pop(context,'\u5927\u957f\u817f:1511009999');\n              },\n            ) ,\n          ],\n        ) \n      ) ,\n    );\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u4eab\u51e0\u4e2a\u5b66\u4e60flutter\u7684\u5b66\u4e60\u7f51\u7ad9\u4e0e\u8d44\u6e90 flutter\u5b9e\u6218\u7b2c\u4e8c\u7248https:\/\/book.flutterc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,1],"tags":[42],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/997"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=997"}],"version-history":[{"count":1,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/997\/revisions"}],"predecessor-version":[{"id":998,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/997\/revisions\/998"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=997"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}