当前位置: 首页 > wzjs >正文

江阴网站建设公司做网站的叫什么软件

江阴网站建设公司,做网站的叫什么软件,软件网站模版,东莞手机网站设计公司可以用ScrollController组件来实现这样列表上拉加载更多的功能: 1. 定义变量 在StatefulWidget 的组件内,添加三个属性: // 滚动视图的控制器final ScrollController _scrollController ScrollController();// 是否已显示了上拉加载中bool _isShowM…

可以用ScrollController组件来实现这样列表上拉加载更多的功能:
请添加图片描述

1. 定义变量

StatefulWidget 的组件内,添加三个属性:

// 滚动视图的控制器final ScrollController _scrollController = ScrollController();// 是否已显示了上拉加载中bool _isShowMore = false;// 是否有更多的数据bool _isHaveMoreData = true;

2. 在初始化的方法中,添加对ScrollController的监听

重写initState的方法,并在initState方法中,添加ScrollController的监听,代码如下:

void initState() {super.initState();// 对 _scrollController 添加监听_scrollController.addListener((){// 内容视图最大的高度double maxScrollExtent = _scrollController.position.maxScrollExtent;// 视图滚动的大小double scrollContentOffY = _scrollController.position.pixels;if (scrollContentOffY > maxScrollExtent + Screenadapter.height(40)) {// 超出了内容视图高度的20,就重新上拉加载更多if (_isShowMore == false && _isHaveMoreData == true) {// 未显示, print("上拉加载更多");// 添加网络请求_getProductListDataRequest();}}});

3. 在ListView中,关联ScrollController

代码如下:

ListView.builder(// 关联 ScrollControllercontroller: _scrollController,itemBuilder: (BuildContext context, int index) {return Column();}
)

4. 写一个简易的Loading的加载视图

代码如下:

import 'package:fangjd/Services/ScreenAdapter.dart';
import 'package:flutter/material.dart';class Loadingwidget extends StatelessWidget {const Loadingwidget({super.key});Widget build(BuildContext context) {return Center(child: Container(padding: EdgeInsets.all(Screenadapter.width(20)),child: Row(mainAxisAlignment: MainAxisAlignment.center,children: [CircularProgressIndicator(strokeWidth: 1.0),SizedBox(width: Screenadapter.width(20)),Text("加载中....")],),),);}
}

5. 在列表的底部视图,添加加载中的视图或者添加我是我底线的视图

代码如下:

// 商品列表底部的视图Widget _productBottomWiget(int index) {if (_isHaveMoreData == true) {// 如果有更多的数据,上拉,就显示“加载中”, 其中_productList 是数据源if (_isShowMore && (index == _productList.length -1)) {// 只有最后一个数据,才添加 “加载中”的loading视图return Loadingwidget();} else {// 其他数据 就什么都不显示return SizedBox(height: 1);}} else {// 如果没有更多的数据,if (index == _productList.length -1) {// 在最后一个数据,添加 “--我也是有底线的--”的视图return Padding(padding:EdgeInsets.only(top: Screenadapter.height(30)), child: Text("--我也是有底线的--", textAlign: TextAlign.center,));} else {// 其他数据 就什么都不显示return SizedBox(height: 1);}}}

6. 模拟网络数据的请求

代码如下:

// -----网络请求--------void _getProductListDataRequest() async {setState(() {// 当调用网络请求的时候,设置_isShowMore 为 true, 表示需要展示“加载中”的底部视图_isShowMore = true;});// 模拟网络延迟await Future.delayed(Duration(seconds: 2)); List<ProductItemModel> mockDataList = _mockData();setState(() {_productList.addAll(mockDataList);// 等网路请求成功后,设置 _isShowMore 为 false, 表示 隐藏 “加载中”的底部视图_isShowMore = false;if (_productList.length > 20) {// 模拟没有更多数据了,就展示“我是有底线的”的视图 _isHaveMoreData = false;}});}// 模拟数据List<ProductItemModel> _mockData() {// returnList<ProductItemModel> mockList = [];for (var index in [1,2,3,4,5,6,7,8,9,0]) {var model = ProductItemModel(iId: 1, title: '笔记本电脑$index', price: "¥3980", oldPrice: "¥4999", pic: "https://www.itying.com/images/flutter/hot${index+1}.jpg");mockList.add(model);}return mockList;}

7. 完整的代码实例

import 'package:fangjd/CommonWidget/LoadingWidget.dart';
import 'package:fangjd/Models/ProductModel.dart';
import 'package:fangjd/Services/ScreenAdapter.dart';
import 'package:flutter/material.dart';class ProductlistPage extends StatefulWidget {const ProductlistPage({super.key});State<ProductlistPage> createState() => _ProductlistPageState();
}class _ProductlistPageState extends State<ProductlistPage> {// ScaffoldState, 控制侧边栏的显示final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();// 筛选导航栏的索引int _selectIndex = 1;// 商品列表的数据List<ProductItemModel> _productList = [];// 滚动视图的控制器final ScrollController _scrollController = ScrollController();// 是否已显示了上拉加载中bool _isShowMore = false;// 是否有更多的数据bool _isHaveMoreData = true;void initState() {super.initState();_scrollController.addListener((){// 内容视图最大的高度double maxScrollExtent = _scrollController.position.maxScrollExtent;// 视图滚动的大小double scrollContentOffY = _scrollController.position.pixels;if (scrollContentOffY > maxScrollExtent + Screenadapter.height(40)) {// 超出了内容视图高度的20,就重新上拉加载更多if (_isShowMore == false && _isHaveMoreData == true) {// 未显示_getProductListDataRequest();}}});// -----网络请求--------_getProductListDataRequest();}// -----网络请求--------void _getProductListDataRequest() async {setState(() {_isShowMore = true;});// 模拟网络延迟await Future.delayed(Duration(seconds: 2)); List<ProductItemModel> mockDataList = _mockData();setState(() {_productList.addAll(mockDataList);_isShowMore = false;if (_productList.length > 20) {_isHaveMoreData = false;}});}// 模拟数据List<ProductItemModel> _mockData() {// returnList<ProductItemModel> mockList = [];for (var index in [1,2,3,4,5,6,7,8,9,0]) {var model = ProductItemModel(iId: 1, title: '笔记本电脑$index', price: "¥3980", oldPrice: "¥4999", pic: "https://www.itying.com/images/flutter/hot${index+1}.jpg");mockList.add(model);}return mockList;}// -----视图设置--------// 设置 筛选透视图的底部选中的线Border _selectHeaderBoder() {return Border(bottom: BorderSide(width: 1,color: Colors.red));}// 筛选导航栏Widget _subHeaderWidget() {return Positioned(top: 0.0,child: Container(height: Screenadapter.height(80),width: Screenadapter.screenWidth(),decoration: BoxDecoration(border: Border(bottom: BorderSide(width: 1,color: Colors.black12),),),child: Row(children: [Expanded(child: InkWell(onTap: () {setState(() {_selectIndex = 1;});},child: Container(decoration: BoxDecoration(border: _selectIndex == 1 ? _selectHeaderBoder() : null),child: Center(child: Text("综合", style: TextStyle(color: _selectIndex == 1 ? Colors.red : Colors.black))),),)),Expanded(child: InkWell(onTap: () {setState(() {_selectIndex = 2;});},child: Container(decoration: BoxDecoration(border: _selectIndex == 2 ? _selectHeaderBoder() : null),child: Center(child: Text("销量", style: TextStyle(color: _selectIndex == 2 ? Colors.red : Colors.black))),),)),Expanded(child: InkWell(onTap: () {setState(() {_selectIndex = 3;});},child: Container(decoration: BoxDecoration(border: _selectIndex == 3 ? _selectHeaderBoder() : null),child: Center(child: Text("价格", style: TextStyle(color: _selectIndex == 3 ? Colors.red : Colors.black))),),)),Expanded(child: InkWell(onTap: () {setState(() {_selectIndex = 4;});_scaffoldKey.currentState?.openEndDrawer();},child: Container(decoration: BoxDecoration(border: _selectIndex == 4 ? _selectHeaderBoder() : null),child: Center(child: Text("筛选", style: TextStyle(color: _selectIndex == 4 ? Colors.red : Colors.black))),),)),],),));}// 商品列表底部的视图Widget _productBottomWiget(int index) {if (_isHaveMoreData == true) {if (_isShowMore && (index == _productList.length -1)) {return Loadingwidget();} else {return Text("");}} else {if (index == _productList.length -1) {return Padding(padding:EdgeInsets.only(top: Screenadapter.height(30)), child: Text("--我也是有底线的--", textAlign: TextAlign.center,));} else {return SizedBox(height: 1);}}}// 商品列表Widget _productListWidget() {if (_productList.isNotEmpty) {return  Container(padding: EdgeInsets.all(Screenadapter.width(10)),margin: EdgeInsets.only(top: Screenadapter.height(80)),child: ListView.builder(controller: _scrollController,itemBuilder: (BuildContext context, int index) {ProductItemModel itemModel = _productList[index];return Column(children: [Row(children: [Container(padding: EdgeInsets.all(Screenadapter.width(20)),width: Screenadapter.width(220),height: Screenadapter.height(220),child: AspectRatio(aspectRatio: 1,child: Image.network(itemModel.pic, fit: BoxFit.cover)),),Expanded(child: Container(height: Screenadapter.height(220),padding: EdgeInsets.only(top: Screenadapter.height(10)),child: Column(mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.start,children: [Text(itemModel.title,maxLines: 2,overflow: TextOverflow.ellipsis,),SizedBox(height: Screenadapter.height(20)),Container(height: Screenadapter.height(36),width: double.infinity,child: Row(children: [Container(height: Screenadapter.height(40),width: Screenadapter.width(80),decoration: BoxDecoration(borderRadius: BorderRadius.circular(Screenadapter.height(18)),color: Colors.black12),child: Center(child: Text(' 4G ', style: TextStyle(color: Colors.black87))),),SizedBox(width: Screenadapter.width(20)),Container(height: Screenadapter.height(40),width: Screenadapter.width(80),decoration: BoxDecoration(borderRadius: BorderRadius.circular(Screenadapter.height(18)),color: Colors.black12),child: Center(child: Text(' 126 ', style: TextStyle(color: Colors.black87))),),],),),SizedBox(height: Screenadapter.width(20)),Text(itemModel.price, style: TextStyle(color: Colors.red, fontSize: 18))])))],),Divider(height: Screenadapter.height(2)),// 添加列表底部视图:显示“加载中”还是显示“我是有底线的”_productBottomWiget(index)],);},itemCount: _productList.length),);} else {return Loadingwidget();}}Widget build(BuildContext context) {return Scaffold(key: _scaffoldKey,appBar: AppBar(title: Text("商品分类"),actions: [// 去掉’endDrawer‘默认设置的图标Text("")],),endDrawer: Drawer(child: Center(child: Text("我是侧边栏")),),body: Stack(children: [// 筛选导航栏_subHeaderWidget(),// 商品列表_productListWidget()],),);}
}
http://www.dtcms.com/wzjs/808749.html

相关文章:

  • 哈尔滨营销网站建设公司哪家好专门做招商的网站是什么情况
  • 怎么建设咨询网站好的交互网站
  • php可以做手机网站吗网站代码上传到服务器后要怎么做的
  • 货代一般用什么网站开发客户摄影设计英文
  • 网上营销推广网站打开很慢怎么做优化
  • 遵义住房和城乡建设局网站app软件开发的费用和流程
  • 网站建设综合训练佛山市骏域网站建设
  • 电商网站改版方案网上怎么找客户资源
  • 用自己照片做衣服 杯子的是哪个网站在线网站搭建系统
  • 中国交通建设官方网站网络外贸运营怎么做
  • 天水 网站建设 招聘人力资源三网站建设
  • 二手购物网站策划书国内网页设计培训
  • 湖南省建设银行网站官网网站客户留言
  • 做网上贸易哪个网站好手机可以搭建网站么
  • 网页制作与网站建设知识框架图中科院网站做的好的院所
  • 深圳建站公司优化网站开发模式名词
  • 网站开发 简历宁波网络推广seo软件
  • 用手机做网站好学吗哪些网站是动态的
  • 做网站用最新软件win 7怎么建立wordpress
  • 从域名角度看网站建设注意事项h5网站和响应式网站区别
  • 网站免费推广100种方法天元建设集团有限公司破产重组
  • 品牌制作网站广州装饰公司
  • 深圳傻瓜式网站建设公司好吗成立公司要多少钱
  • 北京免费模板建站手机优化大师为什么扣钱
  • 都江堰市网站建设全网拓客app
  • 营销型制作网站公司智慧团建登录手机版正式版
  • 国家开发银行助学贷款网站怎样用织梦做网站
  • 网站建设的五个基本要素大型公司网站制作
  • 高校微信网站建设情况汇报前台登录wordpress
  • 免费查找资料的网站网站服务器在国外的如何做百度推广