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.9KB

  1. import 'package:auto_size_text/auto_size_text.dart';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:farm_tpf/custom_model/ActionType.dart';
  4. import 'package:farm_tpf/data/repository/repository.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  6. import 'package:farm_tpf/presentation/screens/actions/environment_update/sc_edit_action_environment_update.dart';
  7. import 'package:farm_tpf/presentation/screens/actions/sc_action.dart';
  8. import 'package:farm_tpf/presentation/screens/plot_detail/bloc/cubit/plot_action_type_cubit.dart';
  9. import 'package:farm_tpf/utils/const_assets.dart';
  10. import 'package:farm_tpf/utils/const_color.dart';
  11. import 'package:farm_tpf/utils/const_common.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:flutter_bloc/flutter_bloc.dart';
  14. import 'package:get/get.dart';
  15. class PlotActionScreen extends StatefulWidget {
  16. final int? cropId;
  17. final String? cropCode;
  18. final int? cropType;
  19. PlotActionScreen({
  20. this.cropId,
  21. this.cropCode,
  22. this.cropType,
  23. });
  24. @override
  25. _PlotActionScreenState createState() => _PlotActionScreenState();
  26. }
  27. class _PlotActionScreenState extends State<PlotActionScreen> with AutomaticKeepAliveClientMixin {
  28. List<ActionType> actions = <ActionType>[];
  29. @override
  30. void initState() {
  31. super.initState();
  32. }
  33. @override
  34. void dispose() {
  35. super.dispose();
  36. }
  37. Widget _createActionButtons(ActionType actionType, BuildContext _context) {
  38. return GestureDetector(
  39. onTap: () {
  40. if (actionType.name == 'ACTIVE_TYPE_UPDATE_ENV') {
  41. Get.to(EditActionEnvironmentUpdate(
  42. cropId: widget.cropId ?? -1,
  43. activityId: -1,
  44. isEdit: false,
  45. ));
  46. } else {
  47. Get.to(ActionScreen(
  48. cropId: widget.cropId ?? -1,
  49. idAction: actionType.id ?? -1,
  50. title: actionType.description ?? '',
  51. activityType: actionType.name ?? '',
  52. activityId: -1,
  53. isEdit: false,
  54. ));
  55. }
  56. },
  57. child: Container(
  58. margin: EdgeInsets.all(8),
  59. padding: EdgeInsets.all(4),
  60. decoration: BoxDecoration(
  61. border: Border.all(color: Colors.grey, width: 0.1),
  62. color: Colors.white,
  63. borderRadius: BorderRadius.all(Radius.circular(8.0)),
  64. boxShadow: <BoxShadow>[
  65. BoxShadow(color: AppColors.GRAY1.withOpacity(0.2), offset: Offset(1.1, 1.1), blurRadius: 4.0),
  66. ],
  67. ),
  68. child: Column(
  69. mainAxisAlignment: MainAxisAlignment.center,
  70. crossAxisAlignment: CrossAxisAlignment.center,
  71. children: [
  72. Expanded(
  73. flex: 1,
  74. child: SizedBox(
  75. width: Get.width / 10,
  76. height: Get.height / 10,
  77. child: CachedNetworkImage(
  78. imageUrl: '${ConstCommon.baseImageUrl}${actionType.urlLogo}',
  79. placeholder: (context, url) => Icon(
  80. Icons.broken_image,
  81. color: Colors.grey[300],
  82. ),
  83. errorWidget: (context, url, error) => Image.asset(
  84. AppAssets.logo,
  85. ),
  86. ),
  87. ),
  88. ),
  89. Expanded(
  90. flex: 1,
  91. child: Align(
  92. alignment: Alignment.center,
  93. child: AutoSizeText(
  94. actionType.description ?? '',
  95. textAlign: TextAlign.center,
  96. style: TextStyle(
  97. fontWeight: FontWeight.bold,
  98. fontSize: 13,
  99. color: AppColors.BLACK2,
  100. ),
  101. maxLines: 2,
  102. ),
  103. ),
  104. ),
  105. ],
  106. ),
  107. ));
  108. }
  109. @override
  110. Widget build(BuildContext context) {
  111. return BlocProvider(
  112. create: (contex) => PlotActionTypeCubit(Repository())
  113. ..getAllActionTypes(
  114. widget.cropType ?? -1,
  115. ),
  116. child: BlocBuilder<PlotActionTypeCubit, PlotActionTypeState>(
  117. builder: (context, state) {
  118. if (state is PlotActionTypeLoading) {
  119. return LoadingListPage();
  120. } else if (state is PlotActionTypeSuccess) {
  121. return GridView.count(
  122. shrinkWrap: true,
  123. crossAxisCount: 3,
  124. children: state.actionTypes.map(
  125. (item) {
  126. return _createActionButtons(item, context);
  127. },
  128. ).toList());
  129. } else if (state is PlotActionTypeError) {
  130. return Center(child: Text(state.error));
  131. } else {
  132. //init
  133. return Container();
  134. }
  135. },
  136. ));
  137. }
  138. @override
  139. bool get wantKeepAlive => true;
  140. }