|
- import 'package:farm_tpf/custom_model/CropPlot.dart';
- import 'package:farm_tpf/data/repository/repository.dart';
- import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
- import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
- import 'package:farm_tpf/presentation/screens/actions/crop_status/sc_edit_action_crop_status.dart';
- import 'package:farm_tpf/presentation/screens/actions/disease/sc_edit_action_disease.dart';
- import 'package:farm_tpf/presentation/screens/actions/dung/sc_edit_action_dung.dart';
- import 'package:farm_tpf/presentation/screens/actions/end/sc_edit_action_end.dart';
- import 'package:farm_tpf/presentation/screens/actions/environment_update/sc_edit_action_environment_update.dart';
- import 'package:farm_tpf/presentation/screens/actions/harvest/sc_edit_action_harvest.dart';
- import 'package:farm_tpf/presentation/screens/actions/harvest_process/sc_edit_action_harvest_process.dart';
- import 'package:farm_tpf/presentation/screens/actions/nursery/sc_edit_action_nursery.dart';
- import 'package:farm_tpf/presentation/screens/actions/other/sc_edit_action_other.dart';
- import 'package:farm_tpf/presentation/screens/actions/packing/sc_edit_action_packing.dart';
- import 'package:farm_tpf/presentation/screens/actions/plant/sc_edit_action_plant.dart';
- import 'package:farm_tpf/presentation/screens/actions/sell/sc_edit_action_sell.dart';
- import 'package:farm_tpf/presentation/screens/actions/spraying/sc_edit_action_spraying.dart';
- import 'package:farm_tpf/presentation/screens/actions/use_water/sc_edit_action_user_water.dart';
- import 'package:farm_tpf/presentation/screens/plot/sc_plot.dart';
- import 'package:farm_tpf/presentation/screens/plot_detail/bloc/plot_detail_bloc.dart';
- import 'package:farm_tpf/utils/const_color.dart';
- import 'package:farm_tpf/utils/const_string.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_bloc/flutter_bloc.dart';
- import 'package:farm_tpf/utils/formatter.dart';
- import 'package:get/get.dart';
-
- class PlotActionScreen extends StatefulWidget {
- int cropId;
- String cropCode;
- int cropType;
- PlotActionScreen({this.cropId, this.cropCode, this.cropType});
- @override
- _PlotActionScreenState createState() => _PlotActionScreenState();
- }
-
- class _PlotActionScreenState extends State<PlotActionScreen> {
- List<ActionType> actions = new List<ActionType>();
- ScrollController _scrollController;
- var blocPlotDetail = PlotDetailBloc(repository: Repository());
-
- @override
- void initState() {
- super.initState();
- _scrollController = ScrollController()..addListener(() => setState(() {}));
- _initActionButtons();
- }
-
- @override
- void dispose() {
- blocPlotDetail.close();
- super.dispose();
- }
-
- _initActionButtons() {
- //type: 0- Trồng, 1- ướm
- if (widget.cropType == 1) {
- actions.add(ActionType(
- plot_action_nursery,
- EditActionNurseryScreen(
- cropId: widget.cropId,
- )));
- } else if (widget.cropType == 0) {
- actions.add(ActionType(
- plot_action_plant,
- EditActionPlantScreen(
- cropId: widget.cropId,
- )));
- }
-
- actions.add(ActionType(
- plot_action_crop_status,
- EditActionCropStatusScreen(
- cropId: widget.cropId,
- )));
- actions.add(ActionType(
- plot_action_environment_update,
- EditActionEnvironmentUpdate(
- cropId: widget.cropId,
- )));
- actions.add(ActionType(
- plot_action_dung,
- EditActionDungScreen(
- cropId: widget.cropId,
- )));
- actions.add(ActionType(
- plot_action_spraying,
- EditActionSprayingScreen(
- cropId: widget.cropId,
- )));
- actions.add(ActionType(
- plot_action_disease,
- EditActionDiseaseScreen(
- cropId: widget.cropId,
- )));
- actions.add(ActionType(
- plot_action_use_water,
- EditActionUseWaterScreen(
- cropId: widget.cropId,
- )));
- actions.add(ActionType(
- plot_action_other,
- EditActionOtherScreen(
- cropId: widget.cropId,
- )));
- actions.add(ActionType(
- plot_action_harvest,
- EditActionHarvestScreen(
- cropId: widget.cropId,
- )));
- actions.add(ActionType(
- plot_action_finish,
- EditActionEndScreen(
- cropId: widget.cropId,
- )));
- }
-
- Widget _createActionButtons(ActionType actionType, BuildContext _context) {
- return BlocBuilder<PlotDetailBloc, PlotDetailState>(
- cubit: blocPlotDetail,
- builder: (context, state) {
- return GestureDetector(
- onTap: () {
- Navigator.of(context)
- .push(MaterialPageRoute(
- builder: (context) => actionType.listScreen))
- .then((value) {
- if (value != null) {
- try {
- blocPlotDetail.add(OnRefresh(cropId: widget.cropId));
- } catch (e) {
- print(e);
- }
- }
- });
- },
- child: Container(
- margin: EdgeInsets.all(2.5),
- decoration: BoxDecoration(
- color: COLOR_CONST.WHITE,
- borderRadius: BorderRadius.all(Radius.circular(4.0)),
- boxShadow: <BoxShadow>[
- BoxShadow(
- color: COLOR_CONST.GRAY1.withOpacity(0.2),
- offset: Offset(1.1, 1.1),
- blurRadius: 4.0),
- ],
- ),
- child: Align(
- alignment: Alignment.center,
- child: Text(
- actionType.actionName,
- textAlign: TextAlign.center,
- style: TextStyle(
- fontWeight: FontWeight.w400,
- fontSize: 11,
- color: COLOR_CONST.BLACK2,
- ),
- )),
- ));
- });
- }
-
- bool _showTitle(BuildContext context) {
- var kExpandedHeight = MediaQuery.of(context).size.width * 0.625 + 24;
- return _scrollController.hasClients &&
- _scrollController.offset > kExpandedHeight - kToolbarHeight;
- }
-
- @override
- Widget build(BuildContext context) {
- return BlocBuilder<PlotDetailBloc, PlotDetailState>(
- cubit: blocPlotDetail,
- builder: (context, state) {
- return NestedScrollView(
- controller: _scrollController,
- headerSliverBuilder: (context, innerBoxScrolled) => [
- SliverAppBar(
- floating: false,
- pinned: false,
- backgroundColor: Colors.white,
- leading: Container(),
- title: null,
- // title: _showTitle(context) ? Text(plot_detail_title) : null,
- //Height flexibleSpace : WidthScreen /2 * 4/16 * 5(row) + 8(space) *3
- expandedHeight:
- MediaQuery.of(context).size.width * 0.625 + 24,
- flexibleSpace: _showTitle(context)
- ? null
- : BlocBuilder<PlotDetailBloc, PlotDetailState>(
- cubit: blocPlotDetail,
- builder: (contextB, state) {
- return FlexibleSpaceBar(
- centerTitle: true,
- title: GridView.count(
- shrinkWrap: true,
- crossAxisCount: 2,
- childAspectRatio: 16 / 4,
- children: actions.map(
- (item) {
- return _createActionButtons(
- item, contextB);
- },
- ).toList()),
- );
- })),
- ],
- body: BlocBuilder<PlotDetailBloc, PlotDetailState>(
- cubit: blocPlotDetail,
- builder: (context, state) {
- return InfinityView(
- cropId: widget.cropId,
- parentScroll: _scrollController,
- plotDetailBloc: blocPlotDetail,
- );
- }),
- );
- });
- }
- }
-
- class InfinityView extends StatefulWidget {
- int cropId;
- String cropCode;
- ScrollController parentScroll;
- PlotDetailBloc plotDetailBloc;
- InfinityView(
- {this.cropId,
- this.cropCode,
- @required this.parentScroll,
- @required this.plotDetailBloc});
- @override
- _InfinityViewState createState() => _InfinityViewState();
- }
-
- class _InfinityViewState extends State<InfinityView> {
- final _scrollController = ScrollController();
- final _scrollThreshold = 250.0;
- var heightOfActionButton = Get.width * 0.625 + 24;
- var heightHeaderList = 44;
- var heightListWithoutHeader = Get.height - (Get.width * 0.625 + 24) - 44;
- var currentOffset = 0.0;
-
- @override
- void initState() {
- widget.plotDetailBloc
- .add(DataFetched(cropId: widget.cropId, cropCode: widget.cropCode));
- _scrollController.addListener(() {
- final maxScroll = _scrollController.position.maxScrollExtent;
- final currentScroll = _scrollController.position.pixels;
- if (maxScroll - currentScroll < _scrollThreshold) {
- widget.plotDetailBloc
- .add(DataFetched(cropId: widget.cropId, cropCode: widget.cropCode));
- }
- });
- widget.parentScroll.addListener(() {
- setState(() {
- currentOffset = widget.parentScroll.offset;
- heightListWithoutHeader = Get.height -
- heightOfActionButton -
- heightHeaderList +
- currentOffset;
- });
- });
- super.initState();
- }
-
- @override
- Widget build(BuildContext context) {
- return BlocBuilder<PlotDetailBloc, PlotDetailState>(
- cubit: widget.plotDetailBloc,
- builder: (context, state) {
- if (state is PlotDetailFailure) {
- return Center(child: Text(state.errorString));
- }
- if (state is PlotDetailSuccess) {
- if (state.items.isEmpty) {
- return Center(child: Text("Không có dữ liệu lịch sử canh tác"));
- }
- List<Activities> currentItems = List<Activities>.from(state.items);
- return Scaffold(
- body: RefreshIndicator(
- child: Column(
- children: [
- Expanded(
- flex: 1,
- child: Container(
- alignment: Alignment.center,
- decoration: BoxDecoration(
- color: COLOR_CONST.WHITE_50,
- border: Border(
- top: BorderSide(
- width: 0.5, color: COLOR_CONST.DEFAULT),
- bottom: BorderSide(
- width: 0.5, color: COLOR_CONST.DEFAULT)),
- ),
- child: Container(
- child: Row(
- children: [
- Expanded(
- child: Text(
- plot_detail_title,
- textAlign: TextAlign.center,
- style: TextStyle(
- fontSize: 20,
- fontWeight: FontWeight.normal),
- )),
- GestureDetector(
- child: (0 == currentOffset)
- ? Icon(Icons.arrow_upward)
- : Icon(Icons.arrow_downward_outlined),
- onTap: () {
- if (currentOffset == 0) {
- widget.parentScroll.animateTo(
- heightOfActionButton,
- duration: Duration(milliseconds: 500),
- curve: Curves.easeOut);
- } else {
- widget.parentScroll.animateTo(0.0,
- duration: Duration(milliseconds: 500),
- curve: Curves.easeOut);
- }
- }),
- ],
- ),
- ),
- ),
- ),
- Expanded(
- flex: heightListWithoutHeader ~/ heightHeaderList,
- child: ListView.builder(
- physics: AlwaysScrollableScrollPhysics(),
- itemBuilder: (BuildContext context, int index) {
- return index >= state.items.length
- ? BottomLoader()
- : ItemInfinityWidget(
- currentItems: currentItems,
- item: state.items[index],
- currentPage: state.page,
- currentReachedMax: state.hasReachedMax);
- },
- itemCount: state.hasReachedMax
- ? state.items.length
- : state.items.length + 1,
- controller: _scrollController,
- ))
- ],
- ),
- onRefresh: () async {
- widget.plotDetailBloc.add(OnRefresh(
- cropId: widget.cropId, cropCode: widget.cropCode));
- }),
- );
- }
- return Center(
- child: LoadingListPage(),
- );
- },
- );
- }
-
- @override
- void dispose() {
- _scrollController.dispose();
- super.dispose();
- }
- }
-
- class ItemInfinityWidget extends StatelessWidget {
- final List<Activities> currentItems;
- final Activities item;
- final int currentPage;
- final bool currentReachedMax;
-
- const ItemInfinityWidget(
- {Key key,
- @required this.currentItems,
- @required this.item,
- @required this.currentPage,
- @required this.currentReachedMax})
- : super(key: key);
-
- @override
- Widget build(BuildContext context) {
- return GestureDetector(
- child: Card(
- child: ListTile(
- title: Text(item.activityTypeDescription ?? ''),
- subtitle: Text(item.executeDate.format_DDMMYY_HHmm()),
- ),
- ),
- onTap: () {
- if (item.activityTypeName == "ACTIVE_TYPE_NURSERY") {
- Get.to(EditActionNurseryScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_STATUS_CROP") {
- Get.to(EditActionCropStatusScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_UPDATE_ENV") {
- Get.to(EditActionEnvironmentUpdate(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName ==
- "ACTIVE_TYPE_PESTS_INVESTIGATION") {
- Get.to(EditActionDiseaseScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_USE_WATER") {
- Get.to(EditActionUseWaterScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_HARVEST") {
- Get.to(EditActionHarvestScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_PACKING") {
- Get.to(EditActionPackingScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_SELL") {
- Get.to(EditActionSellScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_END") {
- Get.to(EditActionEndScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_STATUS_GROW") {
- Get.to(EditActionPlantScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_MANURING") {
- Get.to(EditActionDungScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_SPRAY") {
- Get.to(EditActionSprayingScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else if (item.activityTypeName == "ACTIVE_TYPE_PROCESS") {
- Get.to(EditActionHarvestProcessScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- } else {
- Get.to(EditActionOtherScreen(
- cropId: item.cropId,
- activityId: item.id,
- isEdit: true,
- ));
- }
- });
- }
- }
-
- class ActionType {
- Widget listScreen;
- String actionName;
- ActionType(String actionName, Widget listScreen) {
- this.actionName = actionName;
- this.listScreen = listScreen;
- }
- }
|