|
- import 'package:farm_tpf/custom_model/Plant.dart';
- import 'package:farm_tpf/data/repository/repository.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';
- import 'package:pattern_formatter/pattern_formatter.dart';
- import 'package:farm_tpf/utils/formatter.dart';
-
- class EditActionPlantScreen extends StatefulWidget {
- @override
- _EditActionPlantScreenState createState() => _EditActionPlantScreenState();
- }
-
- class _EditActionPlantScreenState extends State<EditActionPlantScreen> {
- final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
- final _repository = Repository();
- GlobalKey<FormState> _formKey = GlobalKey();
- bool _autoValidate = false;
- Plant _plant = Plant();
- var pref = LocalPref();
- FlutterToast flutterToast;
- TextEditingController _descriptionController = TextEditingController();
- TextEditingController _quantityController = TextEditingController();
- TextEditingController _densityController = TextEditingController();
- String executeTimeView;
- DateTime executeTime = DateTime.now();
-
- @override
- void initState() {
- super.initState();
- flutterToast = FlutterToast(context);
- //UPDATE
- if (_plant != null) {
- try {
- executeTime =
- DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(_plant.executeDate);
- } catch (_) {}
- } else {
- //ADD NEW
- var parsedExecuteDate =
- DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime);
- _plant.executeDate = "$parsedExecuteDate";
- }
- executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime);
- }
-
- 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);
- _plant.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,
- ),
- ],
- )));
- }
-
- Widget _btnSelectSeed() {
- return FlatButton(
- padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
- onPressed: () {},
- 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(
- "Tên giống",
- style: TextStyle(fontSize: 14.0, color: Colors.black87),
- )),
- Icon(
- Icons.arrow_drop_down,
- color: Colors.grey,
- ),
- ],
- )));
- }
-
- Widget _quantityField() {
- return TextFormField(
- keyboardType: TextInputType.numberWithOptions(decimal: true),
- inputFormatters: [
- ThousandsFormatter(
- formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
- ],
- decoration: InputDecoration(labelText: "Số lượng cây trồng"),
- controller: _quantityController,
- validator: (String value) {
- return Validators.validNumber(value, "Số lượng cây trồng");
- },
- onSaved: (newValue) {
- _plant.quantity = newValue.parseDoubleThousand();
- },
- );
- }
-
- Widget _densityField() {
- return TextFormField(
- keyboardType: TextInputType.text,
- decoration: InputDecoration(labelText: "Mật độ"),
- controller: _densityController,
- onSaved: (newValue) {
- _plant.density = newValue;
- },
- );
- }
-
- Widget _desciptionField() {
- return TextFormField(
- keyboardType: TextInputType.text,
- decoration: InputDecoration(labelText: "Ghi chú"),
- controller: _descriptionController,
- onSaved: (newValue) {
- _plant.description = newValue;
- },
- );
- }
-
- _actionAppBar() {
- IconButton iconButton;
- if (1 == 1) {
- iconButton = IconButton(
- icon: Icon(
- Icons.done,
- color: Colors.black,
- ),
- onPressed: () {},
- );
- return <Widget>[iconButton];
- }
- return <Widget>[Container()];
- }
-
- @override
- Widget build(BuildContext context) => KeyboardDismisser(
- gestures: [
- GestureType.onTap,
- GestureType.onPanUpdateDownDirection,
- ],
- child: Scaffold(
- key: _scaffoldKey,
- appBar: AppBar(
- centerTitle: true,
- title: Text(plot_action_plant),
- actions: _actionAppBar()),
- body: KeyboardDismisser(
- child: Form(
- key: _formKey,
- autovalidate: _autoValidate,
- child: SingleChildScrollView(
- padding: EdgeInsets.all(8.0),
- child: Column(
- children: <Widget>[
- 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,
- ),
- Container(
- width: double.infinity,
- child: Text(
- "Tên giống",
- style: TextStyle(
- color: Colors.black54, fontSize: 13.0),
- ),
- ),
- _btnSelectSeed(),
- SizedBox(
- height: 8.0,
- ),
- _quantityField(),
- SizedBox(
- height: 8.0,
- ),
- _densityField(),
- SizedBox(
- height: 8.0,
- ),
- _desciptionField()
- ],
- ),
- )))));
- @override
- void dispose() {
- _quantityController.dispose();
- _densityController.dispose();
- _descriptionController.dispose();
- super.dispose();
- }
- }
|