You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.8 KiB

4 years ago
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
4 years ago
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
3 years ago
import 'package:flutter_easyloading/flutter_easyloading.dart';
4 years ago
import 'package:flutter_html/flutter_html.dart';
4 years ago
import 'package:flutter_html/image_render.dart';
4 years ago
import 'package:huixiang/generated/l10n.dart';
4 years ago
import 'package:huixiang/main.dart';
4 years ago
import 'package:huixiang/retrofit/data/banner.dart';
4 years ago
import 'package:huixiang/retrofit/data/base_data.dart';
3 years ago
import 'package:huixiang/retrofit/data/brand.dart';
4 years ago
import 'package:huixiang/retrofit/data/brand_data.dart';
3 years ago
import 'package:huixiang/retrofit/data/founder.dart';
3 years ago
import 'package:huixiang/retrofit/data/page.dart';
4 years ago
import 'package:huixiang/retrofit/retrofit_api.dart';
4 years ago
import 'package:huixiang/utils/event_type.dart';
4 years ago
import 'package:huixiang/view_widget/classic_header.dart';
4 years ago
import 'package:flutter_screenutil/flutter_screenutil.dart';
4 years ago
import 'package:huixiang/view_widget/store_title_tab.dart';
4 years ago
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
4 years ago
3 years ago
import 'huixiang_view/huixiang_banner.dart';
import 'huixiang_view/origin_info.dart';
4 years ago
class BrandPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _BrandPage();
}
}
4 years ago
class _BrandPage extends State<BrandPage>
with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
3 years ago
final ScrollController scrollController = ScrollController();
final RefreshController refreshController = RefreshController();
4 years ago
4 years ago
ApiService apiService;
3 years ago
List<Brand> brands = [];
4 years ago
BrandData brandData;
3 years ago
List<GlobalKey> globaKeys = [];
4 years ago
List<BannerData> bannerData = [];
3 years ago
Founder founder;
3 years ago
3 years ago
queryHome() async {
EasyLoading.show(status: S.current.zhengzaijiazai,maskType: EasyLoadingMaskType.black);
3 years ago
final SharedPreferences value = await SharedPreferences.getInstance();
3 years ago
apiService = ApiService(
Dio(),
context: context,
token: value.getString('token'),
);
3 years ago
3 years ago
BaseData<BrandData> baseData =
await apiService.queryHome().catchError((error) {
4 years ago
refreshController.refreshFailed();
});
3 years ago
if (baseData != null && baseData.isSuccess) {
brandData = baseData.data;
} else {
refreshController.refreshFailed();
}
3 years ago
BaseData<PageInfo<BannerData>> banner = await apiService.queryBanner({
4 years ago
"model": {"type": "BRAND_APP"},
4 years ago
}).catchError((error) {
refreshController.refreshFailed();
});
bannerData.clear();
3 years ago
bannerData.addAll(banner.data.records);
4 years ago
3 years ago
BaseData brand = await apiService.queryHomeBrand().catchError((error) {
3 years ago
refreshController.refreshFailed();
});
if (brand != null && brand.isSuccess) {
brands.clear();
globaKeys.clear();
brands.addAll((brand.data["brandList"] as List<dynamic>)
.map((e) => Brand.fromJson(e))
.toList());
3 years ago
founder = Founder.fromJson(brand.data["founder"]);
3 years ago
brands.forEach((element) {
globaKeys.add(GlobalKey());
});
} else {
refreshController.refreshFailed();
}
3 years ago
EasyLoading.dismiss();
3 years ago
refreshController.refreshCompleted();
3 years ago
if (mounted) setState(() {});
3 years ago
}
4 years ago
@override
void initState() {
super.initState();
4 years ago
eventBus.on<EventType>().listen((event) {
4 years ago
print("object: BrandPage");
if (event.type < 3) {
setState(() {});
}
4 years ago
});
3 years ago
queryHome();
4 years ago
}
@override
Widget build(BuildContext context) {
4 years ago
super.build(context);
3 years ago
return Stack(
children: [
Positioned(
child: Container(
child: SmartRefresher(
controller: refreshController,
enablePullDown: true,
enablePullUp: false,
header: MyHeader(),
physics: ClampingScrollPhysics(),
onRefresh: queryHome,
scrollController: scrollController,
3 years ago
child: Container(
3 years ago
child: SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
3 years ago
child: Container(
3 years ago
color: Color(0xFFF7F7F7),
margin: EdgeInsets.only(top: 16.h),
child: Column(
children: homeChildItem(),
4 years ago
),
4 years ago
),
),
),
),
3 years ago
),
bottom: 0,
top: 0,
left: 0,
right: 0,
4 years ago
),
3 years ago
if (brands != null && brands.length > 0)
Positioned(
child: Container(
color: Colors.white,
child: StoreTitleTab(
brands,
globaKeys,
scrollController,
isScroll: true,
),
),
top: 0,
left: 0,
right: 0,
),
],
4 years ago
);
}
4 years ago
List<Widget> homeChildItem() {
4 years ago
var widgets = <Widget>[
3 years ago
///回乡banner
HuiXiangBanner(bannerData),
3 years ago
3 years ago
///公司信息
3 years ago
OriginInfo(founder),
3 years ago
4 years ago
StoreTitleTab(
3 years ago
brands,
globaKeys,
scrollController,
4 years ago
isScroll: false,
3 years ago
),
4 years ago
];
3 years ago
if (brands == null) return widgets;
brands.forEach((value) {
3 years ago
widgets.add(
Container(
key: globaKeys[brands.indexOf(value)],
child: Container(
color: Colors.white,
child: Html(
data: value.content,
customImageRenders: {
assetUriMatcher(): assetImageRender(),
networkSourceMatcher(extension: "svg"): svgNetworkImageRender(),
networkSourceMatcher(): networkImageRender(
loadingWidget: () {
return Container();
},
),
},
),
4 years ago
),
4 years ago
),
3 years ago
);
4 years ago
});
return widgets;
}
4 years ago
@override
bool get wantKeepAlive => true;
4 years ago
}