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.

147 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. activityId: -1,
  52. isEdit: false,
  53. ));
  54. }
  55. },
  56. child: Container(
  57. margin: EdgeInsets.all(8),
  58. padding: EdgeInsets.all(4),
  59. decoration: BoxDecoration(
  60. border: Border.all(color: Colors.grey, width: 0.1),
  61. color: Colors.white,
  62. borderRadius: BorderRadius.all(Radius.circular(8.0)),
  63. boxShadow: <BoxShadow>[
  64. BoxShadow(color: AppColors.GRAY1.withOpacity(0.2), offset: Offset(1.1, 1.1), blurRadius: 4.0),
  65. ],
  66. ),
  67. child: Column(
  68. mainAxisAlignment: MainAxisAlignment.center,
  69. crossAxisAlignment: CrossAxisAlignment.center,
  70. children: [
  71. Expanded(
  72. flex: 1,
  73. child: SizedBox(
  74. width: Get.width / 10,
  75. height: Get.height / 10,
  76. child: CachedNetworkImage(
  77. imageUrl: '${ConstCommon.baseImageUrl}${actionType.urlLogo}',
  78. placeholder: (context, url) => Icon(
  79. Icons.broken_image,
  80. color: Colors.grey[300],
  81. ),
  82. errorWidget: (context, url, error) => Image.asset(
  83. AppAssets.logoWithSlogan,
  84. ),
  85. ),
  86. ),
  87. ),
  88. Expanded(
  89. flex: 1,
  90. child: Align(
  91. alignment: Alignment.center,
  92. child: AutoSizeText(
  93. actionType.name ?? '',
  94. textAlign: TextAlign.center,
  95. style: TextStyle(
  96. fontWeight: FontWeight.bold,
  97. fontSize: 13,
  98. color: AppColors.BLACK2,
  99. ),
  100. maxLines: 2,
  101. ),
  102. ),
  103. ),
  104. ],
  105. ),
  106. ));
  107. }
  108. @override
  109. Widget build(BuildContext context) {
  110. return BlocProvider(
  111. create: (contex) => PlotActionTypeCubit(Repository())
  112. ..getAllActionTypes(
  113. widget.cropType ?? -1,
  114. ),
  115. child: BlocBuilder<PlotActionTypeCubit, PlotActionTypeState>(
  116. builder: (context, state) {
  117. if (state is PlotActionTypeLoading) {
  118. return LoadingListPage();
  119. } else if (state is PlotActionTypeSuccess) {
  120. return GridView.count(
  121. shrinkWrap: true,
  122. crossAxisCount: 3,
  123. children: state.actionTypes.map(
  124. (item) {
  125. return _createActionButtons(item, context);
  126. },
  127. ).toList());
  128. } else if (state is PlotActionTypeError) {
  129. return Center(child: Text(state.error));
  130. } else {
  131. //init
  132. return Container();
  133. }
  134. },
  135. ));
  136. }
  137. @override
  138. bool get wantKeepAlive => true;
  139. }