main.dart

import 'package:flutter/material.dart';

void main(){
return runApp(MyApp());
} class ProductInfo{
final String title;
final String description;
ProductInfo(this.title,this.description);
} class MyApp extends StatelessWidget { @override
Widget build(BuildContext context) {
return MaterialApp(
title: '导航栏传值',
home: Scaffold(
appBar: AppBar(title: Text('导航栏传值'),),
body: ProductList(
products: List.generate(20, (i)=>ProductInfo("title$i", "description")),
),
),
);
}
} class ProductList extends StatelessWidget{
final List<ProductInfo> products;
ProductList({Key key, @required this.products}):super(key:key);
@override
Widget build(BuildContext context) { return ListView.builder(
itemCount: products.length,
itemBuilder: (context,index){
return ListTile(
title: Text(products[index].title),
//列表被点击
onTap: (){
print(products[index].title);
// Navigator.push(
// context,
// MaterialPageRoute(
// builder:(context) => ProductDetail(product: products[index])
// ));
_jumpToDetail(context,index);
}, );
},
);
}
_jumpToDetail(BuildContext context,int index) async{ final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProductDetail(product: products[index])
)
);
//使用showSnackBar显示弹窗
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(result),
duration: Duration(seconds: 1),
)
);
}
} class ProductDetail extends StatelessWidget {
final ProductInfo product;
ProductDetail({Key key, @required this.product}):super(key:key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(product.title)),
body: Center(
child: RaisedButton(
child: Text("点击返回"),
onPressed: (){
Navigator.pop(context,'反向传值result=${product.title}');
},
),
)
);
}
}

重点

1.快速创建模型对象

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

2.快速创建ListView列表

ListView.builder(
itemCount: products.length,
itemBuilder: (context,index){
return ListTile(
title: Text(products[index].title),
//列表被点击
onTap: (){
print(products[index].title);
Navigator.push(
context,
MaterialPageRoute(
builder:(context) => ProductDetail(product: products[index])
));
}, );
},
);

3.导航Navigator push

            Navigator.push(
context,
MaterialPageRoute(
builder:(context) => ProductDetail(product: products[index])
));
},

4.导航pop

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

5.反向传值

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

6.SnackBar显示回调值

    Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(result),
duration: Duration(seconds: 1),
)
);

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. c++的并发操作(多线程)

    C++11标准在标准库中为多线程提供了组件,这意味着使用C++编写与平台无关的多线程程序成为可能,而C++程序的可移植性也得到了有力的保证.另外,并发编程可提高应用的性能,这对对性能锱铢必较的C++程 ...

  2. python的内建函数chr,ord

    python的内建函数chr,ord,unichr chr()函数用一个范围在range(256)内的(就是0-255)整数作参数,返回一个对应的字符.unichr()跟它一样,只不过返回的是Unic ...

  3. MSSQL日期分组排序

    等于今天日期的排上面,大于今天的排中间,小于今天的排下面,带分页.

  4. 使用SpringBoot做Javaweb时,数据交互遇到的问题

    有段时间没做过javaweb了,有点生疏了,js也忘记得差不多,所以今天下午做前后端交互的时候,传到后台的参数总是为空,前端控制台了报一个String parameter “xxx” is not p ...

  5. 程序中的一些限制(基于Linux系统C语言)

    今天突然想起来几个问题,在程序运行起来时,存在一些限制: 1,数组的长度(成员的个数)存在限制!(数组定义的空间大小)2,一个进程里打开的文件数.3,一个文件的名字的长度.4,一个进程里创建线程的个数 ...

  6. 弹出框 popover.js

    弹出框 popover.js 为任意元素添加一小块浮层,就像 iPad 上一样,用于存放非主要信息. 弹出框的标题和内容的长度都是零的话将永远不会被显示出来. 插件依赖 弹出框依赖 工具提示插件 ,因 ...

  7. php面向对象相关技术

    step1 一个经典类的设计和实例化 <?php class mycoach { public $_name=''; public $_age=''; public $_expert=array ...

  8. PyPy初体验

    PyPy初体验 PyPy安装 1.下载 下载地址:http://pypy.org/download.html(温馨提示:需要梯子) 下载PyPy3.6 64bit Ubuntu版本 解压 下载下来的文 ...

  9. 【leetcode】815. Bus Routes

    题目如下: We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. ...

  10. CSS3的2D 转换——旋转,缩放,translate(),skew(),matrix()

    2D转换方法:在平面对元素进行旋转,缩放,移动,拉伸. ㈠浏览器支持 ⑴2D转换效果有以下的浏览器支持:   ⑵在编辑代码的时候要注明用哪种浏览器打开,在前面加上前缀,下面是编辑器的简写形式,以及前缀 ...