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.

194 lines
5.4 KiB

4 years ago
import 'package:dio/dio.dart';
4 years ago
import 'package:flutter/material.dart';
4 years ago
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:huixiang/data/banner.dart';
import 'package:huixiang/data/base_data.dart';
import 'package:huixiang/data/brand.dart';
import 'package:huixiang/data/brand_data.dart';
import 'package:huixiang/data/founder.dart';
import 'package:huixiang/data/page.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/main.dart';
4 years ago
import 'package:huixiang/retrofit/retrofit_api.dart';
4 years ago
import 'package:huixiang/utils/event_type.dart';
import 'package:huixiang/utils/shared_preference.dart';
4 years ago
import 'package:huixiang/view_widget/classic_header.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();
}
}
class _BrandPage extends State<BrandPage> with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
3 years ago
final ScrollController scrollController = ScrollController();
final RefreshController refreshController = RefreshController();
4 years ago
ApiService? apiService;
4 years ago
List<Brand> brands = [];
BrandData? brandData;
4 years ago
List<GlobalKey> globaKeys = [];
4 years ago
List<BannerData> bannerData = [];
Founder? founder;
4 years ago
3 years ago
queryHome() async {
SmartDialog.showLoading(
msg: S.current.zhengzaijiazai,
);
3 years ago
3 years ago
apiService = ApiService(
Dio(),
context: context,
token: SharedInstance.instance.token,
3 years ago
);
3 years ago
BaseData<BrandData>? baseData =
await apiService?.queryHome().catchError((error) {
4 years ago
refreshController.refreshFailed();
return BaseData<BrandData>()..isSuccess = false;
4 years ago
});
if (baseData?.isSuccess ?? false) {
brandData = baseData!.data;
3 years ago
} else {
refreshController.refreshFailed();
}
BaseData<PageInfo<BannerData>>? banner = await apiService?.queryBanner({
4 years ago
"model": {"type": "BRAND_APP"},
4 years ago
}).catchError((error) {
refreshController.refreshFailed();
return BaseData<PageInfo<BannerData>>()..isSuccess = false;
4 years ago
});
bannerData.clear();
bannerData.addAll(banner?.data?.records ?? []);
4 years ago
BaseData? brand = await apiService?.queryHomeBrand().catchError((error) {
4 years ago
refreshController.refreshFailed();
return BaseData()..isSuccess = false;
4 years ago
});
if (brand?.isSuccess ?? false) {
4 years ago
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"]);
4 years ago
brands.forEach((element) {
globaKeys.add(GlobalKey());
});
} else {
refreshController.refreshFailed();
}
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,
4 years ago
child: Container(
3 years ago
child: SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
4 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
),
if (brands.length > 0)
3 years ago
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(
4 years ago
brands,
globaKeys,
scrollController,
4 years ago
isScroll: false,
3 years ago
),
4 years ago
];
4 years ago
brands.forEach((value) {
3 years ago
widgets.add(
Container(
key: globaKeys[brands.indexOf(value)],
child: Container(
color: Colors.white,
child: Html(
data: value.content,
),
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
}