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.

148 lines
4.7KB

  1. import 'package:farm_tpf/data/repository/repository.dart';
  2. import 'package:farm_tpf/models/index.dart';
  3. import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  5. import 'package:farm_tpf/utils/bloc/infinity_scroll_bloc.dart';
  6. import 'package:farm_tpf/utils/const_string.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. class ResourceHelperScreen extends StatefulWidget {
  10. final String resourceName;
  11. final int selectedPlotId;
  12. ResourceHelperScreen(
  13. {@required this.resourceName, @required this.selectedPlotId});
  14. @override
  15. _ResourceHelperScreenState createState() => _ResourceHelperScreenState();
  16. }
  17. class _ResourceHelperScreenState extends State<ResourceHelperScreen> {
  18. @override
  19. Widget build(BuildContext context) {
  20. return BlocProvider(
  21. create: (context) =>
  22. InfinityScrollBloc(repository: Repository())..add(DataFetched()),
  23. child: HoldInfinityWidget(
  24. selectedPlotId: widget.selectedPlotId,
  25. ),
  26. );
  27. }
  28. }
  29. class HoldInfinityWidget extends StatelessWidget {
  30. final int selectedPlotId;
  31. HoldInfinityWidget({@required this.selectedPlotId});
  32. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  33. @override
  34. Widget build(BuildContext context) {
  35. return Scaffold(
  36. key: _scaffoldKey,
  37. appBar: AppBar(title: Text("Chọn danh sách")),
  38. body: InfinityView(
  39. selectedPlotId: selectedPlotId,
  40. ));
  41. }
  42. }
  43. class InfinityView extends StatefulWidget {
  44. final int selectedPlotId;
  45. InfinityView({@required this.selectedPlotId});
  46. @override
  47. _InfinityViewState createState() => _InfinityViewState();
  48. }
  49. class _InfinityViewState extends State<InfinityView> {
  50. final _scrollController = ScrollController();
  51. final _scrollThreshold = 250.0;
  52. InfinityScrollBloc _infinityScrollBloc;
  53. @override
  54. void initState() {
  55. _scrollController.addListener(() {
  56. final maxScroll = _scrollController.position.maxScrollExtent;
  57. final currentScroll = _scrollController.position.pixels;
  58. if (maxScroll - currentScroll < _scrollThreshold) {
  59. _infinityScrollBloc.add(DataFetched());
  60. }
  61. });
  62. _infinityScrollBloc = BlocProvider.of<InfinityScrollBloc>(context);
  63. super.initState();
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return BlocBuilder<InfinityScrollBloc, InfinityScrollState>(
  68. builder: (context, state) {
  69. if (state is InfinityScrollFailure) {
  70. return Center(child: Text(label_error_get_data));
  71. }
  72. if (state is InfinityScrollSuccess) {
  73. if (state.items.isEmpty) {
  74. return Center(child: Text(label_list_empty));
  75. }
  76. Plot selectedPlot;
  77. List<Plot> plots = state.items.map((e) => e as Plot).toList();
  78. plots.forEach((plot) {
  79. plot.id == widget.selectedPlotId ? selectedPlot = plot : Plot();
  80. });
  81. return RefreshIndicator(
  82. child: ListView.builder(
  83. itemBuilder: (BuildContext context, int index) {
  84. return index >= state.items.length
  85. ? BottomLoader()
  86. : ItemInfinityWidget(
  87. item: state.items[index],
  88. selectedPlot: selectedPlot,
  89. );
  90. },
  91. itemCount: state.hasReachedMax
  92. ? state.items.length
  93. : state.items.length + 1,
  94. controller: _scrollController,
  95. ),
  96. onRefresh: () async {
  97. _infinityScrollBloc.add(OnRefresh());
  98. });
  99. }
  100. return Center(
  101. child: LoadingListPage(),
  102. );
  103. },
  104. );
  105. }
  106. @override
  107. void dispose() {
  108. _scrollController.dispose();
  109. super.dispose();
  110. }
  111. }
  112. class ItemInfinityWidget extends StatelessWidget {
  113. final Plot item;
  114. final Plot selectedPlot;
  115. const ItemInfinityWidget({Key key, @required this.item, this.selectedPlot})
  116. : super(key: key);
  117. @override
  118. Widget build(BuildContext context) {
  119. return GestureDetector(
  120. child: Card(
  121. color: item.id % 3 == 0 ? Colors.white : Colors.greenAccent[100],
  122. child: Material(
  123. child: RadioListTile(
  124. title: Text(item.activityExecuteDate.toString()),
  125. subtitle: Text(item.id.toString()),
  126. value: item,
  127. groupValue: selectedPlot,
  128. onChanged: (Plot value) {
  129. print("selected value: ${value.id}");
  130. Navigator.of(context).pop(value.id);
  131. }),
  132. )),
  133. onTap: () {});
  134. }
  135. }