简单导航

  1. import 'package:flutter/material.dart';
  2. void main(){
  3. runApp(MaterialApp(
  4. title: "导航条演示01",
  5. home: new FirstScreen(),
  6. ));
  7. }
  8. class FirstScreen extends StatelessWidget{
  9. @override
  10. Widget build(BuildContext context){
  11. return Scaffold(
  12. appBar: AppBar(title: Text("导航页面")),
  13. body: Center(
  14. child: RaisedButton(
  15. child: Text('查看商品详情页'),
  16. onPressed: (){ //onPressed 点击事件
  17. Navigator.push(context, MaterialPageRoute(builder: (context)=>new SecondScreen()));//Navigator导航组件 MaterialPageRoute路由组件
  18. },),
  19. ),
  20. );
  21. }
  22. }
  23. class SecondScreen extends StatelessWidget{
  24. @override
  25. Widget build(BuildContext context){
  26. return Scaffold(
  27. appBar: AppBar(title: Text('WWt商品详情页'),),
  28. body: Center(
  29. child: RaisedButton(
  30. child: Text("返回"),
  31. onPressed: (){
  32. Navigator.pop(context); //返回
  33. },
  34. ),
  35. ),
  36. );
  37. }
  38. }

导航传递参数

  1. import 'package:flutter/material.dart';
  2. class Product{//商品对象
  3. final String title;//商品标题
  4. final String descriptiom;//商品描述
  5. Product(this.title,this.descriptiom);//构造函数
  6. }
  7. void main(){
  8. runApp(MaterialApp(
  9. title: "导航的数据传递和接收",
  10. home: ProductList(
  11. products:List.generate(20, (i) => Product("商品$i", "这是一个商品详情,编号$i"))//生成20个导航
  12. ),
  13. ));
  14. }
  15. class ProductList extends StatelessWidget {
  16. final List<Product> products;
  17. ProductList({Key key,@required this.products}):super(key: key);//构造函数
  18. @override
  19. Widget build(BuildContext context) {
  20. return Scaffold(//脚手架
  21. appBar: AppBar(title: Text("商品列表"),),
  22. body: ListView.builder(
  23. itemCount: products.length,
  24. itemBuilder: (context,index){
  25. return ListTile(
  26. title: Text(products[index].title),
  27. onTap: (){//点击时的响应事件
  28. Navigator.push(
  29. context,
  30. MaterialPageRoute(
  31. builder:(context)=>ProductDetail(product:products[index])
  32. )
  33. );
  34. }
  35. );
  36. },),
  37. );
  38. }
  39. }
  40. class ProductDetail extends StatelessWidget {
  41. final Product product;
  42. ProductDetail({Key key,@required this.product}):super(key: key);
  43. @override
  44. Widget build(BuildContext context) {
  45. return Scaffold(
  46. appBar: AppBar(title: Text("${product.title}"),),
  47. body: Center(child: Text("${product.descriptiom}"),),
  48. );
  49. }
  50. }

导航返回参数(异步请求和等待)

  1. import 'package:flutter/material.dart';
  2. void main(){
  3. runApp(MaterialApp(
  4. title: "页面跳转返回数据",
  5. home: FirstPage(),
  6. ));
  7. }
  8. class FirstPage extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. return Scaffold(
  12. appBar: AppBar(title: Text("找电话"),),
  13. body: Center(
  14. child: RouteButton(),
  15. ),
  16. );
  17. }
  18. }
  19. class RouteButton extends StatelessWidget {
  20. @override
  21. Widget build(BuildContext context) {
  22. return RaisedButton(
  23. onPressed: (){_navigateToPhone(context);},
  24. child: Text("去找人"),
  25. );
  26. }
  27. _navigateToPhone(BuildContext context) async{
  28. final result = await Navigator.push(
  29. context, MaterialPageRoute(builder: (context)=>zhaoren()));
  30. Scaffold.of(context).showSnackBar(SnackBar(content: Text("$result")));//这条是返回参数的重点
  31. }
  32. }
  33. class zhaoren extends StatelessWidget {
  34. @override
  35. Widget build(BuildContext context) {
  36. return Scaffold(
  37. appBar: AppBar(
  38. title: Text("我是人"),
  39. ),
  40. body: Center(
  41. child: Column(
  42. children: <Widget>[
  43. RaisedButton(
  44. child: Text("机器人"),
  45. onPressed: (){
  46. Navigator.pop(context,"机器人编号:10086");
  47. },
  48. ),
  49. RaisedButton(
  50. child: Text("人工机器人"),
  51. onPressed: (){
  52. Navigator.pop(context,"机器人编号:10087");
  53. },
  54. ),
  55. ],
  56. ),
  57. ),
  58. );
  59. }
  60. }