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.

166 lines
5.4KB

  1. import 'package:farm_tpf/data/repository/repository.dart';
  2. import 'package:farm_tpf/presentation/custom_widgets/button/button_2_icon.dart';
  3. import 'package:farm_tpf/presentation/custom_widgets/button/second_button.dart';
  4. import 'package:farm_tpf/presentation/screens/codes/code_detail_page.dart';
  5. import 'package:farm_tpf/presentation/screens/codes/create_stamp_page.dart';
  6. import 'package:farm_tpf/presentation/screens/codes/models/stamp.dart';
  7. import 'package:farm_tpf/presentation/screens/codes/widgets/item_code.dart';
  8. import 'package:farm_tpf/themes/app_colors.dart';
  9. import 'package:flutter/cupertino.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:flutter_bloc/flutter_bloc.dart';
  12. import 'package:get/get.dart';
  13. import '../../../themes/styles_text.dart';
  14. import '../../../utils/const_string.dart';
  15. import '../../custom_widgets/bottom_loader.dart';
  16. import '../../custom_widgets/loading_list_page.dart';
  17. import '../plot/widget_search.dart';
  18. import 'bloc/stamp_bloc.dart';
  19. class CodePage extends StatefulWidget {
  20. const CodePage({super.key});
  21. @override
  22. State<CodePage> createState() => _CodePageState();
  23. }
  24. class _CodePageState extends State<CodePage> {
  25. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  26. StampBloc bloc = StampBloc(Repository());
  27. final _scrollController = ScrollController();
  28. final _scrollThreshold = 250.0;
  29. @override
  30. void initState() {
  31. bloc.add(DataFetched());
  32. _scrollController.addListener(() {
  33. final maxScroll = _scrollController.position.maxScrollExtent;
  34. final currentScroll = _scrollController.position.pixels;
  35. if (maxScroll - currentScroll < _scrollThreshold) {
  36. bloc.add(DataFetched());
  37. }
  38. });
  39. super.initState();
  40. }
  41. @override
  42. void dispose() {
  43. _scrollController.dispose();
  44. super.dispose();
  45. }
  46. @override
  47. Widget build(BuildContext context) {
  48. return Scaffold(
  49. backgroundColor: Colors.white,
  50. key: _scaffoldKey,
  51. body: SafeArea(
  52. child: Column(
  53. crossAxisAlignment: CrossAxisAlignment.start,
  54. children: <Widget>[
  55. SizedBox(
  56. height: 8,
  57. ),
  58. Container(
  59. padding: EdgeInsets.all(8),
  60. color: Colors.white,
  61. child: Text(
  62. 'Danh sách tem',
  63. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
  64. ),
  65. ),
  66. WidgetSearch(),
  67. Row(
  68. children: [
  69. Button2Icon(
  70. leftIcon: CupertinoIcons.arrow_up_arrow_down,
  71. rightIcon: CupertinoIcons.chevron_down,
  72. title: 'Sort',
  73. onPressed: () {},
  74. ),
  75. Button2Icon(
  76. leftIcon: CupertinoIcons.slider_horizontal_3,
  77. title: 'Filter',
  78. rightWidget: Container(
  79. width: 16,
  80. height: 16,
  81. decoration: BoxDecoration(
  82. borderRadius: BorderRadius.circular(12),
  83. color: Colors.blue,
  84. ),
  85. child: Center(
  86. child: Text(
  87. '1',
  88. style: StylesText.caption6.copyWith(color: Colors.white),
  89. ),
  90. ),
  91. ),
  92. onPressed: () {},
  93. ),
  94. const Spacer(),
  95. SecondButton(
  96. onPressed: () {
  97. Get.to(() => CreateStampPage())?.then((value) {
  98. if (value != null) {
  99. bloc.add(OnRefresh());
  100. }
  101. });
  102. },
  103. title: 'Tạo tem',
  104. leftIcon: CupertinoIcons.add,
  105. color: AppColors.primary1,
  106. textColor: Colors.white,
  107. borderColor: AppColors.primary1,
  108. ),
  109. ],
  110. ),
  111. Expanded(
  112. child: mainBody(),
  113. ),
  114. ],
  115. ),
  116. ),
  117. );
  118. }
  119. Widget mainBody() {
  120. return BlocBuilder<StampBloc, StampState>(
  121. bloc: bloc,
  122. builder: (context, state) {
  123. if (state is StampFailure) {
  124. return Center(child: Text(state.errorString));
  125. }
  126. if (state is StampSuccess) {
  127. if ((state.items ?? []).isEmpty) {
  128. return Center(child: Text(label_list_empty));
  129. }
  130. return RefreshIndicator(
  131. child: ListView.builder(
  132. physics: AlwaysScrollableScrollPhysics(),
  133. itemBuilder: (BuildContext context, int index) {
  134. return index >= (state.items ?? []).length
  135. ? BottomLoader()
  136. : ItemCode(
  137. item: state.items?[index],
  138. onPressed: () {
  139. Get.to(() => CodeDetailPage());
  140. },
  141. );
  142. },
  143. itemCount: (state.hasReachedMax ?? false) ? (state.items ?? []).length : (state.items ?? []).length + 1,
  144. controller: _scrollController,
  145. ),
  146. onRefresh: () async {
  147. bloc.add(OnRefresh());
  148. });
  149. }
  150. return Center(
  151. child: LoadingListPage(),
  152. );
  153. },
  154. );
  155. }
  156. }