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.

137 lines
4.8KB

  1. import 'dart:async';
  2. import 'package:farm_tpf/custom_model/EnvironmentParameter.dart';
  3. import 'package:farm_tpf/data/repository/repository.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  7. import 'package:farm_tpf/utils/const_color.dart';
  8. import 'package:farm_tpf/utils/const_string.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter_bloc/flutter_bloc.dart';
  11. import 'package:farm_tpf/utils/formatter.dart';
  12. import 'bloc/plot_parameter_bloc.dart';
  13. class PlotParameterScreen extends StatefulWidget {
  14. final int cropId;
  15. final bool isShowAppbar;
  16. PlotParameterScreen({required this.cropId, this.isShowAppbar = false});
  17. @override
  18. _PlotParameterScreenState createState() => _PlotParameterScreenState();
  19. }
  20. class _PlotParameterScreenState extends State<PlotParameterScreen> with AutomaticKeepAliveClientMixin {
  21. var plotParameterBloc = PlotParameterBloc(repository: Repository());
  22. final _scrollController = ScrollController();
  23. final _scrollThreshold = 250.0;
  24. @override
  25. void initState() {
  26. _scrollController.addListener(() {
  27. final maxScroll = _scrollController.position.maxScrollExtent;
  28. final currentScroll = _scrollController.position.pixels;
  29. if (maxScroll - currentScroll < _scrollThreshold) {
  30. plotParameterBloc.add(DataFetched(cropId: widget.cropId));
  31. }
  32. });
  33. plotParameterBloc.add(DataFetched(cropId: widget.cropId));
  34. super.initState();
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. return Scaffold(
  39. backgroundColor: Colors.white,
  40. appBar: appbar(widget.isShowAppbar),
  41. body: SafeArea(
  42. child: Column(
  43. children: <Widget>[
  44. Expanded(
  45. child: BlocBuilder<PlotParameterBloc, PlotParameterState>(
  46. cubit: plotParameterBloc,
  47. builder: (context, state) {
  48. if (state is PlotParameterFailure) {
  49. return Center(child: Text(state.errorString));
  50. }
  51. if (state is PlotParameterSuccess) {
  52. if (state.items.isEmpty) {
  53. return const Center(child: Text(label_list_empty));
  54. }
  55. return RefreshIndicator(
  56. child: GridView.builder(
  57. gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: 3 / 2),
  58. physics: const AlwaysScrollableScrollPhysics(),
  59. itemBuilder: (BuildContext context, int index) {
  60. return index >= state.items.length ? BottomLoader() : ItemInfinityWidget(item: state.items[index]);
  61. },
  62. itemCount: state.hasReachedMax ? state.items.length : state.items.length + 1,
  63. controller: _scrollController,
  64. ),
  65. onRefresh: () async {
  66. plotParameterBloc.add(OnRefresh(cropId: widget.cropId));
  67. });
  68. }
  69. return Center(
  70. child: LoadingListPage(),
  71. );
  72. },
  73. ))
  74. ],
  75. ),
  76. ),
  77. );
  78. }
  79. PreferredSizeWidget? appbar(bool isShowAppbar) {
  80. if (isShowAppbar) {
  81. return AppBarWidget();
  82. }
  83. }
  84. @override
  85. void dispose() {
  86. _scrollController.dispose();
  87. plotParameterBloc.close();
  88. super.dispose();
  89. }
  90. @override
  91. bool get wantKeepAlive => true;
  92. }
  93. class ItemInfinityWidget extends StatelessWidget {
  94. final EnvironmentParameter item;
  95. const ItemInfinityWidget({Key? key, required this.item}) : super(key: key);
  96. @override
  97. Widget build(BuildContext context) {
  98. return Container(
  99. margin: const EdgeInsets.all(8),
  100. decoration: BoxDecoration(
  101. color: Colors.white,
  102. border: Border.all(color: Colors.grey, width: 0.35),
  103. borderRadius: BorderRadius.circular(8),
  104. boxShadow: [BoxShadow(color: Colors.grey, blurRadius: 4, offset: const Offset(0, 3))]),
  105. padding: const EdgeInsets.all(8),
  106. child: Column(
  107. crossAxisAlignment: CrossAxisAlignment.start,
  108. children: [
  109. Text(
  110. item.index?.formatNumtoStringDecimal() ?? '',
  111. style: TextStyle(color: (item.status ?? false) ? Colors.red : AppColors.YELLOW, fontWeight: FontWeight.bold, fontSize: 22),
  112. ),
  113. Flexible(
  114. child: Text(
  115. "${item.name ?? ''}",
  116. maxLines: 2,
  117. style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
  118. ),
  119. ),
  120. Text(item.executeDate?.format_DDMMYY_HHmm() ?? '', style: const TextStyle(fontSize: 13, color: Colors.grey))
  121. ],
  122. ),
  123. );
  124. }
  125. }