Browse Source

add select device to action dung

master
daivph 5 years ago
parent
commit
a9891d6ae5
14 changed files with 358 additions and 5 deletions
  1. +2
    -0
      lib/custom_model/Device.dart
  2. +9
    -1
      lib/custom_model/SuppliesUsing.dart
  3. +3
    -0
      lib/data/api/rest_client.dart
  4. +20
    -0
      lib/data/api/rest_client.g.dart
  5. +7
    -0
      lib/data/repository/repository.dart
  6. +28
    -0
      lib/presentation/screens/actions/controller/ChangeDevice.dart
  7. +3
    -1
      lib/presentation/screens/actions/controller/ChangeSupplyUsing.dart
  8. +1
    -0
      lib/presentation/screens/actions/dung/sc_edit_action_dung.dart
  9. +75
    -2
      lib/presentation/screens/actions/dung/widget_dung_supply.dart
  10. +50
    -0
      lib/presentation/screens/actions/resource_device_activity/bloc/device_activity_bloc.dart
  11. +18
    -0
      lib/presentation/screens/actions/resource_device_activity/bloc/device_activity_event.dart
  12. +25
    -0
      lib/presentation/screens/actions/resource_device_activity/bloc/device_activity_state.dart
  13. +116
    -0
      lib/presentation/screens/actions/resource_device_activity/sc_device_activity.dart
  14. +1
    -1
      pubspec.yaml

+ 2
- 0
lib/custom_model/Device.dart View File

String name; String name;
String status; String status;
String location; String location;
bool isSelected;


Device({this.id, this.name, this.status, this.location}); Device({this.id, this.name, this.status, this.location});


name = json['name']; name = json['name'];
status = json['status']; status = json['status'];
location = json['location']; location = json['location'];
isSelected = false;
} }


Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {

+ 9
- 1
lib/custom_model/SuppliesUsing.dart View File

String unit; String unit;
String howToUse; String howToUse;
int suppliesInWarehouseId; int suppliesInWarehouseId;
int equipmentOfCustomerId;
int tbSuppliesInWarehouseId; int tbSuppliesInWarehouseId;
String supplyName; String supplyName;
String supplyUnit; String supplyUnit;
int tbEquipmentOfCustomerId;
int equipmentOfCustomerId;
String equipmentName;


SuppliesUsing( SuppliesUsing(
{this.id, {this.id,
this.unit, this.unit,
this.howToUse, this.howToUse,
this.suppliesInWarehouseId, this.suppliesInWarehouseId,
this.tbEquipmentOfCustomerId,
this.equipmentOfCustomerId, this.equipmentOfCustomerId,
this.equipmentName,
this.tbSuppliesInWarehouseId, this.tbSuppliesInWarehouseId,
this.supplyName, this.supplyName,
this.supplyUnit}); this.supplyUnit});
unit = json['unit']; unit = json['unit'];
howToUse = json['howToUse']; howToUse = json['howToUse'];
suppliesInWarehouseId = json['suppliesInWarehouseId']; suppliesInWarehouseId = json['suppliesInWarehouseId'];
tbEquipmentOfCustomerId = json['tbEquipmentOfCustomerId'];
equipmentOfCustomerId = json['equipmentOfCustomerId']; equipmentOfCustomerId = json['equipmentOfCustomerId'];
equipmentName = json['equipmentName'];
tbSuppliesInWarehouseId = json['tbSuppliesInWarehouseId']; tbSuppliesInWarehouseId = json['tbSuppliesInWarehouseId'];
supplyName = json['supplyName']; supplyName = json['supplyName'];
supplyUnit = json['supplyUnit']; supplyUnit = json['supplyUnit'];
data['unit'] = this.unit; data['unit'] = this.unit;
data['howToUse'] = this.howToUse; data['howToUse'] = this.howToUse;
data['suppliesInWarehouseId'] = this.suppliesInWarehouseId; data['suppliesInWarehouseId'] = this.suppliesInWarehouseId;
data['tbEquipmentOfCustomerId'] = this.tbEquipmentOfCustomerId;
data['equipmentOfCustomerId'] = this.equipmentOfCustomerId; data['equipmentOfCustomerId'] = this.equipmentOfCustomerId;
data['equipmentName'] = this.equipmentName;
data['tbSuppliesInWarehouseId'] = this.tbSuppliesInWarehouseId; data['tbSuppliesInWarehouseId'] = this.tbSuppliesInWarehouseId;
data['supplyName'] = this.supplyName; data['supplyName'] = this.supplyName;
data['supplyUnit'] = this.supplyUnit; data['supplyUnit'] = this.supplyUnit;

+ 3
- 0
lib/data/api/rest_client.dart View File

@GET("/api/tb-harvests") @GET("/api/tb-harvests")
Future<List<Harvest>> getHarvests({@DioOptions() Options options}); Future<List<Harvest>> getHarvests({@DioOptions() Options options});


@GET("/api/listDeviceForActivity")
Future<List<Device>> getDeviceForActivity({@DioOptions() Options options});

//Crop //Crop
@GET( @GET(
"/api/tb-crops-detail-for-app/{cropId}?page={page}&size={size}&sort=executeDate,DESC") "/api/tb-crops-detail-for-app/{cropId}?page={page}&size={size}&sort=executeDate,DESC")

+ 20
- 0
lib/data/api/rest_client.g.dart View File

return value; return value;
} }


@override
getDeviceForActivity({options}) async {
const _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
queryParameters.removeWhere((k, v) => v == null);
final _data = <String, dynamic>{};
final newOptions = newRequestOptions(options);
newOptions.extra.addAll(_extra);
newOptions.headers.addAll(<String, dynamic>{});
final Response<List<dynamic>> _result = await _dio.request(
'/api/listDeviceForActivity',
queryParameters: queryParameters,
options: newOptions.merge(method: 'GET', baseUrl: baseUrl),
data: _data);
var value = _result.data
.map((dynamic i) => Device.fromJson(i as Map<String, dynamic>))
.toList();
return value;
}

@override @override
getCropDetail(cropId, {page = 0, size = 20}) async { getCropDetail(cropId, {page = 0, size = 20}) async {
ArgumentError.checkNotNull(cropId, 'cropId'); ArgumentError.checkNotNull(cropId, 'cropId');

+ 7
- 0
lib/data/repository/repository.dart View File

return client.getSupplies(query: query, options: op); return client.getSupplies(query: query, options: op);
} }


Future<List<Device>> getDeviceForActivity() async {
final client = RestClient(dio);
var op = buildConfigurableCacheOptions(
forceRefresh: true, maxAge: Duration(days: ConstCommon.kMaxAgeCache));
return client.getDeviceForActivity(options: op);
}

Object getInstanceClass() { Object getInstanceClass() {
var instanceClass; var instanceClass;
if (1 == 1) { if (1 == 1) {

+ 28
- 0
lib/presentation/screens/actions/controller/ChangeDevice.dart View File

import 'package:farm_tpf/custom_model/Device.dart';
import 'package:get/get.dart';

class ChangeDevice extends GetxController {
Device currentDevice;
int selectedDeviceId;
String selectedDeviceName;

void initValue() {
currentDevice = Device();
selectedDeviceName = "";
selectedDeviceId = -1;
update();
}

void change(Device device) {
currentDevice = device;
selectedDeviceId = device.id;
selectedDeviceName = device.name;
update();
}

void changeByIdAndName(int id, String name) {
selectedDeviceId = id;
selectedDeviceName = name;
update();
}
}

+ 3
- 1
lib/presentation/screens/actions/controller/ChangeSupplyUsing.dart View File

} }


void editSupply(int index, SuppliesUsing supplyUsing) { void editSupply(int index, SuppliesUsing supplyUsing) {
currentItems[index] = supplyUsing;
var newSup = supplyUsing;
newSup.id = currentItems[index].id;
currentItems[index] = newSup;
currentSupplyUsing = SuppliesUsing(); currentSupplyUsing = SuppliesUsing();
update(); update();
} }

+ 1
- 0
lib/presentation/screens/actions/dung/sc_edit_action_dung.dart View File

suppliesUsing.forEach((sup) { suppliesUsing.forEach((sup) {
var newSup = sup; var newSup = sup;
newSup.suppliesInWarehouseId = sup.tbSuppliesInWarehouseId; newSup.suppliesInWarehouseId = sup.tbSuppliesInWarehouseId;
newSup.equipmentOfCustomerId = sup.tbEquipmentOfCustomerId;
newSups.add(newSup); newSups.add(newSup);
}); });
_dung.suppliesUsing = newSups; _dung.suppliesUsing = newSups;

+ 75
- 2
lib/presentation/screens/actions/dung/widget_dung_supply.dart View File

import 'package:farm_tpf/custom_model/Device.dart';
import 'package:farm_tpf/custom_model/SuppliesUsing.dart'; import 'package:farm_tpf/custom_model/SuppliesUsing.dart';
import 'package:farm_tpf/custom_model/Supply.dart'; import 'package:farm_tpf/custom_model/Supply.dart';
import 'package:farm_tpf/presentation/screens/actions/controller/ChangeDevice.dart';
import 'package:farm_tpf/presentation/screens/actions/controller/ChangeFormButton.dart'; import 'package:farm_tpf/presentation/screens/actions/controller/ChangeFormButton.dart';
import 'package:farm_tpf/presentation/screens/actions/controller/ChangeSupplyUsing.dart'; import 'package:farm_tpf/presentation/screens/actions/controller/ChangeSupplyUsing.dart';
import 'package:farm_tpf/presentation/screens/actions/controller/ChangeUnit.dart'; import 'package:farm_tpf/presentation/screens/actions/controller/ChangeUnit.dart';
import 'package:farm_tpf/presentation/screens/actions/resource_device_activity/sc_device_activity.dart';
import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_supply.dart'; import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_supply.dart';
import 'package:farm_tpf/presentation/screens/resources/sc_resource_helper.dart'; import 'package:farm_tpf/presentation/screens/resources/sc_resource_helper.dart';
import 'package:farm_tpf/utils/const_color.dart'; import 'package:farm_tpf/utils/const_color.dart';
final changeSupplyUsing = Get.put(ChangeSupplyUsing()); final changeSupplyUsing = Get.put(ChangeSupplyUsing());
final changeUnit = Get.put(ChangeUnit()); final changeUnit = Get.put(ChangeUnit());
final changeButton = Get.put(ChangeButtonInForm()); final changeButton = Get.put(ChangeButtonInForm());
final changeSelectedDevice = Get.put(ChangeDevice());


@override @override
void initState() { void initState() {
super.initState(); super.initState();
changeSelectedSupply.initValue(); changeSelectedSupply.initValue();
changeSelectedDevice.initValue();
changeSupplyUsing.init(widget.currentItems); changeSupplyUsing.init(widget.currentItems);
changeUnit.initValue(); changeUnit.initValue();
changeButton.resetValue(); changeButton.resetValue();
return Container(); return Container();
} else { } else {
return Container( return Container(
height: 100,
height: 120,
child: ListView.builder( child: ListView.builder(
physics: ClampingScrollPhysics(), physics: ClampingScrollPhysics(),
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
..tbSuppliesName = editedSupplyUsing.supplyName ..tbSuppliesName = editedSupplyUsing.supplyName
..unit = editedSupplyUsing.supplyUnit; ..unit = editedSupplyUsing.supplyUnit;
changeSelectedSupply.change(editedSupply); changeSelectedSupply.change(editedSupply);

var editedDevice = Device()
..id = editedSupplyUsing.tbEquipmentOfCustomerId
..name = editedSupplyUsing.equipmentName;
changeSelectedDevice.change(editedDevice);

changeUnit changeUnit
.updateListByUnitName(editedSupplyUsing.supplyUnit); .updateListByUnitName(editedSupplyUsing.supplyUnit);
changeUnit.updateSelected(editedSupplyUsing.supplyUnit); changeUnit.updateSelected(editedSupplyUsing.supplyUnit);
Flexible( Flexible(
child: Text( child: Text(
"${value.currentItems[index].quantity.formatNumtoStringDecimal() ?? ""} ${value.currentItems[index].supplyUnit ?? ""}")), "${value.currentItems[index].quantity.formatNumtoStringDecimal() ?? ""} ${value.currentItems[index].supplyUnit ?? ""}")),
Flexible(
child: Text(
"${value.currentItems[index].equipmentName ?? ""}")),
Flexible( Flexible(
child: Text( child: Text(
"${value.currentItems[index].howToUse ?? ""}")), "${value.currentItems[index].howToUse ?? ""}")),
.push(MaterialPageRoute( .push(MaterialPageRoute(
builder: (_) => ResourceHelperScreen( builder: (_) => ResourceHelperScreen(
titleName: "Phân bón", titleName: "Phân bón",
type: ConstCommon.supplyTypeDung,
type: ConstCommon.supplyTypeAll,
selectedId: changeSelectedSupply.selectedSupplyId), selectedId: changeSelectedSupply.selectedSupplyId),
fullscreenDialog: false)) fullscreenDialog: false))
.then((value) { .then((value) {
}); });
} }


Widget _btnSelectDevice() {
return GetBuilder<ChangeDevice>(builder: (data) {
return FlatButton(
padding:
EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(
builder: (_) => ListDeviceActivity(
selectedId: changeSelectedDevice.selectedDeviceId),
fullscreenDialog: false))
.then((value) {
if (value != null) {
var result = value as Device;
changeSelectedDevice.change(result);
}
});
},
child: Container(
padding: EdgeInsets.only(
top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
decoration: BoxDecoration(
border: kBorderTextField,
),
child: Row(
children: [
GetBuilder<ChangeSupply>(
builder: (_) => Expanded(
child: Text(
changeSelectedDevice.selectedDeviceName ??
"Thiết bị",
style: TextStyle(
fontSize: 14.0, color: Colors.black87)))),
Icon(
Icons.arrow_drop_down,
color: Colors.grey,
),
],
)));
});
}

Widget _dropdownUnitTypes() { Widget _dropdownUnitTypes() {
return GetBuilder<ChangeUnit>(builder: (data) { return GetBuilder<ChangeUnit>(builder: (data) {
return DropdownButtonFormField<String>( return DropdownButtonFormField<String>(
borderRadius: new BorderRadius.circular(8.0)), borderRadius: new BorderRadius.circular(8.0)),
onPressed: () { onPressed: () {
var currentSupply = changeSelectedSupply.currentSupply; var currentSupply = changeSelectedSupply.currentSupply;
var currentDevice = changeSelectedDevice.currentDevice;
var currentQuantity = var currentQuantity =
_quantityController.text.parseDoubleThousand(); _quantityController.text.parseDoubleThousand();
if (currentSupply.id != null && currentQuantity > 0) { if (currentSupply.id != null && currentQuantity > 0) {
..tbSuppliesInWarehouseId = currentSupply.id ..tbSuppliesInWarehouseId = currentSupply.id
..suppliesInWarehouseId = currentSupply.id ..suppliesInWarehouseId = currentSupply.id
..supplyName = currentSupply.tbSuppliesName ..supplyName = currentSupply.tbSuppliesName
..tbEquipmentOfCustomerId = currentDevice.id
..equipmentOfCustomerId = currentDevice.id
..equipmentName = currentDevice.name
..supplyUnit = currentSupply.unit ..supplyUnit = currentSupply.unit
..unit = currentSupply.unit; ..unit = currentSupply.unit;
changeSupplyUsing.editSupply( changeSupplyUsing.editSupply(
borderRadius: new BorderRadius.circular(8.0)), borderRadius: new BorderRadius.circular(8.0)),
onPressed: () { onPressed: () {
var currentSupply = changeSelectedSupply.currentSupply; var currentSupply = changeSelectedSupply.currentSupply;
var currentDevice = changeSelectedDevice.currentDevice;
var currentQuantity = var currentQuantity =
_quantityController.text.parseDoubleThousand(); _quantityController.text.parseDoubleThousand();
if (currentSupply.id != null && currentQuantity > 0) { if (currentSupply.id != null && currentQuantity > 0) {
..suppliesInWarehouseId = currentSupply.id ..suppliesInWarehouseId = currentSupply.id
..supplyName = currentSupply.tbSuppliesName ..supplyName = currentSupply.tbSuppliesName
..supplyUnit = currentSupply.unit ..supplyUnit = currentSupply.unit
..tbEquipmentOfCustomerId = currentDevice.id
..equipmentOfCustomerId = currentDevice.id
..equipmentName = currentDevice.name
..unit = currentSupply.unit; ..unit = currentSupply.unit;
changeSupplyUsing.addSupply(newSup); changeSupplyUsing.addSupply(newSup);
_resetForm(); _resetForm();
child: _dropdownUnitTypes(), child: _dropdownUnitTypes(),
)), )),
]), ]),
Container(
width: double.infinity,
child: Text(
"Thiết bị",
style: TextStyle(color: Colors.black54, fontSize: 13.0),
),
),
_btnSelectDevice(),
TextFormField( TextFormField(
keyboardType: TextInputType.text, keyboardType: TextInputType.text,
controller: _howToUseController, controller: _howToUseController,
_quantityController.text = ""; _quantityController.text = "";
changeUnit.initValue(); changeUnit.initValue();
changeSelectedSupply.initValue(); changeSelectedSupply.initValue();
changeSelectedDevice.initValue();
} }


_hidenKeyboard(BuildContext context) { _hidenKeyboard(BuildContext context) {

+ 50
- 0
lib/presentation/screens/actions/resource_device_activity/bloc/device_activity_bloc.dart View File

import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:farm_tpf/custom_model/Device.dart';
import 'package:farm_tpf/data/repository/repository.dart';
import 'package:meta/meta.dart';

part 'device_activity_event.dart';
part 'device_activity_state.dart';

class DeviceActivityBloc
extends Bloc<DeviceActivityEvent, DeviceActivityState> {
final Repository repository;
DeviceActivityBloc({@required this.repository})
: super(DeviceActivityInitial());

@override
Stream<DeviceActivityState> mapEventToState(
DeviceActivityEvent event,
) async* {
if (event is DataFetched) {
try {
final response = await repository.getDeviceForActivity();
List<Device> devices = response.map((device) {
if (device.id == event.selectedId) {
device.isSelected = true;
}
return device;
}).toList();
yield DeviceActivitySuccess(items: devices);
} catch (_) {
yield DeviceActivityFailure();
}
} else if (event is OnRefresh) {
try {
final response = await repository.getDeviceForActivity();
List<Device> devices = response.map((device) {
if (device.id == event.selectedId) {
device.isSelected = true;
}
return device;
}).toList();
yield DeviceActivitySuccess(items: devices);
} catch (_) {
yield DeviceActivityFailure();
}
}
}
}

+ 18
- 0
lib/presentation/screens/actions/resource_device_activity/bloc/device_activity_event.dart View File

part of 'device_activity_bloc.dart';

abstract class DeviceActivityEvent extends Equatable {
const DeviceActivityEvent();

@override
List<Object> get props => [];
}

class DataFetched extends DeviceActivityEvent {
final int selectedId;
DataFetched({this.selectedId});
}

class OnRefresh extends DeviceActivityEvent {
final int selectedId;
OnRefresh({this.selectedId});
}

+ 25
- 0
lib/presentation/screens/actions/resource_device_activity/bloc/device_activity_state.dart View File

part of 'device_activity_bloc.dart';

abstract class DeviceActivityState extends Equatable {
const DeviceActivityState();

@override
List<Object> get props => [];
}

class DeviceActivityInitial extends DeviceActivityState {}

class DeviceActivityFailure extends DeviceActivityState {}

class DeviceActivitySuccess<T> extends DeviceActivityState {
final List<T> items;

const DeviceActivitySuccess({this.items});

DeviceActivitySuccess copyWith({List<T> items}) {
return DeviceActivitySuccess(items: items ?? this.items);
}

@override
List<Object> get props => [items];
}

+ 116
- 0
lib/presentation/screens/actions/resource_device_activity/sc_device_activity.dart View File

import 'package:farm_tpf/custom_model/Device.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/utils/const_string.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'bloc/device_activity_bloc.dart';

class ListDeviceActivity extends StatefulWidget {
final int selectedId;
ListDeviceActivity({@required this.selectedId});
@override
_ListDeviceActivityState createState() => _ListDeviceActivityState();
}

class _ListDeviceActivityState extends State<ListDeviceActivity> {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => DeviceActivityBloc(repository: Repository())
..add(DataFetched(selectedId: widget.selectedId)),
child: HoldInfinityWidget(selectedId: widget.selectedId),
);
}
}

class HoldInfinityWidget extends StatelessWidget {
final int selectedId;
HoldInfinityWidget({@required this.selectedId});
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(title: Text("Chọn thiết bị")),
body: InfinityView(selectedId: selectedId));
}
}

class InfinityView extends StatefulWidget {
final int selectedId;
InfinityView({@required this.selectedId});
@override
_InfinityViewState createState() => _InfinityViewState();
}

class _InfinityViewState extends State<InfinityView> {
DeviceActivityBloc _deviceActivityBloc;

@override
void initState() {
_deviceActivityBloc = BlocProvider.of<DeviceActivityBloc>(context);
_deviceActivityBloc.add(DataFetched(selectedId: widget.selectedId));
super.initState();
}

@override
Widget build(BuildContext context) {
return BlocBuilder<DeviceActivityBloc, DeviceActivityState>(
builder: (context, state) {
if (state is DeviceActivityFailure) {
return Center(child: Text(label_error_get_data));
}
if (state is DeviceActivitySuccess) {
if (state.items.isEmpty) {
return Center(child: Text(label_list_empty));
}
return RefreshIndicator(
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return index >= state.items.length
? BottomLoader()
: ItemInfinityWidget(item: state.items[index]);
},
itemCount: state.items.length),
onRefresh: () async {
_deviceActivityBloc
.add(OnRefresh(selectedId: widget.selectedId));
});
}
return Center(
child: LoadingListPage(),
);
},
);
}

@override
void dispose() {
super.dispose();
}
}

class ItemInfinityWidget extends StatelessWidget {
final Device item;

const ItemInfinityWidget({Key key, @required this.item}) : super(key: key);

@override
Widget build(BuildContext context) {
return GestureDetector(
child: Card(
child: Material(
child: RadioListTile(
title: Text(item.name.toString()),
value: item,
groupValue: item.isSelected == false ? null : item,
onChanged: (Device value) {
Navigator.of(context).pop(value);
}),
)),
onTap: () {});
}
}

+ 1
- 1
pubspec.yaml View File

description: A new Flutter project. description: A new Flutter project.


publish_to: 'none' publish_to: 'none'
version: 0.2.0+1
version: 0.3.0+1


environment: environment:
sdk: ">=2.7.0 <3.0.0" sdk: ">=2.7.0 <3.0.0"

Loading…
Cancel
Save