大家好,我是一名flutter爱好者,ahyang

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

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

class _CategoryPageState extends State<CategoryPage> {
  String _data = '加载中';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商品分类'),
        centerTitle: true,
        elevation: 0,
      ),
      body: Row(
        children: <Widget>[
          LeftCategoryNav(),
          Column(
            children: <Widget>[
              RightCategoryNav(),
              CategoryGoodsList(),
            ],
          ),
        ],
      ),
    );
  }
}

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

class _LeftCategoryState extends State<LeftCategoryNav> {
  List list = List();
  var listIndex = 0;

  void initState() {
    super.initState();
    _getCategory();
    getGoodsList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(180),
      decoration: BoxDecoration(
          border: Border(
        right: BorderSide(width: 1, color: Colors.black12),
      )),
      child: ListView.builder(
        itemCount: list.length,
        itemBuilder: (context, index) {
          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;
        Provide.value<ChildCategory>(context)
            .getChildCategory(childList, categoryId);
        getGoodsList(categoryId: categoryId);
      },
      child: Container(
        height: ScreenUtil().setHeight(100),
        padding: EdgeInsets.only(left: 10, top: 20),
        decoration: BoxDecoration(
            color: isClick ? Color.fromRGBO(236, 236, 236, 1.0) : Colors.white,
            border: Border(
              bottom: BorderSide(width: 1, color: Colors.black12),
            )),
        child: Text(
          list[index].mallCategoryName,
          style: TextStyle(
            fontSize: ScreenUtil().setSp(28),
          ),
        ),
      ),
    );
  }

  _getCategory() async {
    await request('getCategory').then((val) {
      var data = json.decode(val.toString());
      CategoryModle category = CategoryModle.fromJson(data);
      setState(() {
        list = category.data;
      });
      Provide.value<ChildCategory>(context)
          .getChildCategory(list[0].bxMallSubDto, list[0].mallCategoryId);
    });
  }

  getGoodsList({String categoryId}) {
    var data = {
      'categoryId': categoryId == null ? '4' : categoryId,
      'categorySubId': '',
      'page': 1,
    };

    request('getMallGoods', formData: data).then((val) {
      var data = json.decode(val.toString());
      CategoryGoodsListModel _listModel = CategoryGoodsListModel.fromJson(data);
      Provide.value<CategoryGoodsListProvide>(context)
          .getGoodsList(_listModel.data);
      // setState(() {
      //   list = _listModel.data;
      // });
    });
  }
}

// 顶部状态管理分类
class RightCategoryNav extends StatefulWidget {
  @override
  _RightCategoryNavState createState() => _RightCategoryNavState();
}

class _RightCategoryNavState extends State<RightCategoryNav> {
  // List list = ['零度', '雪花', '动力', '名酒', '白酒', '红酒', '烈酒', '葡萄酒'];

  @override
  Widget build(BuildContext context) {
    return Provide<ChildCategory>(
      builder: (context, child, childCategory) {
        return Container(
          height: ScreenUtil().setHeight(80),
          width: ScreenUtil().setWidth(570),
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: childCategory.childCategoryList.length,
            itemBuilder: (context, index) {
              return _rightInWell(
                  index, childCategory.childCategoryList[index]);
            },
          ),
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border(
              bottom: BorderSide(
                width: 1,
                color: Colors.black12,
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _rightInWell(int index, BxMallSubDto item) {
    bool isClick = false;
    isClick = (index == Provide.value<ChildCategory>(context).childIndex)
        ? true
        : false;

    return InkWell(
      onTap: () {
        Provide.value<ChildCategory>(context).changeChildIndex(index, item.mallSubId);
        getGoodsList(item.mallSubId);
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 10.0),
        child: Text(
          item.mallSubName,
          style: TextStyle(
              fontSize: ScreenUtil().setSp(28),
              color: isClick ? Colors.pink : null),
        ),
      ),
    );
  }

  getGoodsList(String categorySubId) {
    var data = {
      'categoryId': Provide.value<ChildCategory>(context).categoryId,
      'categorySubId': categorySubId,
      'page': 1,
    };

    request('getMallGoods', formData: data).then((val) {
      var data = json.decode(val.toString());
      CategoryGoodsListModel _listModel = CategoryGoodsListModel.fromJson(data);
      if (_listModel.data == null) {
        Provide.value<CategoryGoodsListProvide>(context).getGoodsList([]);
      } else {
        Provide.value<CategoryGoodsListProvide>(context)
            .getGoodsList(_listModel.data);
      }
      // setState(() {
      //   list = _listModel.data;
      // });
    });
  }
}

// 商品列表,可以上拉加载
class CategoryGoodsList extends StatefulWidget {
  @override
  _CategoryGoodsListState createState() => _CategoryGoodsListState();
}

class _CategoryGoodsListState extends State<CategoryGoodsList> {
  // List list = [];

  @override
  void initState() {
    super.initState();
    // getGoodsList();
  }

  @override
  Widget build(BuildContext context) {
    return Provide<CategoryGoodsListProvide>(
      builder: (context, child, data) {
        if(data.goodsList.length > 0) {
          return Expanded(
          child: Container(
            width: ScreenUtil().setWidth(570),
            child: ListView.builder(
              itemCount: data.goodsList.length,
              itemBuilder: (context, index) {
                return _listView(data.goodsList, index);
              },
            ),
          ),
        );
        } else {
          return Text('没有数据,嗯好义熙');
        }
      },
    );
  }

  Widget _goodsImage(newList, index) {
    return Container(
      padding: EdgeInsets.only(left: 10),
      width: ScreenUtil().setWidth(200),
      child: Image.network(newList[index].image),
    );
  }

  Widget _goodsName(newList, index) {
    return Container(
      padding: EdgeInsets.only(left: 10),
      width: ScreenUtil().setWidth(370),
      child: Text(
        newList[index].goodsName,
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: ScreenUtil().setSp(28),
        ),
      ),
    );
  }

  Widget _goodsPrice(newList, index) {
    return Container(
      width: ScreenUtil().setWidth(370),
      margin: EdgeInsets.only(top: 20),
      padding: EdgeInsets.only(left: 10),
      child: Row(
        children: <Widget>[
          Text(
            '价格:¥${newList[index].presentPrice}',
            style: TextStyle(
              fontSize: ScreenUtil().setSp(30),
              color: Colors.pink,
            ),
          ),
          Text(
            '${newList[index].oriPrice}',
            style: TextStyle(
              color: Colors.black26,
              decoration: TextDecoration.lineThrough,
            ),
          )
        ],
      ),
    );
  }

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

 这是分割线,

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。 import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:baixingg/service/service_method.dart'; import 'package:baixingg/model/category.dart'; import 'package:baixingg/model/goods_list.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:provide/provide.dart'; import 'package:baixingg/provide/child_category.dart'; import 'package:baixingg/provide/category_goods_list.dart';
class CategoryPage extends StatefulWidget { @override _CategoryPageState createState() => _CategoryPageState(); }
class _CategoryPageState extends State<CategoryPage> { String _data = '加载中'; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('商品分类'), centerTitle: true, elevation: 0, ), body: Row( children: <Widget>[ LeftCategoryNav(), Column( children: <Widget>[ RightCategoryNav(), CategoryGoodsList(), ], ), ], ), ); } }
// 左侧大类导航 class LeftCategoryNav extends StatefulWidget { @override _LeftCategoryState createState() => _LeftCategoryState(); }
class _LeftCategoryState extends State<LeftCategoryNav> { List list = List(); var listIndex = 0;
void initState() { super.initState(); _getCategory(); getGoodsList(); }
@override Widget build(BuildContext context) { return Container( width: ScreenUtil().setWidth(180), decoration: BoxDecoration( border: Border( right: BorderSide(width: 1, color: Colors.black12), )), child: ListView.builder( itemCount: list.length, itemBuilder: (context, index) { 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; Provide.value<ChildCategory>(context) .getChildCategory(childList, categoryId); getGoodsList(categoryId: categoryId); }, child: Container( height: ScreenUtil().setHeight(100), padding: EdgeInsets.only(left: 10, top: 20), decoration: BoxDecoration( color: isClick ? Color.fromRGBO(236, 236, 236, 1.0) : Colors.white, border: Border( bottom: BorderSide(width: 1, color: Colors.black12), )), child: Text( list[index].mallCategoryName, style: TextStyle( fontSize: ScreenUtil().setSp(28), ), ), ), ); }
_getCategory() async { await request('getCategory').then((val) { var data = json.decode(val.toString()); CategoryModle category = CategoryModle.fromJson(data); setState(() { list = category.data; }); Provide.value<ChildCategory>(context) .getChildCategory(list[0].bxMallSubDto, list[0].mallCategoryId); }); }
getGoodsList({String categoryId}) { var data = { 'categoryId': categoryId == null ? '4' : categoryId, 'categorySubId': '', 'page': 1, };
request('getMallGoods', formData: data).then((val) { var data = json.decode(val.toString()); CategoryGoodsListModel _listModel = CategoryGoodsListModel.fromJson(data); Provide.value<CategoryGoodsListProvide>(context) .getGoodsList(_listModel.data); // setState(() { // list = _listModel.data; // }); }); } }
// 顶部状态管理分类 class RightCategoryNav extends StatefulWidget { @override _RightCategoryNavState createState() => _RightCategoryNavState(); }
class _RightCategoryNavState extends State<RightCategoryNav> { // List list = ['零度', '雪花', '动力', '名酒', '白酒', '红酒', '烈酒', '葡萄酒'];
@override Widget build(BuildContext context) { return Provide<ChildCategory>( builder: (context, child, childCategory) { return Container( height: ScreenUtil().setHeight(80), width: ScreenUtil().setWidth(570), child: ListView.builder( scrollDirection: Axis.horizontal, itemCount: childCategory.childCategoryList.length, itemBuilder: (context, index) { return _rightInWell( index, childCategory.childCategoryList[index]); }, ), decoration: BoxDecoration( color: Colors.white, border: Border( bottom: BorderSide( width: 1, color: Colors.black12, ), ), ), ); }, ); }
Widget _rightInWell(int index, BxMallSubDto item) { bool isClick = false; isClick = (index == Provide.value<ChildCategory>(context).childIndex) ? true : false;
return InkWell( onTap: () { Provide.value<ChildCategory>(context).changeChildIndex(index, item.mallSubId); getGoodsList(item.mallSubId); }, child: Container( padding: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 10.0), child: Text( item.mallSubName, style: TextStyle( fontSize: ScreenUtil().setSp(28), color: isClick ? Colors.pink : null), ), ), ); }
getGoodsList(String categorySubId) { var data = { 'categoryId': Provide.value<ChildCategory>(context).categoryId, 'categorySubId': categorySubId, 'page': 1, };
request('getMallGoods', formData: data).then((val) { var data = json.decode(val.toString()); CategoryGoodsListModel _listModel = CategoryGoodsListModel.fromJson(data); if (_listModel.data == null) { Provide.value<CategoryGoodsListProvide>(context).getGoodsList([]); } else { Provide.value<CategoryGoodsListProvide>(context) .getGoodsList(_listModel.data); } // setState(() { // list = _listModel.data; // }); }); } }
// 商品列表,可以上拉加载 class CategoryGoodsList extends StatefulWidget { @override _CategoryGoodsListState createState() => _CategoryGoodsListState(); }
class _CategoryGoodsListState extends State<CategoryGoodsList> { // List list = [];
@override void initState() { super.initState(); // getGoodsList(); }
@override Widget build(BuildContext context) { return Provide<CategoryGoodsListProvide>( builder: (context, child, data) { if(data.goodsList.length > 0) { return Expanded( child: Container( width: ScreenUtil().setWidth(570), child: ListView.builder( itemCount: data.goodsList.length, itemBuilder: (context, index) { return _listView(data.goodsList, index); }, ), ), ); } else { return Text('没有数据,嗯好义熙'); } }, ); }
Widget _goodsImage(newList, index) { return Container( padding: EdgeInsets.only(left: 10), width: ScreenUtil().setWidth(200), child: Image.network(newList[index].image), ); }
Widget _goodsName(newList, index) { return Container( padding: EdgeInsets.only(left: 10), width: ScreenUtil().setWidth(370), child: Text( newList[index].goodsName, maxLines: 2, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: ScreenUtil().setSp(28), ), ), ); }
Widget _goodsPrice(newList, index) { return Container( width: ScreenUtil().setWidth(370), margin: EdgeInsets.only(top: 20), padding: EdgeInsets.only(left: 10), child: Row( children: <Widget>[ Text( '价格:¥${newList[index].presentPrice}', style: TextStyle( fontSize: ScreenUtil().setSp(30), color: Colors.pink, ), ), Text( '${newList[index].oriPrice}', style: TextStyle( color: Colors.black26, decoration: TextDecoration.lineThrough, ), ) ], ), ); }
Widget _listView(newList, index) { return InkWell( child: Container( padding: EdgeInsets.only(top: 10, bottom: 10), decoration: BoxDecoration( border: Border( bottom: BorderSide( width: 1, color: Colors.black12, ), ), ), child: Row( children: <Widget>[ _goodsImage(newList, index), Column( children: <Widget>[ _goodsName(newList, index), _goodsPrice(newList, index), ], ), ], ), ), onTap: () {}, ); } }
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄