ProductList.dart

import 'package:flutter/material.dart';
import '../services/ScreenAdaper.dart';
import '../config/Config.dart';
import 'package:dio/dio.dart';
import '../model/ProductModel.dart';
import '../widget/LoadingWidget.dart'; class ProductListPage extends StatefulWidget {
Map arguments;
ProductListPage({Key key, this.arguments}) : super(key: key); _ProductListPageState createState() => _ProductListPageState();
} class _ProductListPageState extends State<ProductListPage> {
//通过事件打开侧边栏,需要全局声明一下:
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
//配置下拉加载更多:
ScrollController _scrollController = ScrollController();
//分页:
int _page = ;
//每一页有多少条数据:
int _pageSize = ;
//分页:
List _productList = [];
//排序:
String _sort = "";
//解决重复请求的问题:
bool flag = true; //是否有数据:
bool _hasMore = true; //初始化的时候获取的生命周期函数:
@override
void initState() {
super.initState();
_getProductListData();
//监听滚动条滚动事件:
_scrollController.addListener(() {
// _scrollController.position.pixels //获取滚动条滚动高度
// _scrollController.position.maxScrollExtent //获取页面滚动高度:
if (_scrollController.position.pixels >
_scrollController.position.maxScrollExtent - ) {
if (this.flag&&this._hasMore) {
_getProductListData();
}
}
});
} //获取商品列表的数据:
_getProductListData() async {
setState(() {
this.flag = false;
});
var api =
'${Config.domain}api/plist?cid=${widget.arguments["cid"]}&page=${_page}&sort=${this._sort}&pageSize=${_pageSize}';
var result = await Dio().get(api);
var productList = ProductModel.fromJson(result.data);
print(productList.result); if (productList.result.length < this._pageSize) {
setState(() {
this._productList.addAll(productList.result);
this._hasMore = false;
this.flag = true;
// this._productList = productList.result;
});
} else {
setState(() {
this._productList.addAll(productList.result);
this._page++;
this.flag = true;
// this._productList = productList.result;
});
}
}
//显示加载中的圈圈:
Widget _showMore(index){
if(this._hasMore){
return (index == this._productList.length - )? LoadingWidget() : Text('');
}else{
return (index == this._productList.length - )? Text("---暂无其他数据了--") : Text('');
}
} //商品列表:
Widget _productListWidget() {
if (this._productList.length > ) {
return Container(
padding: EdgeInsets.all(),
margin: EdgeInsets.only(top: ScreenAdaper.height()),
child: ListView.builder(
controller: _scrollController,
itemBuilder: (context, index) {
//处理图片:
String pic = this._productList[index].pic;
pic = Config.domain + pic.replaceAll('\\', '/');
//获得每一个元素:
return Column(
children: <Widget>[
Row(
children: <Widget>[
Container(
width: ScreenAdaper.width(),
height: ScreenAdaper.height(),
child: Image.network("${pic}", fit: BoxFit.cover),
),
Expanded(
flex: ,
child: Container(
height: ScreenAdaper.height(),
margin: EdgeInsets.only(left: ),
// color: Colors.red,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"${this._productList[index].title}",
maxLines: ,
overflow: TextOverflow.ellipsis,
),
Row(
children: <Widget>[
Container(
height: ScreenAdaper.height(),
margin: EdgeInsets.only(right: ),
padding: EdgeInsets.fromLTRB(, , , ),
//注意:如果Container里面加上decoration属性,这个时候color属性必须放到BoxDecoration
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(),
// color:Color.fromRGBO(230, 230, 230, 0.9)
),
child: Text('4G'),
),
Container(
height: ScreenAdaper.height(),
margin: EdgeInsets.only(right: ),
padding: EdgeInsets.fromLTRB(, , , ),
//注意:如果Container里面加上decoration属性,这个时候color属性必须放到BoxDecoration
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(),
// color:Color.fromRGBO(230, 230, 230, 0.3)
),
child: Text('16G'),
)
],
),
Text("¥ ${this._productList[index].price}",
style:
TextStyle(color: Colors.red, fontSize: ))
],
),
),
)
],
),
Divider(
height: ,
),
this._showMore(index)
],
);
},
itemCount: this._productList.length,
),
);
} else {
return LoadingWidget();
}
} //筛选导航:
Widget _subHeaderWidget() {
return Positioned(
top: ,
height: ScreenAdaper.height(),
width: ScreenAdaper.width(),
child: Container(
height: ScreenAdaper.height(),
width: ScreenAdaper.width(),
// color: Colors.red,
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: , color: Color.fromRGBO(, , , 0.9)))),
child: Row(
children: <Widget>[
Expanded(
flex: ,
child: InkWell(
child: Padding(
padding: EdgeInsets.fromLTRB(
, ScreenAdaper.height(), , ScreenAdaper.height()),
child: Text(
'综合',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.red),
),
),
onTap: () {},
),
),
Expanded(
flex: ,
child: InkWell(
child: Padding(
padding: EdgeInsets.fromLTRB(
, ScreenAdaper.height(), , ScreenAdaper.height()),
child: Text('销量', textAlign: TextAlign.center),
),
onTap: () {},
),
),
Expanded(
flex: ,
child: InkWell(
child: Padding(
padding: EdgeInsets.fromLTRB(
, ScreenAdaper.height(), , ScreenAdaper.height()),
child: Text('价格', textAlign: TextAlign.center),
),
onTap: () {},
),
),
Expanded(
flex: ,
child: InkWell(
child: Padding(
padding: EdgeInsets.fromLTRB(
, ScreenAdaper.height(), , ScreenAdaper.height()),
child: Text('筛选', textAlign: TextAlign.center),
),
onTap: () {
_scaffoldKey.currentState.openEndDrawer();
},
),
)
],
),
),
);
} @override
Widget build(BuildContext context) {
ScreenAdaper.init(context);
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text('商品列表'),
actions: <Widget>[Text('')],
),
endDrawer: Drawer(
child: Container(
child: Text('实现筛选功能'),
),
),
// body: Text("${widget.arguments}"),
body: Stack(
children: <Widget>[_productListWidget(), _subHeaderWidget()],
),
);
}
}

Category.dart

import 'package:flutter/material.dart';
import '../../services/ScreenAdaper.dart';
import '../../config/Config.dart';
import 'package:dio/dio.dart';
import '../../model/CateModel.dart'; class CategoryPage extends StatefulWidget {
CategoryPage({Key key}) : super(key: key); _CategoryPageState createState() => _CategoryPageState();
} class _CategoryPageState extends State<CategoryPage>
with AutomaticKeepAliveClientMixin {
int _selectIndex = ;
List _leftCateList = [];
List _rightCateList = [];
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
@override
void initState() {
super.initState();
_getLeftCateData();
} //左侧数据:
_getLeftCateData() async {
var api = '${Config.domain}api/pcate';
var result = await Dio().get(api);
var leftCateList = CateModel.fromJson(result.data);
setState(() {
this._leftCateList = leftCateList.result;
});
_getRightCateData(leftCateList.result[].sId);
} //右侧数据:
_getRightCateData(pid) async {
var api = '${Config.domain}api/pcate?pid=${pid}';
var result = await Dio().get(api);
var rightCateList = CateModel.fromJson(result.data);
print();
setState(() {
this._rightCateList = rightCateList.result;
});
} //左侧组件
Widget _leftCateWidget(leftWidth) {
if (this._leftCateList.length > ) {
return Container(
width: leftWidth,
height: double.infinity,
// color: Colors.red,
child: ListView.builder(
itemCount: this._leftCateList.length,
itemBuilder: (context, index) {
return Column(
children: <Widget>[
InkWell(
onTap: () {
setState(() {
// setState(() {
_selectIndex = index;
this._getRightCateData(this._leftCateList[index].sId);
});
print(_selectIndex);
},
child: Container(
width: double.infinity,
height: ScreenAdaper.height(),
padding: EdgeInsets.only(top: ScreenAdaper.height()),
child: Text("${this._leftCateList[index].title}",
textAlign: TextAlign.center),
color: _selectIndex == index
? Color.fromRGBO(, , , 0.9)
: Colors.white,
),
),
Divider(height: ),
],
);
},
),
);
} else {
return Container(
width: leftWidth,
height: double.infinity,
);
}
} //右侧组件:
Widget _rightCateWidget(rightItemWidth, rightItemHeigth) {
if (this._rightCateList.length > ) {
return Expanded(
flex: ,
child: Container(
padding: EdgeInsets.all(),
height: double.infinity,
color: Color.fromRGBO(, , , 0.9),
// color: Colors.blue,
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: ,
childAspectRatio: rightItemWidth / rightItemHeigth,
crossAxisSpacing: ,
mainAxisSpacing: ),
itemCount: this._rightCateList.length,
itemBuilder: (context, index) {
//处理图片:
String pic = this._rightCateList[index].pic;
pic = Config.domain + pic.replaceAll('\\', '/');
return InkWell(
child: Container(
// padding: EdgeInsets.all(ScreenAdaper.width(20)),
child: Column(
children: <Widget>[
AspectRatio(
aspectRatio: / ,
child: Image.network("${pic}", fit: BoxFit.cover),
),
Container(
height: ScreenAdaper.height(),
child: Text("${this._rightCateList[index].title}"),
)
],
),
),
onTap: (){
Navigator.pushNamed(context,'/productList',arguments: {
"cid":this._rightCateList[index].sId
});
}, );
},
),
),
);
} else {
return Expanded(
flex: ,
child: Container(
padding: EdgeInsets.all(),
height: double.infinity,
color: Color.fromRGBO(, , , 0.9),
child: Text('加载中...'),
));
}
} Widget build(BuildContext context) {
ScreenAdaper.init(context); //计算右侧GridView宽高比:
var leftWidth = ScreenAdaper.getScreenWidth() / ;
//右侧宽高=总宽度-左侧宽度-Gridview外层元素左右的Padding值-GridView中间的间距
var rightItemWidth =
(ScreenAdaper.getScreenWidth() - leftWidth - - ) / ;
rightItemWidth = ScreenAdaper.width(rightItemWidth);
var rightItemHeigth = rightItemWidth + ScreenAdaper.height(); return Row(
children: <Widget>[
_leftCateWidget(leftWidth),
_rightCateWidget(rightItemWidth, rightItemHeigth)
],
);
}
}

12 Flutter仿京东商城项目 商品列表页面请求数据、封装Loading Widget、上拉分页加载更多的更多相关文章

  1. 11 Flutter仿京东商城项目 商品列表页面二级筛选导航布局

    ProductList.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; imp ...

  2. 13 Flutter仿京东商城项目 商品列表筛选以及上拉分页加载更多

    ProductList.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; imp ...

  3. 18 Flutter仿京东商城项目 商品详情顶部tab切换 顶部下拉菜单 底部浮动导航

    ProductContent.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; ...

  4. 19 Flutter仿京东商城项目 商品详情 底部浮动导航布局 商品页面布局

    效果: widget/JdButton.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.da ...

  5. 38 Flutter仿京东商城项目 渲染结算页面商品数据

    加群452892873 下载对应38课文件,运行方法,建好项目,直接替换lib目录 CartServices.dart import 'dart:convert'; import 'Storage.d ...

  6. 20 Flutter仿京东商城项目 商品详情 底部弹出筛选属性 以及筛选属性页面布局

    ProductContentFirst.dart import 'package:flutter/material.dart'; import '../../widget/JdButton.dart' ...

  7. 21 Flutter仿京东商城项目 商品详情 请求接口渲染数据 商品属性数据渲染

    加群452892873 下载对应21可文件,运行方法,建好项目,直接替换lib目录,在往pubspec.yaml添加上一下扩展.   cupertino_icons: ^0.1.2   flutter ...

  8. 01-02 Flutter仿京东商城项目 功能分析、底部导航Tab切换以及路由配置、架构搭建:(Flutter仿京东商城项目 首页布局以及不同终端屏幕适配方案)

    Flutter和Dart交流学习群:交流群:452892873 01Flutter仿京东商城项目 功能分析.底部导航Tab切换以及路由配置.架构搭建 02Flutter仿京东商城项目 首页布局以及不同 ...

  9. 16 Flutter仿京东商城项目 跳转到搜索页面实现搜索功能 以及搜索筛选

    ProductList.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; imp ...

随机推荐

  1. MacOS文本编辑无法打不开GB18030

      不要直接双击打开   而是 打开sublime text或者其他文本编辑后,从软件里面的open选型打开 

  2. docker 部署 elasticsearch + elasticsearch-head + elasticsearch-head跨域问题 + IK分词器

    0.  docker pull 拉取elasticsearch + elasticsearch-head 镜像 1.  启动elasticsearch Docker镜像 docker run -di ...

  3. BZOJ 3716 [PA2014]Muzeum 贪心SET最大闭合子图

    看上去像是一个最大权闭合子图裸题但是数据太大 我们可以先把守卫的视野转换到第二象限(每个守卫可以看到横坐标比他小 纵坐标比他大的宝物) 然后按X从小到大 再按Y从大到小排 这样我们就可以按SORT序遍 ...

  4. hibernate的详解

    一,环境的搭建 1)创建maven项目 2)导入依赖的jar包.pom.xml和创建实体类User <?xml version="1.0" encoding="UT ...

  5. usb相关

    https://github.com/daynix/UsbDk/tree/master/UsbDk 更应该关注下libusb

  6. vue 之 render 函数不能渲染非全局自定义函数-方案

    import customCom from 'xxx.vue' render: (h) => { return h(customCom) }

  7. rabbitmq 持久化 事务 发送确认模式

    部分内容来自:http://blog.csdn.net/hzw19920329/article/details/54315940 http://blog.csdn.net/hzw19920329/ar ...

  8. Postman----登录接口返回的reponse中token值传递给其他接口的一个简单接口测试示例

    注: 在进行接口测试时,我们都需要使用登录,并且其他的接口都要在登录后进行,那么必不可少的会使用到将登录接口的reponse返回结果中的某些参数值需要进行返回,并传递给其他接口,这样才可以进行登录后的 ...

  9. node.js接收异步任务结果的两种方法----callback和事件广播

    事件广播 发送方调用emit方法,接收方调用on方法,无论发送方或是接收方,都会工作在一个频道 声明了一个模块,用于读取mime.json中的记录 var fs = require('fs'); va ...

  10. Python 实践--混入类

    Mix-in:混入类是一种Python程序设计中的技术,作用是在运行期间动态改变类的基类或类的方法,从而使得类的表现可以发生变化.可以用在一个通用类接口中. 在实践一个 解析XML文件的实践中,体会动 ...