123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import 'package:flutter/material.dart';
- import 'package:shuqi/public.dart';
- import 'home_model.dart';
- import 'home_banner.dart';
- import 'home_menu.dart';
- import 'novel_normal_card.dart';
- import 'novel_four_grid_view.dart';
- import 'novel_first_hybird_card.dart';
- import 'novel_second_hybird_card.dart';
- enum HomeListType {
- excellent,
- male,
- female,
- cartoon,
- }
- class HomeListView extends StatefulWidget {
- final HomeListType type;
- HomeListView(this.type);
- @override
- State<StatefulWidget> createState() {
- return HomeListViewState();
- }
- }
- class HomeListViewState extends State<HomeListView> with AutomaticKeepAliveClientMixin {
- List<CarouselInfo> carouselInfos = [];
- int pageIndex = 1;
- List<HomeModule> modules = [];
- @override
- void initState() {
- super.initState();
- fetchData();
- }
- @override
- bool get wantKeepAlive => true;
- Future<void> fetchData() async {
- try {
- late var action;
- switch (this.widget.type) {
- case HomeListType.excellent:
- action = 'home_excellent';
- break;
- case HomeListType.female:
- action = 'home_female';
- break;
- case HomeListType.male:
- action = 'home_male';
- break;
- case HomeListType.cartoon:
- action = 'home_cartoon';
- break;
- default:
- break;
- }
- var responseJson = await Request.get(action: action);
- List moduleData = responseJson['module'];
- List<HomeModule> modules = [];
- moduleData.forEach((data) {
- modules.add(HomeModule.fromJson(data));
- });
- setState(() {
- this.modules = modules;
- this.carouselInfos = carouselInfos;
- });
- } catch (e) {
- Toast.show(e.toString());
- }
- }
- Widget bookCardWithInfo(HomeModule module) {
- Widget? card;
- switch (module.style) {
- case 1:
- card = NovelFourGridView(module);
- break;
- case 2:
- card = NovelSecondHybirdCard(module);
- break;
- case 3:
- card = NovelFirstHybirdCard(module);
- break;
- case 4:
- card = NovelNormalCard(module);
- break;
- }
- return card ?? SizedBox();
- }
- Widget buildModule(BuildContext context, HomeModule module) {
- if (module.carousels != null) {
- return HomeBanner(module.carousels!);
- } else if (module.menus != null) {
- return HomeMenu(module.menus!);
- } else if (module.books != null) {
- return bookCardWithInfo(module);
- }
- return Container();
- }
- @override
- Widget build(BuildContext context) {
- super.build(context);
- return Container(
- child: RefreshIndicator(
- onRefresh: fetchData,
- child: ListView.builder(
- itemCount: modules.length,
- itemBuilder: (BuildContext context, int index) {
- return buildModule(context, modules[index]);
- },
- ),
- ),
- );
- }
- }
|