my_estate_page.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import 'package:cs_resources/generated/l10n.dart';
  2. import 'package:cs_resources/theme/app_colors_theme.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:auto_route/auto_route.dart';
  5. import 'package:flutter_hooks/flutter_hooks.dart';
  6. import 'package:hooks_riverpod/hooks_riverpod.dart';
  7. import 'package:router/ext/auto_router_extensions.dart';
  8. import 'package:widgets/load_state_layout.dart';
  9. import 'package:widgets/my_appbar.dart';
  10. import 'package:widgets/widget_export.dart';
  11. import '../../router/page/profile_page_router.dart';
  12. import 'item_estate.dart';
  13. import 'item_estate_header.dart';
  14. import 'my_estate_view_model.dart';
  15. @RoutePage()
  16. class MyEstatePage extends HookConsumerWidget {
  17. const MyEstatePage({Key? key}) : super(key: key);
  18. //启动当前页面
  19. static void startInstance({BuildContext? context}) {
  20. if (context != null) {
  21. context.router.push(const MyEstatePageRoute());
  22. } else {
  23. appRouter.push(const MyEstatePageRoute());
  24. }
  25. }
  26. @override
  27. Widget build(BuildContext context, WidgetRef ref) {
  28. final viewModel = ref.watch(myEstateViewModelProvider.notifier);
  29. final state = ref.watch(myEstateViewModelProvider);
  30. useEffect(() {
  31. // 组件挂载时执行 - 执行接口请求
  32. Future.microtask(() => viewModel.fetchList());
  33. return () {
  34. // 组件卸载时执行
  35. };
  36. }, []);
  37. return Scaffold(
  38. appBar: MyAppBar.appBar(
  39. context,
  40. S.current.yy_home_accounts,
  41. backgroundColor: context.appColors.whiteBG,
  42. ),
  43. backgroundColor: context.appColors.backgroundDark,
  44. body: SizedBox(
  45. width: double.infinity,
  46. height: double.infinity,
  47. child: EasyRefresh(
  48. controller: viewModel.refreshController,
  49. onRefresh: viewModel.onRefresh,
  50. onLoad: viewModel.loadMore,
  51. child: LoadStateLayout(
  52. state: state.loadingState,
  53. errorMessage: state.errorMessage,
  54. errorRetry: () {
  55. viewModel.retryRequest();
  56. },
  57. successSliverWidget: [
  58. SliverList(
  59. delegate: SliverChildBuilderDelegate(
  60. (context, index) {
  61. return StickyHeader(
  62. header: EstateItemHeader(state.datas[index].groupId),
  63. content: Column(
  64. children: _buildGroup(state.datas[index].groupDatas!, index),
  65. ),
  66. );
  67. },
  68. childCount: state.datas.length,
  69. ))
  70. ],
  71. ),
  72. ),
  73. ),
  74. );
  75. }
  76. //当前组内的子数据
  77. List<Widget> _buildGroup(List<String> list, int index) {
  78. return list
  79. .asMap()
  80. .map((childIndex, item) {
  81. return MapEntry(childIndex, EstateItem(item: item, childIndex: childIndex));
  82. })
  83. .values
  84. .toList();
  85. }
  86. }