flutter ListView列表和导航传值以及回调
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列表和导航传值以及回调的更多相关文章
- Flutter ListView 列表组件
列表常见的情况: 1.垂直列表 2.垂直图文列表 3.横向列表 4.动态列表 名称 类型 说明 scrollDirection Axis Axis.horizontal 横向列表 Axis.verti ...
- Flutter 自定义列表以及本地图片引用
前言 上篇关于Flutter的文章总结了下标签+导航的项目模式的搭建,具体的有需要的可以去看看Flutter分类的文章,这篇文章我们简单的总结一下关于Flutter本地文件引用以及简单的自定义List ...
- Android项目开发全程(四)-- 将网络返回的json字符串轻松转换成listview列表
前面几篇博文介绍了从项目搭建到获取网络字符串,对一个项目的前期整体工作进行了详细的介绍,本篇接着上篇介绍一下怎么样优雅将网络返回的json字符串轻松转换成listview列表. 先上图,看一下效果. ...
- 15 Flutter BottomNavigationBar自定义底部导航条 以及实现页面切换 以及模块化
效果: /** * Flutter BottomNavigationBar 自定义底部导航条.以及实现页面切换: * BottomNavigationBar是底部导航条,可以让我们定义底部Tab ...
- Android一个ListView列表之中插入两种不同的数据
http://www.cnblogs.com/roucheng/ Android一个ListView列表之中插入两种不同的数据 代码如下: public class ViewHolder{ Butto ...
- Android ListView列表控件的简单使用
ListView 列表是我们经常会使用的控件, 如果想要自定义里面的显示的话是挺麻烦的, 需要新建XML.Class SimpleAdapter这两个文件, 较为麻烦. 如果我们只是想显示两.三行文字 ...
- ListView列表拖拽排序
ListView列表拖拽排序能够參考Android源代码下的Music播放列表,他是能够拖拽的,源代码在[packages/apps/Music下的TouchInterceptor.java下]. 首 ...
- iframe 自适应高度、父子页面传值、回调
总结一下最近用iframe遇到的问题与解决办法: 结构:主页面main.html,里面套用iframe.iframe不能出现滚动条,自适应子页面高度.内容多了滚动条是main.html页面的. 1. ...
- Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件
UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个用于存放 ...
随机推荐
- tornado实现高并发爬虫
from pyquery import PyQuery as pq from tornado import ioloop, gen, httpclient, queues from urllib.pa ...
- 移远模组-BC95-工作模式之间关系
三种连接状态下,均可发送上行数据( CoAP/UDP): IDLE 下发送数据, 模块会进入 CONNECT 状态: PSM 下发送是数据会唤醒模块, 进入 CONNECT,或者当 TAU(TAU 的 ...
- 怎么去掉zencart模板网址后面的zenid=数字这个东西
搜索引擎优化后第一次进入商店网址URL后面会出现zenid=XXXX 如:http://afish.cnblogs.com/zencart-zenid.html?zenid=tbisz675099db ...
- 清北学堂提高组突破营游记day3
讲课人更换成dms. 真的今天快把我们逼疯了.. 今天主攻数据结构, 基本上看完我博客能理解个大概把, 1.LCA 安利之前个人博客链接.之前自己学过QWQ. 2.st表.同上. 3.字符串哈希.同上 ...
- poj2987 Firing[最小割]
题目 求选最少点个数的最大权闭合子图.(板子题) 最小割入门题,什么都不想说,丢个别人题解地址就跑. 附加几点个人理解:与s相通的S点集是闭合子图,剩下的与t相通的T点集是其他的.任意一个割都保证了有 ...
- python -- 数据可视化(二)
python -- 数据可视化 一.Matplotlib 绘图 1.图形对象(图形窗口) mp.figure(窗口名称, figsize=窗口大小, dpi=分辨率, facecolor=颜色) 如果 ...
- linux学习:【第4篇】之nginx
一.知识点回顾 临时:关闭当前正在运行的 /etc/init.d/iptables stop 永久:关闭开机自启动 chkonfig iptables off ll /var/log/secure # ...
- Windows 和 Linux 下生成以当前时间命名的文件
在 Windows.Linux 操作系统,分别利用BAT批处理文件和Shell脚本,生成类似“20110228_082905.txt”以“年月日_时分秒”命名的文件. Windows BAT批处理文件 ...
- JavaScript基础——JavaScript函数(笔记)
avaScript 函数(笔记) JavaScript 是函数式编程语言,在JavaScript脚本中可以随处看到函数,函数构成了JavaScript源代码的主体. 一.定义函数 定义函数的方法有两种 ...
- Wpf自动滚动效果
一.思路 1.使用ScrollView的Scroll.ScrollToVerticalOffset(offset)方法进行滚动 2.ScrollView中放置2个ListView,第一个滚动出边界后, ...