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.

135 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: widget.isShowAppbar ? AppBarWidget() : null,
  41. // PreferredSize(
  42. // preferredSize: Size(0, 0),
  43. // child: SizedBox(),
  44. // ),
  45. body: SafeArea(
  46. child: Column(
  47. children: <Widget>[
  48. Expanded(
  49. child: BlocBuilder<PlotParameterBloc, PlotParameterState>(
  50. bloc: plotParameterBloc,
  51. builder: (context, state) {
  52. if (state is PlotParameterFailure) {
  53. return Center(child: Text(state.errorString ?? ''));
  54. }
  55. if (state is PlotParameterSuccess) {
  56. if ((state.items ?? []).isEmpty) {
  57. return Center(child: Text(label_list_empty));
  58. }
  59. return RefreshIndicator(
  60. child: GridView.builder(
  61. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: 3 / 2),
  62. physics: AlwaysScrollableScrollPhysics(),
  63. itemBuilder: (BuildContext context, int index) {
  64. return index >= (state.items ?? []).length ? BottomLoader() : ItemInfinityWidget(item: state.items?[index]);
  65. },
  66. itemCount: (state.hasReachedMax ?? false) ? (state.items ?? []).length : (state.items ?? []).length + 1,
  67. controller: _scrollController,
  68. ),
  69. onRefresh: () async {
  70. plotParameterBloc.add(OnRefresh(cropId: widget.cropId));
  71. });
  72. }
  73. return Center(
  74. child: LoadingListPage(),
  75. );
  76. },
  77. ))
  78. ],
  79. ),
  80. ),
  81. );
  82. }
  83. @override
  84. void dispose() {
  85. _scrollController.dispose();
  86. plotParameterBloc.close();
  87. super.dispose();
  88. }
  89. @override
  90. bool get wantKeepAlive => true;
  91. }
  92. class ItemInfinityWidget extends StatelessWidget {
  93. final EnvironmentParameter item;
  94. const ItemInfinityWidget({Key? key, required this.item}) : super(key: key);
  95. @override
  96. Widget build(BuildContext context) {
  97. return Container(
  98. margin: EdgeInsets.all(8),
  99. decoration: BoxDecoration(
  100. color: Colors.white,
  101. border: Border.all(color: Colors.grey, width: 0.35),
  102. borderRadius: BorderRadius.circular(8),
  103. boxShadow: [BoxShadow(color: Colors.grey, blurRadius: 4, offset: Offset(0, 3))]),
  104. padding: EdgeInsets.all(8),
  105. child: Column(
  106. crossAxisAlignment: CrossAxisAlignment.start,
  107. children: [
  108. Text(
  109. item.index?.formatNumtoStringDecimal() ?? '',
  110. style: TextStyle(color: (item.status ?? false) ? Colors.red : AppColors.YELLOW, fontWeight: FontWeight.bold, fontSize: 22),
  111. ),
  112. Flexible(
  113. child: Text(
  114. "${item.name ?? ''}",
  115. maxLines: 2,
  116. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
  117. ),
  118. ),
  119. Text(item.executeDate?.fromUtcToLocal() ?? '', style: TextStyle(fontSize: 13, color: Colors.grey))
  120. ],
  121. ),
  122. );
  123. }
  124. }