用setState改变状态

class CpwsList extends StatefulWidget {
_CpwsListState createState() => _CpwsListState();
} class _CpwsListState extends State<CpwsList> {
int page = ;
List<Map> hotList = [];
GlobalKey<RefreshFooterState> _footerKey = new GlobalKey<RefreshFooterState>(); //定义key
var scrollController = new ScrollController(); //声明控制器变量 @override
void initState() {
super.initState();
_getList();
} void _getList(){
var formData = {'page':page, 'isDebug':'aa'};
request('get', 'cpwsList', formData: formData).then((val){
var data = val.toString();
//print(data);
List<Map>newGoodsList = (val['data']['data'] as List).cast();
print(newGoodsList);
setState(() {
hotList.addAll(newGoodsList);
});
});
} @override
Widget build(BuildContext context) { return Container(
height: ScreenUtil().setHeight(),
margin: EdgeInsets.only(top:),
child: ListView.builder(
itemCount: hotList.length,
itemBuilder: (context, index) {
final Map<String, dynamic> item = (hotList)[index];
return Container(
margin: EdgeInsets.fromLTRB(, , , ),
child: Column(
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
child: Text('${item["title"]}',style: TextStyle(fontSize: ScreenUtil().setSp()),),
),
Container(
padding: EdgeInsets.only(top:),
alignment: Alignment.centerLeft,
child: Text('${item["caseno"]}'),
),
Container(
padding: EdgeInsets.only(top:),
alignment: Alignment.centerLeft,
child: Row(
children: <Widget>[
Expanded(
child:Text('${item["court"]}',style: TextStyle(fontSize: ScreenUtil().setSp(),color:Colors.black38)),
),
Text('${item["judgedate"]}',style: TextStyle(fontSize: ScreenUtil().setSp(),color:Colors.black38)),
],
),
),
], ),
);
}
)
); }
}

再添加上拉加载和下拉刷新,代码如下:

class CpwsList extends StatefulWidget {
_CpwsListState createState() => _CpwsListState();
} class _CpwsListState extends State<CpwsList> {
int page = ;
List<Map> hotList = [];
GlobalKey<RefreshHeaderState> _headerKey = new GlobalKey<RefreshHeaderState>(); //定义key
GlobalKey<RefreshFooterState> _footerKey = new GlobalKey<RefreshFooterState>(); //定义key
var scrollController = new ScrollController(); //声明控制器变量 @override
void initState() {
super.initState();
_getList();
} void _getList(){
var formData = {'page':page, 'isDebug':'aa'};
request('get', 'cpwsList', formData: formData).then((val){
var data = val.toString();
//print(data);
List<Map>newGoodsList = (val['data']['data'] as List).cast();
print(newGoodsList);
setState(() {
hotList.addAll(newGoodsList);
});
});
} @override
Widget build(BuildContext context) { return Container(
height: ScreenUtil().setHeight(),
margin: EdgeInsets.only(top:),
child: EasyRefresh(
refreshHeader: ClassicsHeader(//自定义下拉刷新效果
key:_headerKey,
bgColor:Colors.white,
textColor: Colors.blueGrey,
moreInfoColor: Colors.blueGrey,
refreshingText: '加载中', //加载时显示的文字
refreshedText: '刷新成功',
),
refreshFooter:ClassicsFooter( //自定义上拉加载效果
key:_footerKey,
bgColor:Colors.white,
textColor: Colors.blueGrey,
moreInfoColor: Colors.blueGrey,
showMore: true,
noMoreText: '',
moreInfo: '加载中', //加载时显示的文字
loadReadyText: '上拉加载...', //准备时显示的文字
),
child: ListView.builder(
itemCount: hotList.length,
itemBuilder: (context, index) {
final Map<String, dynamic> item = (hotList)[index];
return Container(
margin: EdgeInsets.fromLTRB(, , , ),
child: Column(
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
child: Text('${item["title"]}',style: TextStyle(fontSize: ScreenUtil().setSp()),),
),
Container(
padding: EdgeInsets.only(top:),
alignment: Alignment.centerLeft,
child: Text('${item["caseno"]}'),
),
Container(
padding: EdgeInsets.only(top:),
alignment: Alignment.centerLeft,
child: Row(
children: <Widget>[
Expanded(
child:Text('${item["court"]}',style: TextStyle(fontSize: ScreenUtil().setSp(),color:Colors.black38)),
),
Text('${item["judgedate"]}',style: TextStyle(fontSize: ScreenUtil().setSp(),color:Colors.black38)),
],
),
),
], ),
);
}
),
onRefresh: () async {
await new Future.delayed(const Duration(seconds: ), () {
setState(() {
hotList.clear();
page = ;
_getList();
});
});
},
loadMore: () async {
await new Future.delayed(const Duration(seconds: ), () {
setState(() {
page++;
_getList();
});
});
}, )
); }
}

异步请求再渲染 不用setState改变状态

class CpwsList extends StatefulWidget {
_CpwsListState createState() => _CpwsListState();
} class _CpwsListState extends State<CpwsList> {
@override
Widget build(BuildContext context) {
var formData = {'isDebug':'aa'};
return Container(
height: ScreenUtil().setHeight(),
margin: EdgeInsets.only(top:),
child: FutureBuilder( //异步请求再渲染 不用setState改变状态
future: request('get','cpwsList',formData:formData), //可选参数formData:formData=属性名:属性值
builder: (context, snapshot){ //snapshot就是_calculation在时间轴上执行过程的状态快照
if(snapshot.hasData){ //判断有没有值
//print(snapshot.data); //数据处理
var data = snapshot.data;
List<Map> cpwsData = (data['data']['data'] as List).cast();
//print(chinaData); return ListView.builder(
itemCount: cpwsData.length,
itemBuilder: (context, index) {
final Map<String, dynamic> item = (cpwsData)[index];
return Container(
margin: EdgeInsets.fromLTRB(, , , ),
child: Column(
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
child: Text('${item["title"]}',style: TextStyle(fontSize: ScreenUtil().setSp()),),
),
Container(
padding: EdgeInsets.only(top:),
alignment: Alignment.centerLeft,
child: Text('${item["caseno"]}'),
),
Container(
padding: EdgeInsets.only(top:),
alignment: Alignment.centerLeft,
child: Row(
children: <Widget>[
Expanded(
child:Text('${item["court"]}',style: TextStyle(fontSize: ScreenUtil().setSp(),color:Colors.black38)),
),
Text('${item["judgedate"]}',style: TextStyle(fontSize: ScreenUtil().setSp(),color:Colors.black38)),
],
),
),
], ),
);
}
); }else{
return Center(
child: Text('加载中...'),
);
}
}
),
);
}
}

EasyRefresh + ScrollController 实现下拉刷新和上拉加载

FutureBuilder 需要结合 Future 使用,先定义一个 Future,异步网络请求。

///先定义一个 Future
Future getDataFuture;
... @override
void initState() {
super.initState();
getDataFuture = _getList();
} //获取数据
Future _getList() async{
var formData = {'page':page, 'isDebug':'aa'};
var response = await request('get', 'cpwsList', formData: formData);
//print(response);
List<Map>newGoodsList = (response['data']['data'] as List).cast();
//print(newGoodsList);
if(newGoodsList != null){
hotList.addAll(newGoodsList);
}
return hotList;
}

通过 ScrollController 可以判断滚动列表是否滚动到底部,如果是,就调用上滑加载的功能获取数据即可。

var scrollController = new ScrollController(); //声明控制器变量

添加上拉加载及下拉刷新代码:

class CpwsList extends StatefulWidget {
_CpwsListState createState() => _CpwsListState();
} class _CpwsListState extends State<CpwsList> {
Future getDataFuture;
int page = ;
List<Map> hotList = [];
GlobalKey<RefreshHeaderState> _headerKey = new GlobalKey<RefreshHeaderState>(); //定义key
GlobalKey<RefreshFooterState> _footerKey = new GlobalKey<RefreshFooterState>(); //定义key
var scrollController = new ScrollController(); //声明控制器变量 @override
void initState() {
super.initState();
getDataFuture = _getList();
} Future _getList() async{
var formData = {'page':page, 'isDebug':'aa'};
var response = await request('get', 'cpwsList', formData: formData);
//print(response);
List<Map>newGoodsList = (response['data']['data'] as List).cast();
//print(newGoodsList);
if(newGoodsList != null){
hotList.addAll(newGoodsList);
}
return hotList;
} @override
Widget build(BuildContext context) { return Container(
height: ScreenUtil().setHeight(),
margin: EdgeInsets.only(top:),
child: FutureBuilder( //异步请求再渲染 不用setState改变状态
future: getDataFuture,
builder: (context,AsyncSnapshot async){
//print(async.hasData);
if(async.hasData){ //判断有没有值 return EasyRefresh(
refreshHeader: ClassicsHeader(//自定义下拉刷新效果
key:_headerKey,
bgColor:Colors.white,
textColor: Colors.blueGrey,
moreInfoColor: Colors.blueGrey,
refreshingText: '加载中', //加载时显示的文字
refreshedText: '刷新成功',
),
refreshFooter: ClassicsFooter( //自定义refreshFooter
key: _footerKey,
bgColor: Colors.white,
textColor: Colors.blueGrey,
moreInfoColor: Colors.blueGrey,//加载时显示的文字颜色
showMore: true,
noMoreText: '',
moreInfo: '加载中', //加载时显示的文字
loadReadyText: '上拉加载...', //准备文字
),
child: ListView.builder(
controller: scrollController,
itemCount: hotList.length,
itemBuilder: (context, index) {
final Map<String, dynamic> item = (hotList)[index];
return Container(
margin: EdgeInsets.fromLTRB(, , , ),
child: Column(
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
child: Text('${item["title"]}',style: TextStyle(fontSize: ScreenUtil().setSp()),),
),
Container(
padding: EdgeInsets.only(top:),
alignment: Alignment.centerLeft,
child: Text('${item["caseno"]}'),
),
Container(
padding: EdgeInsets.only(top:),
alignment: Alignment.centerLeft,
child: Row(
children: <Widget>[
Expanded(
child:Text('${item["court"]}',style: TextStyle(fontSize: ScreenUtil().setSp(),color:Colors.black38)),
),
Text('${item["judgedate"]}',style: TextStyle(fontSize: ScreenUtil().setSp(),color:Colors.black38)),
],
),
),
], ),
);
}
),
onRefresh: () async {
await new Future.delayed(const Duration(seconds: ), () {
setState(() {
hotList.clear();
page = ;
getDataFuture = _getList();
});
});
},
loadMore: () async {
await new Future.delayed(const Duration(seconds: ), () {
setState(() {
page++;
getDataFuture = _getList();
});
});
},
); }else{
return Center(
child: Text('加载中...'),
);
}
}
),
);
}
}

注意:getDataFuture = _getList();

最后把子项剥离出来,单独写一个Widget,方便InkWell。

Widget _cpwsitem(Map item, index) {
return InkWell(
onTap: () {},
child: Column(
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
child: Text(
'${item["title"]}',
style: TextStyle(fontSize: ScreenUtil().setSp()),
),
),
Container(
padding: EdgeInsets.only(top: ),
alignment: Alignment.centerLeft,
child: Text('${item["caseno"]}'),
),
Container(
padding: EdgeInsets.only(top: ),
alignment: Alignment.centerLeft,
child: Row(
children: <Widget>[
Expanded(
child: Text(
'${item["court"]}',
style: TextStyle(fontSize: ScreenUtil().setSp(), color: Colors.black38)
),
),
Text(
'${item["judgedate"]}',
style: TextStyle(fontSize: ScreenUtil().setSp(), color: Colors.black38)
),
],
),
),
],
),
);
}

然后把上面Container的child改为:

child: _cpwsitem(item, index)

Flutter 的setState与FutureBuilder及EasyRefresh示例的更多相关文章

  1. Flutter easyrefresh示例 上拉加载+下拉刷新

    官方示例,简单改了下,实现功能为主. 代码如下: import 'dart:async'; import 'package:flutter/material.dart'; import 'packag ...

  2. Flutter FutureBuilder异步请求列表示例

    Flutter的FutureBuilder列表示例 import 'package:flutter/material.dart'; import '../service/service_method. ...

  3. Flutter 异步Future与FutureBuilder实用技巧

    什么是Future? Future表示在接下来的某个时间的值或错误,借助Future我们可以在Flutter实现异步操作.它类似于ES6中的Promise,提供then和catchError的链式调用 ...

  4. 聊一聊Flutter的setState()

    Flutter 里面包含两种widget 一种可变的,一种不可变的: 在可变的widget中可以使用 setstate(){} 函数. 官方也给出了例子: _onClick(){ setState() ...

  5. 【Flutter】功能型组件之异步UI更新

    前言 很多时候会依赖一些异步数据来动态更新UI,比如在打开一个页面时我们需要先从互联网上获取数据,在获取数据的过程中我们显示一个加载框,等获取到数据时我们再渲染页面:又比如想展示Stream(比如文件 ...

  6. Flutter Web 支持现已进入稳定版

    作者 / Mariam Hasnany, Product Manager, Flutter 我们对 Flutter 的愿景是成为一个可移植的 UI 框架,在全平台上构建精美的应用体验.做为 Flutt ...

  7. Flutter资源

    目录 文章 一开始 HOWTO文档 网站/博客 高级 视频 组件 演示 UI 材料设计 图片 地图 图表 导航 验证 文字和富文本 分析.流量统计 自动构建 风格样式 媒体 音频 视频 语音 存储 获 ...

  8. Flutter 多引擎支持 PlatformView 以及线程合并解决方案

    作者:字节移动技术-李皓骅 摘要 本文介绍了 Flutter 多引擎下,使用 PlatformView 场景时不能绕开的一个线程合并问题,以及它最终的解决方案.最终 Pull Request 已经 m ...

  9. textfield reload issue and other things reload problem.===================================

    https://github.com/flutter/flutter/issues/18828 https://blog.csdn.net/u011272795/article/details/830 ...

随机推荐

  1. Netty源码分析 (七)----- read过程 源码分析

    在上一篇文章中,我们分析了processSelectedKey这个方法中的accept过程,本文将分析一下work线程中的read过程. private static void processSele ...

  2. GIT常见问题及其解决方案

    问题: remote: Permission to beijing01/learn_github.git denied to liuhongyang02. fatal: unable to acces ...

  3. Dijkstra算法详细(单源最短路径算法)

    介绍 对于dijkstra算法,很多人可能感觉熟悉而又陌生,可能大部分人比较了解bfs和dfs,而对dijkstra和floyd算法可能知道大概是图论中的某个算法,但是可能不清楚其中的作用和原理,又或 ...

  4. SeekBar拖动条

    seekbar拖动条,通过滑块的位置值的改变,来对某些数据进行调节,比如音量调节; 改变滑块的外观可以通过android:thumb属性完成 实例:拖动滑块改变图片透明度 <LinearLayo ...

  5. Ubuntu16.4安装Vivado Design Suite sdx2019.1

    1:下载安装包.到Xilinx官网下载下面为网址: https://www.xilinx.com/support/download.html 2:进入安装包路径,打开终端 Ctrl+alt +t sh ...

  6. charles Glist发布设置

    本文参考:charles Glist发布设置 在这里可以设置Github账户, 发布list的大小限制:等等: 在这里 Auh 就是设置Github账户, 设置登陆你的Github后,才能针对该用户进 ...

  7. jmeter linux压测报错:Error in NonGUIDriver java.lang.IllegalArgumentException: Problem loading XML from:'/home/server/ptest/disk_out.jmx'.

    1.linux环境jmeter与win环境编写脚本的jmeter版本不一致,版本改为一致 2.脚本中存在中文,去除中文 3.脚本中存在类似于jp@gc - Active Threads Over Ti ...

  8. 003:CSS三大重点之一:盒子模型

    目录 1:盒子模型 2:边框: 2.1:合写 2.2:适用于:table系元素.边框合并 3:内边距 4:外边距: 4.1:盒子居中三大条件 4.2:外边距合并.外边距塌陷(父子嵌套)解决方法三种 前 ...

  9. 19 (OC)* RunLoop

    面试题 1:讲讲RunLoop,项目中有用到吗? 2:RunLoop内部实现逻辑? 3:Runloop和线程的关系? 4:timer 与 Runloop 的关系? 5:程序中添加每3秒响应一次的NST ...

  10. [Pandas] 06 - Data Preprocessing and Cleaning

    From: DBWangGroup 基于该系列代码的实践与补充思考. 补充:特征工程 结合:[Scikit-learn] 4.3. Preprocessing data /* implement */