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.

136 lines
4.1KB

  1. import 'package:dio/dio.dart';
  2. import 'package:farm_tpf/data/repository/repository.dart';
  3. import 'package:farm_tpf/models/Plot.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  6. import 'package:farm_tpf/utils/bloc/infinity_scroll_bloc.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. import 'package:farm_tpf/utils/const_string.dart';
  10. import 'package:font_awesome_flutter/font_awesome_flutter.dart';
  11. class PlotListScreen extends StatefulWidget {
  12. @override
  13. _PlotListScreenState createState() => _PlotListScreenState();
  14. }
  15. class _PlotListScreenState extends State<PlotListScreen> {
  16. @override
  17. Widget build(BuildContext context) {
  18. return BlocProvider(
  19. create: (context) =>
  20. InfinityScrollBloc(repository: Repository())..add(DataFetched()),
  21. child: HoldInfinityWidget(),
  22. );
  23. }
  24. }
  25. class HoldInfinityWidget extends StatelessWidget {
  26. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  27. @override
  28. Widget build(BuildContext context) {
  29. return Scaffold(
  30. key: _scaffoldKey,
  31. appBar: AppBar(
  32. centerTitle: true,
  33. title: Text("Danh sách lô"),
  34. actions: <Widget>[
  35. IconButton(icon: Icon(FontAwesomeIcons.qrcode), onPressed: () {})
  36. ],
  37. ),
  38. body: InfinityView());
  39. }
  40. }
  41. class InfinityView extends StatefulWidget {
  42. @override
  43. _InfinityViewState createState() => _InfinityViewState();
  44. }
  45. class _InfinityViewState extends State<InfinityView> {
  46. final _scrollController = ScrollController();
  47. final _scrollThreshold = 250.0;
  48. InfinityScrollBloc _infinityScrollBloc;
  49. @override
  50. void initState() {
  51. _scrollController.addListener(() {
  52. final maxScroll = _scrollController.position.maxScrollExtent;
  53. final currentScroll = _scrollController.position.pixels;
  54. if (maxScroll - currentScroll < _scrollThreshold) {
  55. _infinityScrollBloc.add(DataFetched());
  56. }
  57. });
  58. _infinityScrollBloc = BlocProvider.of<InfinityScrollBloc>(context);
  59. super.initState();
  60. }
  61. @override
  62. Widget build(BuildContext context) {
  63. return BlocBuilder<InfinityScrollBloc, InfinityScrollState>(
  64. builder: (context, state) {
  65. if (state is InfinityScrollFailure) {
  66. return Center(child: Text(label_error_get_data));
  67. }
  68. if (state is InfinityScrollSuccess) {
  69. if (state.items.isEmpty) {
  70. return Center(child: Text(label_list_empty));
  71. }
  72. return RefreshIndicator(
  73. child: ListView.builder(
  74. itemBuilder: (BuildContext context, int index) {
  75. return index >= state.items.length
  76. ? BottomLoader()
  77. : ItemInfinityWidget(item: state.items[index]);
  78. },
  79. itemCount: state.hasReachedMax
  80. ? state.items.length
  81. : state.items.length + 1,
  82. controller: _scrollController,
  83. ),
  84. onRefresh: () async {
  85. _infinityScrollBloc.add(OnRefresh());
  86. });
  87. }
  88. return Center(
  89. child: LoadingListPage(),
  90. );
  91. },
  92. );
  93. }
  94. @override
  95. void dispose() {
  96. _scrollController.dispose();
  97. super.dispose();
  98. }
  99. }
  100. class ItemInfinityWidget extends StatelessWidget {
  101. final Plot item;
  102. const ItemInfinityWidget({Key key, @required this.item}) : super(key: key);
  103. @override
  104. Widget build(BuildContext context) {
  105. return GestureDetector(
  106. child: Card(
  107. color: item.id % 3 == 0 ? Colors.white : Colors.greenAccent[100],
  108. child: Container(
  109. padding: EdgeInsets.all(8.0),
  110. child: Column(
  111. crossAxisAlignment: CrossAxisAlignment.start,
  112. children: <Widget>[
  113. Text("Ngày giờ: " + item.activityExecuteDate.toString()),
  114. SizedBox(
  115. height: 8.0,
  116. ),
  117. Text("Thời gian: " + item.id.toString() + " giây"),
  118. ],
  119. ),
  120. ),
  121. ),
  122. onTap: () {});
  123. }
  124. }