home_list_view.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'package:flutter/material.dart';
  2. import 'package:shuqi/public.dart';
  3. import 'home_model.dart';
  4. import 'home_banner.dart';
  5. import 'home_menu.dart';
  6. import 'novel_normal_card.dart';
  7. import 'novel_four_grid_view.dart';
  8. import 'novel_first_hybird_card.dart';
  9. import 'novel_second_hybird_card.dart';
  10. enum HomeListType {
  11. excellent,
  12. male,
  13. female,
  14. cartoon,
  15. }
  16. class HomeListView extends StatefulWidget {
  17. final HomeListType type;
  18. HomeListView(this.type);
  19. @override
  20. State<StatefulWidget> createState() {
  21. return HomeListViewState();
  22. }
  23. }
  24. class HomeListViewState extends State<HomeListView> with AutomaticKeepAliveClientMixin {
  25. List<CarouselInfo> carouselInfos = [];
  26. int pageIndex = 1;
  27. List<HomeModule> modules = [];
  28. @override
  29. void initState() {
  30. super.initState();
  31. fetchData();
  32. }
  33. @override
  34. bool get wantKeepAlive => true;
  35. Future<void> fetchData() async {
  36. try {
  37. late var action;
  38. switch (this.widget.type) {
  39. case HomeListType.excellent:
  40. action = 'home_excellent';
  41. break;
  42. case HomeListType.female:
  43. action = 'home_female';
  44. break;
  45. case HomeListType.male:
  46. action = 'home_male';
  47. break;
  48. case HomeListType.cartoon:
  49. action = 'home_cartoon';
  50. break;
  51. default:
  52. break;
  53. }
  54. var responseJson = await Request.get(action: action);
  55. List moduleData = responseJson['module'];
  56. List<HomeModule> modules = [];
  57. moduleData.forEach((data) {
  58. modules.add(HomeModule.fromJson(data));
  59. });
  60. setState(() {
  61. this.modules = modules;
  62. this.carouselInfos = carouselInfos;
  63. });
  64. } catch (e) {
  65. Toast.show(e.toString());
  66. }
  67. }
  68. Widget bookCardWithInfo(HomeModule module) {
  69. Widget? card;
  70. switch (module.style) {
  71. case 1:
  72. card = NovelFourGridView(module);
  73. break;
  74. case 2:
  75. card = NovelSecondHybirdCard(module);
  76. break;
  77. case 3:
  78. card = NovelFirstHybirdCard(module);
  79. break;
  80. case 4:
  81. card = NovelNormalCard(module);
  82. break;
  83. }
  84. return card ?? SizedBox();
  85. }
  86. Widget buildModule(BuildContext context, HomeModule module) {
  87. if (module.carousels != null) {
  88. return HomeBanner(module.carousels!);
  89. } else if (module.menus != null) {
  90. return HomeMenu(module.menus!);
  91. } else if (module.books != null) {
  92. return bookCardWithInfo(module);
  93. }
  94. return Container();
  95. }
  96. @override
  97. Widget build(BuildContext context) {
  98. super.build(context);
  99. return Container(
  100. child: RefreshIndicator(
  101. onRefresh: fetchData,
  102. child: ListView.builder(
  103. itemCount: modules.length,
  104. itemBuilder: (BuildContext context, int index) {
  105. return buildModule(context, modules[index]);
  106. },
  107. ),
  108. ),
  109. );
  110. }
  111. }