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. shell判断文件,目录是否存在或者具有权限

    shell判断文件,目录是否存在或者具有权限  #!/bin/sh  myPath="/var/log/httpd/"  myFile="/var /log/httpd/ ...

  2. Tensorflow模型移植Arm之一:C与Python互相调用

    一.C调用Python 1.新建一个Python文件,名称为py_multipy.py: #import numpy as np def multiply(a=1,b=2): print('Funct ...

  3. linux下测试web访问及网络相关的命令

    curl命令 curl是linux系统命令行下用来简单测试web访问的工具. curl -xip:port www.baidu.com    -x可以指定ip和端口,省略写hosts,方便实用 -I  ...

  4. Linux进程管理工具之ps

    1.PS进程管理指令 ps    -aux USER:用户名称 PID:进程号 %CPU:进程占用CPU的百分比 %MEM:进程占用物理内存的百分比 VSZ:进程占用的虚拟内存大小(单位:KB) RS ...

  5. 01--springmvc分布式项目Web项目配置

    springmvc的配置文件,放在resources目录下: 文件名:applicationContext-mvc.xml <?xml version="1.0" encod ...

  6. Practical Lessons from Predicting Clicks on Ads at Facebook (2014)论文阅读

    文章链接: https://quinonero.net/Publications/predicting-clicks-facebook.pdf abstract Facebook日活跃度7.5亿,活跃 ...

  7. redis 加锁与释放锁(分布式锁1)

    使用Redis的 SETNX 命令可以实现分布式锁 SETNX key value 返回值 返回整数,具体为 - 1,当 key 的值被设置 - 0,当 key 的值没被设置 分布式锁使用 impor ...

  8. 【The 13th Chinese Northeast Collegiate Programming Contest E题】

    题目大意:给定一棵 N 个点的树,边有边权,定义"线树"为一个图,其中图的顶点是原树中的边,原树中两条有公共端点的边对应在线图中存在一条边,边权为树中两条边的边权和,求线图的最小生 ...

  9. shell知识点(二)

    Shell 中的数组 Shell 数组用括号来表示,元素用"空格"符号分割开,语法格式如下: 方式2: arr=(value1 value2 value3)   (这种方式带值) ...

  10. 需求-java web 能够实现整个文件夹的上传下载吗?

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...