main.dart

  1. import 'package:flutter/material.dart';
  2. void main(){
  3. return runApp(MyApp());
  4. }
  5. class ProductInfo{
  6. final String title;
  7. final String description;
  8. ProductInfo(this.title,this.description);
  9. }
  10. class MyApp extends StatelessWidget {
  11. @override
  12. Widget build(BuildContext context) {
  13. return MaterialApp(
  14. title: '导航栏传值',
  15. home: Scaffold(
  16. appBar: AppBar(title: Text('导航栏传值'),),
  17. body: ProductList(
  18. products: List.generate(20, (i)=>ProductInfo("title$i", "description")),
  19. ),
  20. ),
  21. );
  22. }
  23. }
  24. class ProductList extends StatelessWidget{
  25. final List<ProductInfo> products;
  26. ProductList({Key key, @required this.products}):super(key:key);
  27. @override
  28. Widget build(BuildContext context) {
  29. return ListView.builder(
  30. itemCount: products.length,
  31. itemBuilder: (context,index){
  32. return ListTile(
  33. title: Text(products[index].title),
  34. //列表被点击
  35. onTap: (){
  36. print(products[index].title);
  37. // Navigator.push(
  38. // context,
  39. // MaterialPageRoute(
  40. // builder:(context) => ProductDetail(product: products[index])
  41. // ));
  42. _jumpToDetail(context,index);
  43. },
  44. );
  45. },
  46. );
  47. }
  48. _jumpToDetail(BuildContext context,int index) async{
  49. final result = await Navigator.push(
  50. context,
  51. MaterialPageRoute(
  52. builder: (context) => ProductDetail(product: products[index])
  53. )
  54. );
  55. //使用showSnackBar显示弹窗
  56. Scaffold.of(context).showSnackBar(
  57. SnackBar(
  58. content: Text(result),
  59. duration: Duration(seconds: 1),
  60. )
  61. );
  62. }
  63. }
  64. class ProductDetail extends StatelessWidget {
  65. final ProductInfo product;
  66. ProductDetail({Key key, @required this.product}):super(key:key);
  67. @override
  68. Widget build(BuildContext context) {
  69. return Scaffold(
  70. appBar: AppBar(title: Text(product.title)),
  71. body: Center(
  72. child: RaisedButton(
  73. child: Text("点击返回"),
  74. onPressed: (){
  75. Navigator.pop(context,'反向传值result=${product.title}');
  76. },
  77. ),
  78. )
  79. );
  80. }
  81. }

重点

1.快速创建模型对象

  1. List.generate(20, (i)=>ProductInfo("title$i", "description"))

2.快速创建ListView列表

  1. ListView.builder(
  2. itemCount: products.length,
  3. itemBuilder: (context,index){
  4. return ListTile(
  5. title: Text(products[index].title),
  6. //列表被点击
  7. onTap: (){
  8. print(products[index].title);
  9. Navigator.push(
  10. context,
  11. MaterialPageRoute(
  12. builder:(context) => ProductDetail(product: products[index])
  13. ));
  14. },
  15. );
  16. },
  17. );

3.导航Navigator push

  1. Navigator.push(
  2. context,
  3. MaterialPageRoute(
  4. builder:(context) => ProductDetail(product: products[index])
  5. ));
  6. },

4.导航pop

  1. //不含参数
  2. Navigator.pop(context);
  3. //包含回调参数
  4. Navigator.pop(context,'反向传值result=${product.title}');

5.反向传值

  1. //通过result接收返回值
  2. final result = await Navigator.push(
  3. context,
  4. MaterialPageRoute(
  5. builder: (context) => ProductDetail(product: products[index])
  6. )
  7. );

6.SnackBar显示回调值

  1. Scaffold.of(context).showSnackBar(
  2. SnackBar(
  3. content: Text(result),
  4. duration: Duration(seconds: 1),
  5. )
  6. );

flutter ListView列表和导航传值以及回调的更多相关文章

  1. Flutter ListView 列表组件

    列表常见的情况: 1.垂直列表 2.垂直图文列表 3.横向列表 4.动态列表 名称 类型 说明 scrollDirection Axis Axis.horizontal 横向列表 Axis.verti ...

  2. Flutter 自定义列表以及本地图片引用

    前言 上篇关于Flutter的文章总结了下标签+导航的项目模式的搭建,具体的有需要的可以去看看Flutter分类的文章,这篇文章我们简单的总结一下关于Flutter本地文件引用以及简单的自定义List ...

  3. Android项目开发全程(四)-- 将网络返回的json字符串轻松转换成listview列表

    前面几篇博文介绍了从项目搭建到获取网络字符串,对一个项目的前期整体工作进行了详细的介绍,本篇接着上篇介绍一下怎么样优雅将网络返回的json字符串轻松转换成listview列表. 先上图,看一下效果. ...

  4. 15 Flutter BottomNavigationBar自定义底部导航条 以及实现页面切换 以及模块化

    效果: /**  * Flutter  BottomNavigationBar 自定义底部导航条.以及实现页面切换:  * BottomNavigationBar是底部导航条,可以让我们定义底部Tab ...

  5. Android一个ListView列表之中插入两种不同的数据

    http://www.cnblogs.com/roucheng/ Android一个ListView列表之中插入两种不同的数据 代码如下: public class ViewHolder{ Butto ...

  6. Android ListView列表控件的简单使用

    ListView 列表是我们经常会使用的控件, 如果想要自定义里面的显示的话是挺麻烦的, 需要新建XML.Class SimpleAdapter这两个文件, 较为麻烦. 如果我们只是想显示两.三行文字 ...

  7. ListView列表拖拽排序

    ListView列表拖拽排序能够參考Android源代码下的Music播放列表,他是能够拖拽的,源代码在[packages/apps/Music下的TouchInterceptor.java下]. 首 ...

  8. iframe 自适应高度、父子页面传值、回调

    总结一下最近用iframe遇到的问题与解决办法: 结构:主页面main.html,里面套用iframe.iframe不能出现滚动条,自适应子页面高度.内容多了滚动条是main.html页面的. 1.  ...

  9. Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件

    UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个用于存放 ...

随机推荐

  1. 25、Nginx常见典型故障

    1.为什么nginx里面有的是浏览器渲染出的页面,有的时候就变成下载文件? 这个一个取决于服务端nginx,一个取决于你浏览器.在Nginx服务端的配置文件目录下,有一个mime.types 文件,内 ...

  2. zookeeper+activemq高可用集群搭建

    一.准备工作: 准备三台机器:192.168.35.111192.168.35.112192.168.35.113 二.搭建zookeeper 三台机器上均要搭建zookeeper服务// 下载zoo ...

  3. Chrome OS 更新新版本可让Linux访问USB连接的Android设备

    谷歌再次为Chrome OS带来了重大版本更新,使版本号达到了75.本次更新的一大亮点就是允许在Chrome OS上运行的Linux能够识别通过USB方式连接的Android设备,能够让用户使用Lin ...

  4. 双向BFS统计

    Hdu1195 两个四位密码 问你最少多少步能到达 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof ...

  5. POJ 1038 Bugs Integrated Inc (复杂的状压DP)

    $ POJ~1038~~\times Bugs~Integrated~Inc: $ (复杂的状压DP) $ solution: $ 很纠结的一道题目,写了大半天,就想练练手,结果这手生的.其实根据之前 ...

  6. sed基础语法

    sed 太强大了 参考博客如下:https://www.cnblogs.com/ctaixw/p/5860221.html sed: Stream Editor文本流编辑,sed是一个“非交互式的”面 ...

  7. 【LuoguP4887】第十四分块(前体)

    题目链接 题意 区间两数异或在二进制下有 \(k\) 个 \(1\) 的对数. Sol 普通莫队的话,如果要实时维护好区间内的答案需要支持区间对一个数求答案. 直接做不是很好做,容易发现其实这也就是一 ...

  8. Centos7——docker入门(笔记)

    docker 入门(笔记) 一.Docker是什么? 官方原话: Docker provides a way to run applications securely isolated in a co ...

  9. 【转】linux中fork()函数详解

    原文链接:http://blog.csdn.net/jason314/article/details/5640969#comments 总结:面宝P268 fork()的意思是进程从这里开始分叉,分成 ...

  10. element-ui 弹出添加拖拽功能

    1.新建dialog.js文件2.在main.js 中引入dialog.js  import ‘./utils/dialog.js’3. 使用:<el-dialog v-dialogDrag&g ...