import 'package:farm_tpf/data/repository/repository.dart'; import 'package:farm_tpf/models/index.dart'; import 'package:farm_tpf/presentation/screens/actions/other/bloc_get_action_type.dart'; import 'package:farm_tpf/utils/const_string.dart'; import 'package:farm_tpf/utils/const_style.dart'; import 'package:farm_tpf/utils/pref.dart'; import 'package:farm_tpf/utils/validators.dart'; import 'package:flutter/material.dart'; import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:intl/intl.dart'; import 'package:keyboard_dismisser/keyboard_dismisser.dart'; class EditActionOtherScreen extends StatefulWidget { @override _EditActionOtherScreenState createState() => _EditActionOtherScreenState(); } class _EditActionOtherScreenState extends State { final GlobalKey _scaffoldKey = new GlobalKey(); final _repository = Repository(); GlobalKey _formKey = GlobalKey(); bool _autoValidate = false; OtherAction _otherAction = OtherAction(); List _actionTypes = List(); ActionType _actionType; var pref = LocalPref(); FlutterToast flutterToast; TextEditingController _descriptionController = TextEditingController(); TextEditingController _workerNameController = TextEditingController(); String executeTimeView; DateTime executeTime = DateTime.now(); @override void initState() { super.initState(); flutterToast = FlutterToast(context); //UPDATE if (_otherAction != null) { try { executeTime = DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") .parse(_otherAction.executeDate); } catch (_) {} } else { //ADD NEW var parsedExecuteDate = DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime); _otherAction.executeDate = "$parsedExecuteDate"; } executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime); //Get action types getActionTypeBloc.getActionTypes((data) { _actionTypes = data; for (var item in _actionTypes) { if (item.id == _otherAction.activityId) { _actionType = item; break; } } }, (err) {}); } Widget _btnExecuteTimePicker() { return FlatButton( padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0), onPressed: () { DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {}, onConfirm: (date) { setState(() { var parsedDate = DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(date); _otherAction.executeDate = "$parsedDate"; executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(date); }); }, currentTime: executeTime, locale: LocaleType.vi); }, child: Container( padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0), decoration: BoxDecoration( border: kBorderTextField, ), child: Row( children: [ Expanded( child: Text( //TODO: check condition executeTimeView == null ? "$executeTime" : executeTimeView, style: TextStyle(fontSize: 14.0, color: Colors.black87), )), Icon( Icons.date_range, color: Colors.blue, ), ], ))); } List> _buildDropMenu(List actions) { return actions .map((action) => DropdownMenuItem( child: Text(action.description.toString()), value: action, )) .toList(); } Widget _dropdownAcionTypes() { return StreamBuilder( stream: getActionTypeBloc.actions, builder: (context, AsyncSnapshot snapshot) { if (snapshot.hasData) { return DropdownButtonFormField( value: _actionType, hint: Text("Hoạt động"), onChanged: (ActionType newValue) { setState(() { _actionType = newValue; _otherAction.activityId = newValue.id; _otherAction.activityTypeName = newValue.name; }); }, validator: (value) => value == null ? "Hoạt động" : null, isExpanded: true, items: _buildDropMenu(_actionTypes)); } else if (snapshot.hasError) { return Container(); } else { return Center( child: CircularProgressIndicator(), ); } }, ); } Widget _desciptionField() { return TextFormField( keyboardType: TextInputType.text, decoration: InputDecoration(labelText: "Ghi chú"), controller: _descriptionController, onSaved: (newValue) { _otherAction.description = newValue; }, ); } Widget _workerNameField() { return TextFormField( keyboardType: TextInputType.text, decoration: InputDecoration(labelText: "Người thực hiện"), controller: _workerNameController, validator: (String value) { return Validators.validateNotNullOrEmpty(value, "Người thực hiện"); }, onSaved: (newValue) { _otherAction.workerName = newValue; }, ); } _actionAppBar() { IconButton iconButton; if (1 == 1) { iconButton = IconButton( icon: Icon( Icons.done, color: Colors.black, ), onPressed: () {}, ); return [iconButton]; } return [Container()]; } @override Widget build(BuildContext context) => KeyboardDismisser( gestures: [ GestureType.onTap, GestureType.onPanUpdateDownDirection, ], child: Scaffold( key: _scaffoldKey, appBar: AppBar( centerTitle: true, title: Text(plot_action_other), actions: _actionAppBar()), body: KeyboardDismisser( child: Form( key: _formKey, autovalidate: _autoValidate, child: SingleChildScrollView( padding: EdgeInsets.all(8.0), child: Column( children: [ Container( width: double.infinity, child: Text( "Ngày thực hiện", style: TextStyle( color: Colors.black54, fontSize: 13.0), ), ), _btnExecuteTimePicker(), SizedBox( height: 8.0, ), _dropdownAcionTypes(), SizedBox( height: 8.0, ), _desciptionField(), SizedBox( height: 8.0, ), _workerNameField() ], ), ))))); @override void dispose() { _descriptionController.dispose(); _workerNameController.dispose(); super.dispose(); } }