Flutter mobile provider combat - (33) List _ subclasses and the Product List interactivity

Click on the list of the main achievement of the following subcategories followed switching

Get on the right side below the list of information that must be passed id categories of transfer sub-categories, so it is necessary to categories on the left side of id have to Provide

Interfaces can look on the website:

https://jspang.com/posts/2019/03/01/flutter-shop.html#%E5%90%8E%E7%AB%AF%E6%8E%A5%E5%8F%A3api%E6%96%87%E6%A1%A3

Id Provide broad categories of

When we click on the categories on the left side of the time, we should keep up the current category id.

category_page.dart

After more than we modified the above parameters passed, then the page where the error will be the category_page.dart

Our categories respectively passed id

That we get rid of async and await modifiers

We copied this method to the following _getGoodsList

Copy to the following methods _rightInkWell

Already have this data access method, the next step is to call our approach

data demonstration:

The final code:

provide/child_category.dart

Import 'Package: Flutter / material.dart'; 
Import '../model/category.dart'; 

class ChildCategory with ChangeNotifier { 
 List <BxMallSubDto> childCategoryList = []; 
 int the childIndex = 0; // index highlighted subclass 
 String categoryId = '4'; // default category ID to 4 liquor id 
  // categories switching logic 
  getChildCategory (List <BxMallSubDto> List, String id) { 
    the childIndex = 0; // CPC classes, subclasses of index must be emptied out 
    categoryId the above mentioned id =; 
    BxMallSubDto all = BxMallSubDto (); 
    all.mallCategoryId = "00"; 
    all.mallCategoryId = "00"; 
    all.comments = "null"; 
    all.mallSubName = 'all'; 
    childCategoryList = [All]; 
    // childCategoryList = List; 
    childCategoryList.addAll(list);addAll (List); 
    notifyListeners (); // listen
  } 
  // change the subclass index, indexs come of it? From our specific class transfer 
  changeChildIndex (index) { 
    the childIndex = index; // pass over the index assigned to us the childIndex 
    notifyListeners (); // notify 
  } 
}

category_page.dart

import 'package:flutter/material.dart';
import '../service/service_method.dart';
import 'dart:convert';
import '../model/category.dart';
import '../model/categoryGoodsList.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import '../provide/child_category.dart';
import '../provide/category_goods_list.dart';

class CategoryPage extends StatefulWidget {
  @override
  _CategoryPageState createState() => _CategoryPageState();
}

class _CategoryPageState extends State<CategoryPage> {
  @override
  Widget build(BuildContext context) {
    //_getCategory();
    return Scaffold(
      appBar: AppBar(title: Text('商品分类'),),
      body: Container(
        child: Row(
          children: <Widget>[
            LeftCategoryNav(),
            Column(
              children: <Widget>[
                RightCategoryNav(),
                CategoryGoodsList()
              ],
            )
          ],
        ),
      ),
    );
  }

 
}

//左侧大类导航
class LeftCategoryNav extends StatefulWidget {
  @override
  _LeftCategoryNavState createState() => _LeftCategoryNavState();
}

State _LeftCategoryNavState the extends class <LeftCategoryNav> { 
        itemBuilder: (Contex, index) {
  = List List []; 
  var ListIndex = 0; 
  @override 
  void InitState () { 
    super.initState (); 
    _getCategory (); // request data interface 
    _getGoodsList (); // default parameter is optional so here is 4 can not pass value 
  } 
  @override 
  the Widget Build (BuildContext context) { 
    return Container ( 
      width:. ScreenUtil () setWidth (180 [), 
      Decoration: BoxDecoration ( 
        border: border ( 
          right: BorderSide (width: 1.0, Color: Colors.black12) // with border 
        ) 
      ), 
      Child: ListView.builder ( 
        itemCount: List.length, 
          return _leftInkWell (index); 
        }, 
      ), 
    );
  }

  Widget _leftInkWell(int index){
    bool isClick=false;
    isClick=(index==listIndex)?true:false;
    return InkWell(
      onTap: (){
        setState(() {
         listIndex=index; 
        });
        var childList=list[index].bxMallSubDto;//当前大类的子类的列表
        var categoryId=list[index].mallCategoryId;//大类的id
        Provide.value<ChildCategory>(context).getChildCategory(childList,categoryId);
        _getGoodsList(categoryId:categoryId);
      },
      child: Container(
        height: ScreenUtil().setHeight(100),
        padding: EdgeInsets.only(left:10.0,top:10.0),
        decoration: BoxDecoration(
          color: isClick?Color.fromRGBO(236, 236, 236, 1.0): Colors.white,
          border: Border(
            bottom: BorderSide(width: 1.0,color: Colors.black12)
          )
        ),
        child: Text(
          list[index].mallCategoryName,
          style: TextStyle(fontSize: ScreenUtil().setSp(28)),//设置字体大小,为了兼容使用setSp
        ),
      ),
    );
  }
   void _getCategory() async{
    await request('getCategory').then((val){
      var data=json.decode(val.toString());
      //print(data);
      CategoryModel category= CategoryModel.fromJson(data);
      the setState (() { 
       list=category.data; 
      });
      Provide.value <ChildCategory> (context) .getChildCategory (List [0] .bxMallSubDto, List [0] .mallCategoryId); 
    }); 
  } 

  void _getGoodsList (categoryId {String}) { 
    var {Data = 
      ' categoryId ': categoryId == null?' 4 ': categoryId, // default category liquor 
      ' categorySubId ': "", 
      ' Page ':. 1 
    }; 
    Request (' getMallGoods', formData: Data) .then ((Val) { 
      var = JSON.decode Data (val.toString ()); 
      CategoryGoodsListModel goodsList = CategoryGoodsListModel.fromJson (Data); // This 'class model converted from JSON 
      // Print (' >>>>>>>> >>>>>>>>>>>>>>>>>>>>>:${goodsList.data[0].goodsName}');
      // setState(() {
      //  list=goodsList.data; 
      // });
      Provide.value <CategoryGoodsListProvide> (context) .getGoodsList (goodsList.data); 
    }); 
  } 
} 

class RightCategoryNav the extends StatefulWidget { 
  @override 
  _RightCategoryNavState createState () => _RightCategoryNavState (); 
} 

class _RightCategoryNavState the extends State <RightCategoryNav> { 
  // List list = [ 'wine', 'Fung', 'Beijing Double pot', 'willing', 'Wuliangye', 'Maotai', 'scattered white']; 
  @override 
  the Widget Build (BuildContext context) { 
    return the Provide <ChildCategory> ( 
      Builder: (context, Child, childCategory) { 
        return Container ( 
          height: ScreenUtil().setHeight(80),
          width:. ScreenUtil () setWidth (570), // total width -180 750 
          Decoration:BoxDecoration(
            color: Colors.white,//白色背景
            border: Border(
              bottom: BorderSide(width: 1.0,color: Colors.black12)//边界线
            )
          ),
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: childCategory.childCategoryList.length,
            itemBuilder: (context,index){
              return _rightInkWell(index,childCategory.childCategoryList[index]);
            },
          ),
        );
      }
    );
  }

  _RightInkWell the Widget (int index, Item BxMallSubDto) { 
    BOOL = isClick to false; 
          ),
    = isClick (index == Provide.value <ChildCategory> (context) .childIndex) to true:? to false; 

    return Inkwell ( 
      onTap in: () { 
        Provide.value <ChildCategory> (context) .changeChildIndex (index); 
        _getGoodsList (Item. mallSubId); 
      }, // event empty 
      child: Container (// everything plus a container, so good layout 
        padding: EdgeInsets.fromLTRB (5.0, 10.0, 5.0, 10.0), // is down about 10 is 5.0 
        Child: the Text ( 
          item.mallSubName, 
          style: the TextStyle ( 
            . the fontSize: ScreenUtil () setSp (28), 
            ? Color: isClick Colors.pink: Colors.black 
  } 
  void _getGoodsList (String categorySubId) {
        ), 
      ), 
    ); 
    Var {Data = 
      'categoryId': Provide.value <ChildCategory> (context) .categoryId, // category ID 
      'categorySubId': categorySubId, 
      'Page':. 1 
    }; 
    Request ( 'getMallGoods', formData: Data) .then ((Val) { 
      var = JSON.decode Data (val.toString ()); 
      CategoryGoodsListModel goodsList = CategoryGoodsListModel.fromJson (Data); // so from json 'model class converted 
      Provide.value <CategoryGoodsListProvide> (context) .getGoodsList (goodsList.data); 
    }); 
  } 
} 

// list of items, can upload the LAC 
class CategoryGoodsList the extends StatefulWidget { 
  @override 
  _CategoryGoodsListState createState () => _CategoryGoodsListState (); 
}

class _CategoryGoodsListState extends State<CategoryGoodsList> {

  @override
  void initState() {
    //_getGoodsList();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Provide<CategoryGoodsListProvide>(
      builder: (context,child,data){
        return  Expanded(
          child: Container(
            width: ScreenUtil().setWidth(570),
            //height: ScreenUtil().setHeight(974),
            child: ListView.builder(
              itemCount: data.goodsList.length,
              itemBuilder: (Contex, index) { 
                return _listWidget (data.goodsList, index); 
        maxLines: 2, line 2 // displays up
              },
            ),
          ), 
        ); 
      }, 
    ); 
    
   
  } 
  

  The Widget _goodsImage (List newList, index) { 
    return Container ( 
      width: ScreenUtil () setWidth (200), a width of 200 // set limits. 
      Child: Image.network (newList [index]. Image), 
    ); 
  } 
  the Widget _goodsName (List newList, index) { 
    return Container ( 
      padding: EdgeInsets.all (5.0), both vertical and horizontal padding // 5.0 
      width: ScreenUtil () setWidth (370 ), /. / 370 is a value of approximately 
      Child: the Text ( 
        newList [index] .goodsName, 
        overflow: TextOverflow.ellipsis, 
        style: the TextStyle (the fontSize: ScreenUtil () setSp (28)), the font size //. 
      ), 
    ); 
  }
 
  the Widget _goodsPrice (List newList, index) { 
    return Container ( 
      margin: EdgeInsets.only (Top: 20.0), // and to the above pitch 
      width: ScreenUtil () setWidth (370 ), // 370 is an approximate value. 
      Child: Row ( 
        Children: <the Widget> [ 
          the Text ( 
            'price ¥ $ {newList [index] } .presentPrice ', 
            style: the TextStyle (Color: Colors.pink, the fontSize:. ScreenUtil () setSp (30)), 
          ), 
          the Text ( 
            ' $ {newList price ¥ [index] .oriPrice} ', 
            style: the TextStyle ( 
              Color : Colors.black26, 
              Decoration: TextDecoration.lineThrough 
            ), // delete the line style 
          ) 
        ], 
      ), 
    ); 
  }

  Widget _listWidget(List newList,int index){
    return InkWell(
      onTap: (){},
      child: Container(
        padding: EdgeInsets.only(top:5.0,bottom:5.0),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            bottom: BorderSide(width: 1.0,color: Colors.black12)
          )
        ),
        child: Row(
          children: <Widget>[
            _goodsImage(newList,index),
            Column(
              children: <Widget>[
                _goodsName(newList,index),
                _goodsPrice(newList,index)
              ],
            )
          ],
        ),
      ),
    );
  }
}

.

Guess you like

Origin www.cnblogs.com/crazycode2/p/11442623.html