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来说,则是一个用于存放 ...
随机推荐
- 25、Nginx常见典型故障
1.为什么nginx里面有的是浏览器渲染出的页面,有的时候就变成下载文件? 这个一个取决于服务端nginx,一个取决于你浏览器.在Nginx服务端的配置文件目录下,有一个mime.types 文件,内 ...
- zookeeper+activemq高可用集群搭建
一.准备工作: 准备三台机器:192.168.35.111192.168.35.112192.168.35.113 二.搭建zookeeper 三台机器上均要搭建zookeeper服务// 下载zoo ...
- Chrome OS 更新新版本可让Linux访问USB连接的Android设备
谷歌再次为Chrome OS带来了重大版本更新,使版本号达到了75.本次更新的一大亮点就是允许在Chrome OS上运行的Linux能够识别通过USB方式连接的Android设备,能够让用户使用Lin ...
- 双向BFS统计
Hdu1195 两个四位密码 问你最少多少步能到达 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof ...
- POJ 1038 Bugs Integrated Inc (复杂的状压DP)
$ POJ~1038~~\times Bugs~Integrated~Inc: $ (复杂的状压DP) $ solution: $ 很纠结的一道题目,写了大半天,就想练练手,结果这手生的.其实根据之前 ...
- sed基础语法
sed 太强大了 参考博客如下:https://www.cnblogs.com/ctaixw/p/5860221.html sed: Stream Editor文本流编辑,sed是一个“非交互式的”面 ...
- 【LuoguP4887】第十四分块(前体)
题目链接 题意 区间两数异或在二进制下有 \(k\) 个 \(1\) 的对数. Sol 普通莫队的话,如果要实时维护好区间内的答案需要支持区间对一个数求答案. 直接做不是很好做,容易发现其实这也就是一 ...
- Centos7——docker入门(笔记)
docker 入门(笔记) 一.Docker是什么? 官方原话: Docker provides a way to run applications securely isolated in a co ...
- 【转】linux中fork()函数详解
原文链接:http://blog.csdn.net/jason314/article/details/5640969#comments 总结:面宝P268 fork()的意思是进程从这里开始分叉,分成 ...
- element-ui 弹出添加拖拽功能
1.新建dialog.js文件2.在main.js 中引入dialog.js import ‘./utils/dialog.js’3. 使用:<el-dialog v-dialogDrag&g ...