From c6883718b4abd9e417069769d8edd83c10f491d5 Mon Sep 17 00:00:00 2001 From: laiiihz Date: Sat, 8 May 2021 16:07:20 +0800 Subject: [PATCH] update goods list view --- lib/models/market/goods_item.dart | 31 +++++ lib/models/market/goods_item.g.dart | 21 ++++ lib/ui/market/_market_data.dart | 100 ---------------- lib/ui/market/category/category_card.dart | 5 +- lib/ui/market/category/category_page.dart | 2 +- lib/ui/market/category/category_sub_card.dart | 23 +++- lib/ui/market/category/category_sub_view.dart | 12 +- lib/ui/market/goods/goods_list_view.dart | 113 +++++++++++++----- lib/ui/market/market_page.dart | 5 +- 9 files changed, 167 insertions(+), 145 deletions(-) create mode 100644 lib/models/market/goods_item.dart create mode 100644 lib/models/market/goods_item.g.dart delete mode 100644 lib/ui/market/_market_data.dart diff --git a/lib/models/market/goods_item.dart b/lib/models/market/goods_item.dart new file mode 100644 index 00000000..ebd1369a --- /dev/null +++ b/lib/models/market/goods_item.dart @@ -0,0 +1,31 @@ +import 'package:equatable/equatable.dart'; +import 'package:json_annotation/json_annotation.dart'; + +import 'package:aku_community/model/common/img_model.dart'; + +part 'goods_item.g.dart'; + +@JsonSerializable() +class GoodsItem extends Equatable { + final int id; + final String title; + final String recommend; + final num sellingPrice; + final num markingPrice; + final int subscribeNum; + final List imgList; + GoodsItem({ + required this.id, + required this.title, + required this.recommend, + required this.sellingPrice, + required this.markingPrice, + required this.subscribeNum, + required this.imgList, + }); + @override + List get props => throw UnimplementedError(); + + factory GoodsItem.fromJson(Map json) => + _$GoodsItemFromJson(json); +} diff --git a/lib/models/market/goods_item.g.dart b/lib/models/market/goods_item.g.dart new file mode 100644 index 00000000..f446023f --- /dev/null +++ b/lib/models/market/goods_item.g.dart @@ -0,0 +1,21 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'goods_item.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +GoodsItem _$GoodsItemFromJson(Map json) { + return GoodsItem( + id: json['id'] as int, + title: json['title'] as String, + recommend: json['recommend'] as String, + sellingPrice: json['sellingPrice'] as num, + markingPrice: json['markingPrice'] as num, + subscribeNum: json['subscribeNum'] as int, + imgList: (json['imgList'] as List) + .map((e) => ImgModel.fromJson(e as Map)) + .toList(), + ); +} diff --git a/lib/ui/market/_market_data.dart b/lib/ui/market/_market_data.dart deleted file mode 100644 index 3c42c681..00000000 --- a/lib/ui/market/_market_data.dart +++ /dev/null @@ -1,100 +0,0 @@ -import 'package:flutter/material.dart'; - -import 'package:flutter_easyrefresh/easy_refresh.dart'; -import 'package:get/get.dart'; -import 'package:waterfall_flow/waterfall_flow.dart'; - -import 'package:aku_community/ui/market/goods/goods_card.dart'; -import 'package:aku_community/ui/market/goods/goods_list_view.dart'; -import 'package:aku_community/utils/headers.dart'; - -@Deprecated('NO NEED THIS CLASS IN FUTURE') -class MarketData { - final String name; - final String path; - MarketData({ - required this.name, - required this.path, - }); -} - -@Deprecated('NO NEED THIS LIST IN FUTURE') -List mockableMarketData = [ - MarketData(name: '居家生活', path: R.ASSETS_ICONS_TOOL_JJSH_PNG), - MarketData(name: '数码家电', path: R.ASSETS_ICONS_TOOL_SMJD_PNG), - MarketData(name: '休闲副食', path: R.ASSETS_ICONS_TOOL_XXFS_PNG), - MarketData(name: '滋补保健', path: R.ASSETS_ICONS_TOOL_ZBBJ_PNG), - MarketData(name: '彩妆香水', path: R.ASSETS_ICONS_TOOL_CZXS_PNG), - MarketData(name: '服饰箱包', path: R.ASSETS_ICONS_TOOL_FSXB_PNG), - MarketData(name: '母婴玩具', path: R.ASSETS_ICONS_TOOL_MYWJ_PNG), - MarketData(name: '饮料酒水', path: R.ASSETS_ICONS_TOOL_YLJS_PNG), -]; - -@Deprecated('NO NEED THIS WIDGET IN FUTURE') -class MockableMarketWidget extends StatelessWidget { - final MarketData data; - const MockableMarketWidget({Key? key, required this.data}) : super(key: key); - - @override - Widget build(BuildContext context) { - return MaterialButton( - child: Column( - children: [ - Spacer(), - Image.asset( - data.path, - height: 75.w, - width: 75.w, - ), - 12.hb, - Text( - data.name, - style: TextStyle( - fontSize: 24.sp, - color: Color(0xFF4A4B51), - ), - ), - Spacer(), - ], - ), - onPressed: () { - Get.to( - () => GoodsListView(), - ); - }, - ); - } -} - -@Deprecated('NO NEED THIS WIDGET IN FUTURE') -class MockableMarketList extends StatefulWidget { - MockableMarketList({Key? key}) : super(key: key); - - @override - _MockableMarketListState createState() => _MockableMarketListState(); -} - -class _MockableMarketListState extends State { - @override - Widget build(BuildContext context) { - return EasyRefresh( - header: MaterialHeader(completeDuration: Duration(milliseconds: 300)), - onRefresh: () async {}, - child: WaterfallFlow( - padding: EdgeInsets.all(32.w), - gridDelegate: SliverWaterfallFlowDelegateWithFixedCrossAxisCount( - crossAxisCount: 2, - mainAxisSpacing: 20.w, - crossAxisSpacing: 20.w, - ), - children: [ - GoodsCard(), - GoodsCard(), - GoodsCard(), - GoodsCard(), - GoodsCard(), - ], - ), - ); - } -} diff --git a/lib/ui/market/category/category_card.dart b/lib/ui/market/category/category_card.dart index 14d976c2..d8e02cf9 100644 --- a/lib/ui/market/category/category_card.dart +++ b/lib/ui/market/category/category_card.dart @@ -35,7 +35,10 @@ class CategoryCard extends StatelessWidget { ), onPressed: () { Get.to( - () => GoodsListView(), + () => GoodsListView( + model: model, + subModels: [], + ), ); }, ); diff --git a/lib/ui/market/category/category_page.dart b/lib/ui/market/category/category_page.dart index ad96c6d9..426fc5c9 100644 --- a/lib/ui/market/category/category_page.dart +++ b/lib/ui/market/category/category_page.dart @@ -103,7 +103,7 @@ class _CategoryPageState extends State controller: _tabController, physics: NeverScrollableScrollPhysics(), children: - widget.models.map((e) => CategorySubView(id: e.id)).toList(), + widget.models.map((e) => CategorySubView(model: e)).toList(), ).expand(), ], ), diff --git a/lib/ui/market/category/category_sub_card.dart b/lib/ui/market/category/category_sub_card.dart index 0140173b..e9efe7c9 100644 --- a/lib/ui/market/category/category_sub_card.dart +++ b/lib/ui/market/category/category_sub_card.dart @@ -7,8 +7,15 @@ import 'package:get/get.dart'; import 'package:aku_community/utils/headers.dart'; class CategorySubCard extends StatelessWidget { + final List subModels; + final MarketCategoryModel selectModel; final MarketCategoryModel model; - const CategorySubCard({Key? key, required this.model}) : super(key: key); + const CategorySubCard({ + Key? key, + required this.model, + required this.subModels, + required this.selectModel, + }) : super(key: key); @override Widget build(BuildContext context) { @@ -17,14 +24,14 @@ class CategorySubCard extends StatelessWidget { children: [ Spacer(), FadeInImage.assetNetwork( - image: API.image(ImgModel.first(model.imgList)), + image: API.image(ImgModel.first(selectModel.imgList)), placeholder: R.ASSETS_IMAGES_PLACEHOLDER_WEBP, height: 75.w, width: 75.w, ), 12.hb, Text( - model.name, + selectModel.name, style: TextStyle( fontSize: 24.sp, color: Color(0xFF4A4B51), @@ -33,9 +40,13 @@ class CategorySubCard extends StatelessWidget { Spacer(), ], ), - onPressed: () { - Get.to( - () => GoodsListView(), + onPressed: () async { + await Get.to( + () => GoodsListView( + model: model, + subModels: subModels, + selectSubModel: selectModel, + ), ); }, ); diff --git a/lib/ui/market/category/category_sub_view.dart b/lib/ui/market/category/category_sub_view.dart index 20b24f7b..960f92af 100644 --- a/lib/ui/market/category/category_sub_view.dart +++ b/lib/ui/market/category/category_sub_view.dart @@ -5,8 +5,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_easyrefresh/easy_refresh.dart'; class CategorySubView extends StatefulWidget { - final int id; - CategorySubView({Key? key, required this.id}) : super(key: key); + final MarketCategoryModel model; + CategorySubView({Key? key, required this.model}) : super(key: key); @override _CategorySubViewState createState() => _CategorySubViewState(); @@ -20,7 +20,7 @@ class _CategorySubViewState extends State { header: MaterialHeader(), firstRefresh: true, onRefresh: () async { - _models = await DisplayCategoryModel.fetchCategory(widget.id); + _models = await DisplayCategoryModel.fetchCategory(widget.model.id); setState(() {}); }, child: GridView.builder( @@ -29,7 +29,11 @@ class _CategorySubViewState extends State { ), itemBuilder: (context, index) { final model = _models[index]; - return CategorySubCard(model: model); + return CategorySubCard( + model: widget.model, + selectModel: model, + subModels: _models, + ); }, itemCount: _models.length, ), diff --git a/lib/ui/market/goods/goods_list_view.dart b/lib/ui/market/goods/goods_list_view.dart index d63d2e75..57b83713 100644 --- a/lib/ui/market/goods/goods_list_view.dart +++ b/lib/ui/market/goods/goods_list_view.dart @@ -1,84 +1,137 @@ +import 'package:aku_community/base/base_style.dart'; +import 'package:aku_community/constants/api.dart'; +import 'package:aku_community/model/common/img_model.dart'; +import 'package:aku_community/models/market/market_category_model.dart'; +import 'package:aku_community/ui/market/search/search_goods_page.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:get/get.dart'; -import 'package:waterfall_flow/waterfall_flow.dart'; - -import 'package:aku_community/ui/market/goods/goods_card.dart'; import 'package:aku_community/utils/headers.dart'; import 'package:aku_community/widget/bee_scaffold.dart'; class GoodsListView extends StatefulWidget { - GoodsListView({Key? key}) : super(key: key); + final MarketCategoryModel model; + final MarketCategoryModel? selectSubModel; + final List subModels; + GoodsListView({ + Key? key, + required this.model, + required this.subModels, + this.selectSubModel, + }) : super(key: key); @override _GoodsListViewState createState() => _GoodsListViewState(); } -class _GoodsListViewState extends State { +class _GoodsListViewState extends State + with TickerProviderStateMixin { + late MarketCategoryModel? _selectModel; + late TabController _tabController; + + @override + void initState() { + super.initState(); + int initIndex = 0; + //初始化已选项目 + if (widget.subModels.isNotEmpty) { + _selectModel = widget.subModels.first; + if (widget.selectSubModel != null) { + _selectModel = widget.selectSubModel; + initIndex = widget.subModels.indexOf(widget.selectSubModel!); + } + } + + _tabController = TabController( + length: widget.subModels.length, + vsync: this, + initialIndex: initIndex, + ); + } + + @override + void dispose() { + _tabController.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { return BeeScaffold( - title: 'TEST', + title: widget.model.name, actions: [ IconButton( icon: Icon(CupertinoIcons.search), - onPressed: () {}, + onPressed: () { + Get.to(() => SearchGoodsPage()); + }, ) ], appBarBottom: PreferredSize( child: SizedBox( height: 220.w, - child: ListView( + child: ListView.builder( padding: EdgeInsets.symmetric(horizontal: 18.w), scrollDirection: Axis.horizontal, - children: [ - GoodsSubTypeButton(), - ], + itemBuilder: (context, index) { + return GoodsSubTypeButton( + model: widget.subModels[index], + groupValue: _selectModel, + onTap: () { + _selectModel = widget.subModels[index]; + _tabController.animateTo(index); + setState(() {}); + }, + ); + }, + itemCount: widget.subModels.length, ), ), preferredSize: Size.fromHeight(220.w), ), - body: WaterfallFlow( - padding: EdgeInsets.all(32.w), - gridDelegate: SliverWaterfallFlowDelegateWithFixedCrossAxisCount( - crossAxisCount: 2, - mainAxisSpacing: 20.w, - crossAxisSpacing: 20.w, - ), - children: [ - GoodsCard(), - GoodsCard(), - GoodsCard(), - GoodsCard(), - GoodsCard(), - ], + body: TabBarView( + children: widget.subModels.map((e) => Text(e.name)).toList(), + controller: _tabController, ), ); } } class GoodsSubTypeButton extends StatelessWidget { - const GoodsSubTypeButton({Key? key}) : super(key: key); + final MarketCategoryModel model; + final MarketCategoryModel? groupValue; + final VoidCallback onTap; + const GoodsSubTypeButton({ + Key? key, + required this.model, + required this.groupValue, + required this.onTap, + }) : super(key: key); + + bool get same => model == groupValue; @override Widget build(BuildContext context) { return MaterialButton( minWidth: 136.w, padding: EdgeInsets.zero, - onPressed: () {}, + onPressed: onTap, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - Image.asset( - R.ASSETS_IMAGES_PLACEHOLDER_WEBP, + FadeInImage.assetNetwork( + image: API.image(ImgModel.first(model.imgList)), + placeholder: R.ASSETS_IMAGES_PLACEHOLDER_WEBP, width: 100.w, height: 100.w, ), 20.hb, Text( - '健康运动', + model.name, style: TextStyle( fontSize: 24.sp, + color: same ? kPrimaryColor : ktextPrimary, ), ), ], diff --git a/lib/ui/market/market_page.dart b/lib/ui/market/market_page.dart index 957d6f3c..9911acf6 100644 --- a/lib/ui/market/market_page.dart +++ b/lib/ui/market/market_page.dart @@ -8,7 +8,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; -import 'package:aku_community/ui/market/_market_data.dart'; import 'package:aku_community/ui/market/category/category_page.dart'; import 'package:aku_community/ui/market/search/search_goods_page.dart'; import 'package:aku_community/utils/headers.dart'; @@ -131,8 +130,8 @@ class _MarketPageState extends State }, body: TabBarView( children: [ - MockableMarketList(), - MockableMarketList(), + SizedBox(), + SizedBox(), ], controller: _tabController, ),