主要实现功能,点击一级分类,二级分类跟着变。这里主要用我们的provide

新建provide

provide文件夹下创建:child_category.dart

事件上就是这个实体:BxMallSubDto

这样我们的Provide类就写完了。

然后在main.dart中注册provide

  1. import './provide/child_category.dart';

继续category_page.dart

先引入我们的provide和childCategory的provide

  1. import 'package:provide/provide.dart';
  2. import '../provide/child_category.dart';

给左侧的类别加事件

左侧每个元素的点击事件

右侧的小分类

移动过来以后呢,这里就报错了,因为我们的list已经注释带掉了不用了。

效果展示

点击左侧大类会显示右侧的小类别

增加点击的左侧大类,高亮效果

我们用setState去解决这个问题

最终展示效果

最终代码

provide/child_category.dart

  1. import 'package:flutter/material.dart';
  2. import '../model/category.dart';
  3.  
  4. class ChildCategory with ChangeNotifier{
  5. List<BxMallSubDto> childCategoryList=[];
  6.  
  7. getChildCategory(List list)
  8. {
  9. childCategoryList=list;
  10. notifyListeners();//监听
  11. }
  12. }

lib/main.dart

  1. import 'package:flutter/material.dart';
  2. import './pages/index_page.dart';
  3. import 'package:provide/provide.dart';
  4. import './provide/counter.dart';
  5. import './provide/child_category.dart';
  6.  
  7. void main(){
  8. var counter=Counter();
  9. var providers=Providers();
  10. var childCategory=ChildCategory();
  11. //注册 依赖
  12. providers
  13. ..provide(Provider<Counter>.value(counter))
  14. ..provide(Provider<ChildCategory>.value(childCategory));
  15. runApp(ProviderNode(child: MyApp(),providers: providers,));
  16. }
  17.  
  18. class MyApp extends StatelessWidget {
  19. @override
  20. Widget build(BuildContext context) {
  21. return Container(
  22. child:MaterialApp(
  23. title:'百姓生活+',
  24. debugShowCheckedModeBanner: false,
  25. theme: ThemeData(
  26. primaryColor: Colors.pink
  27. ),
  28. home: IndexPage(),
  29. )
  30. );
  31. }
  32. }

category_page.dart

  1. import 'package:flutter/material.dart';
  2. import '../service/service_method.dart';
  3. import 'dart:convert';
  4. import '../model/category.dart';
  5. import 'package:flutter_screenutil/flutter_screenutil.dart';
  6. import 'package:provide/provide.dart';
  7. import '../provide/child_category.dart';
  8.  
  9. class CategoryPage extends StatefulWidget {
  10. @override
  11. _CategoryPageState createState() => _CategoryPageState();
  12. }
  13.  
  14. class _CategoryPageState extends State<CategoryPage> {
  15. @override
  16. Widget build(BuildContext context) {
  17. //_getCategory();
  18. return Scaffold(
  19. appBar: AppBar(title: Text('商品分类'),),
  20. body: Container(
  21. child: Row(
  22. children: <Widget>[
  23. LeftCategoryNav(),
  24. Column(
  25. children: <Widget>[
  26. RightCategoryNav()
  27. ],
  28. )
  29. ],
  30. ),
  31. ),
  32. );
  33. }
  34.  
  35. }
  36.  
  37. //左侧大类导航
  38. class LeftCategoryNav extends StatefulWidget {
  39. @override
  40. _LeftCategoryNavState createState() => _LeftCategoryNavState();
  41. }
  42.  
  43. class _LeftCategoryNavState extends State<LeftCategoryNav> {
  44. List list=[];
  45. var listIndex=0;
  46. @override
  47. void initState() {
  48. super.initState();
  49. _getCategory();//请求接口的数据
  50. }
  51. @override
  52. Widget build(BuildContext context) {
  53. return Container(
  54. width: ScreenUtil().setWidth(180),
  55. decoration: BoxDecoration(
  56. border: Border(
  57. right: BorderSide(width:1.0,color: Colors.black12),//有边框
  58. )
  59. ),
  60. child: ListView.builder(
  61. itemCount: list.length,
  62. itemBuilder: (contex,index){
  63. return _leftInkWell(index);
  64. },
  65. ),
  66. );
  67. }
  68.  
  69. Widget _leftInkWell(int index){
  70. bool isClick=false;
  71. isClick=(index==listIndex)?true:false;
  72. return InkWell(
  73. onTap: (){
  74. setState(() {
  75. listIndex=index;
  76. });
  77. var childList=list[index].bxMallSubDto;//当前大类的子类的列表
  78. Provide.value<ChildCategory>(context).getChildCategory(childList);
  79. },
  80. child: Container(
  81. height: ScreenUtil().setHeight(100),
  82. padding: EdgeInsets.only(left:10.0,top:10.0),
  83. decoration: BoxDecoration(
  84. color: isClick?Colors.black26: Colors.white,
  85. border: Border(
  86. bottom: BorderSide(width: 1.0,color: Colors.black12)
  87. )
  88. ),
  89. child: Text(
  90. list[index].mallCategoryName,
  91. style: TextStyle(fontSize: ScreenUtil().setSp(28)),//设置字体大小,为了兼容使用setSp
  92. ),
  93. ),
  94. );
  95. }
  96. void _getCategory() async{
  97. await request('getCategory').then((val){
  98. var data=json.decode(val.toString());
  99. //print(data);
  100. CategoryModel category= CategoryModel.fromJson(data);
  101. setState(() {
  102. list=category.data;
  103. });
  104. //list.data.forEach((item)=>print(item.mallCategoryName));
  105. });
  106. }
  107. }
  108.  
  109. class RightCategoryNav extends StatefulWidget {
  110. @override
  111. _RightCategoryNavState createState() => _RightCategoryNavState();
  112. }
  113.  
  114. class _RightCategoryNavState extends State<RightCategoryNav> {
  115. //List list = ['名酒','宝丰','北京二锅头','舍得','五粮液','茅台','散白'];
  116. @override
  117. Widget build(BuildContext context) {
  118. return Provide<ChildCategory>(
  119. builder: (context,child,childCategory){
  120. return Container(
  121. height: ScreenUtil().setHeight(80),
  122. width: ScreenUtil().setWidth(570),//总的宽度是750 -180
  123. decoration: BoxDecoration(
  124. color: Colors.white,//白色背景
  125. border: Border(
  126. bottom: BorderSide(width: 1.0,color: Colors.black12)//边界线
  127. )
  128. ),
  129. child: ListView.builder(
  130. scrollDirection: Axis.horizontal,
  131. itemCount: childCategory.childCategoryList.length,
  132. itemBuilder: (context,index){
  133. return _rightInkWell(childCategory.childCategoryList[index]);
  134. },
  135. ),
  136. );
  137. }
  138. );
  139. }
  140.  
  141. Widget _rightInkWell(BxMallSubDto item){
  142. return InkWell(
  143. onTap: (){},//事件留空
  144. child: Container(//什么都加一个container,这样好布局
  145. padding: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 10.0),//上下是10 左右是5.0
  146. child: Text(
  147. item.mallSubName,
  148. style:TextStyle(fontSize: ScreenUtil().setSp(28)),
  149. ),
  150. ),
  151. );
  152. }
  153. }

.

Flutter移动电商实战 --(26)列表页_使用Provide控制子类-2的更多相关文章

  1. Flutter实战视频-移动电商-26.列表页_使用Provide控制子类-2

    26.列表页_使用Provide控制子类-2 主要实现功能,点击一级分类,二级分类跟着变.这里主要用哦我们的provide 新建provide provide文件夹下创建:child_category ...

  2. Flutter实战视频-移动电商-25.列表页_使用Provide控制子类-1

    25.列表页_使用Provide控制子类-1 主要是二级分类的UI布局 1分15秒 生成我们的右侧动态类 定义list变量 开始写里面的子项,把每一个小的写了 再拼成一个大的 这样我们的小类就写完了 ...

  3. Flutter移动电商实战 --(25)列表页_使用Provide控制子类-1

    主要是二级分类的UI布局 生成我们的右侧动态类 定义list变量 开始写里面的子项,把每一个小的写了 再拼成一个大的 这样我们的小类就写完了 开始写我的大类别:是一个横向的ListView.写横向的L ...

  4. Flutter移动电商实战 --(28)列表页_商品列表后台接口调试

    主要调试商品列表页的接口 这个接口是最难的因为有大类.小类还有上拉加载 先配置接口 config/service_url.dart //const serviceUrl='http://test.ba ...

  5. Flutter移动电商实战 --(51)购物车_Provide中添加商品

    新加provide的cart.dart页面 引入三个文件.开始写provide类.provide需要用with 进行混入 从prefs里面获取到数据,判断有没有数据,如果有数据就返转正List< ...

  6. Flutter移动电商实战 --(40)路由_Fluro的全局注入和使用方法

    路由注册到顶层,使每个页面都可以使用,注册到顶层就需要在main.dart中 main.dart注册路由 注入 onGenerateRoute是MaterialApp自带的路由配置项, 首页跳转到详细 ...

  7. Flutter移动电商实战 --(35)列表页_上拉加载更多制作

    右侧列表上拉加载配合类别的切换 上拉加载需要一个page参数,当点击大类或者小类的时候,这个page就要变成1 provide内定义参数 首先我们需要定义一个page的变量 下图是我们之前在首页的时候 ...

  8. Flutter移动电商实战 --(34)列表页_小BUG的修复

    当高粱酒的子类没有数据返回的时候就会报错. 解决接口空数据报错的问题 没有数据的时候,给用户一个友好的提示, 我们没有数据的时候还要告诉用户,提示一下他没有数据,在我们的右侧列表的build方法内去判 ...

  9. Flutter移动电商实战 --(33)列表页_子类和商品列表交互效果

    主要实现点击小类下面的列表跟着切换 获取右侧下面的列表信息,即要传递大类的id也要传递小类的,所以需要把左侧的大类的id也要Provide化 可以看下网站上的接口说明: https://jspang. ...

随机推荐

  1. SourceTree撤销commit

    参考链接:http://blog.csdn.net/gang544043963/article/details/71511958 重点是:选中提交之前的版本,再进行撤销回退  

  2. Python 多进程拷贝文件夹案例

    import os import multiprocessing def copy_file(q, file_name, old_folder_name, new_folder_name): &quo ...

  3. Struts2系列漏洞起始篇

    前言 到目前位置struts2的漏洞编号已经到了S2-057,一直想系统的学习下Struts2的漏洞,但由于工作量较大,一直搁浅.最近由于工作需要,借此机会来填下坑.个人认为一个框架漏洞出来了仅仅看下 ...

  4. 通过Nginx统计网站的PV、UV、IP

    转载:通过Nginx统计网站的PV.UV.IP 概念 UV:独立访客:以cookie为依据,假设一台电脑装有3个不同的浏览器,分别打开同一个页面,将会产生3个UV.PV:访问量:页面每访问或刷新一次, ...

  5. LINUX安装Tree软件包

     1.将镜像光盘放入光驱中 查看当前LINUX版本号:cat  /etc/redhat-release 2.挂载镜像文件 mount  /dev/cdrom /mnt mount :挂载设备命令 /d ...

  6. with语句和空语句

    with语句能够为一组语句创建缺省的对象,在一组语句中,任何不指定对象的属性引用都将被认为是缺省对象. 语法如下: with(object){ statements; } <body> & ...

  7. linux修改文件系统注册设备

  8. Linux用户管理——useradd

    除了useradd还有一个命令adduser,两者是链接关系 [root@51cto ~]# which adduser /usr/sbin/adduser [root@51cto ~]# which ...

  9. http文件服务器上传与下载功能

    https://www.cnblogs.com/liferecord/p/4843615.html

  10. hibernate步骤和配置

    1.引入hibernate的jar包和数据库驱动包 2.src添加hibernate.cfg.xml(hibernate配置文件) 3.数据库编写pojo public class Test { pu ...