Skip to content

Commit

Permalink
add model for movements and create his own service for fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
abdessamadpas committed Aug 30, 2023
1 parent 448ceab commit d463584
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 1 deletion.
113 changes: 113 additions & 0 deletions lib/controllers/stock/logMovementController.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import 'dart:ffi';
import 'dart:math';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

import 'dart:convert';
import 'package:flutter/material.dart';
import '../../../constant.dart';
import 'package:sendatrack/model/movementGet.dart';
import 'package:sendatrack/services/movement.dart';

class LogMovementController extends GetxController {
RxBool tangerStore = false.obs;
RxBool casaStore = false.obs;
RxList<MovementGet> stockList = <MovementGet>[].obs;
RxList<MovementGet> stockListFiltered = <MovementGet>[].obs;
bool _isFetching = false;

RxBool isLoading = true.obs;
@override
void onInit() {
fetchStock();
// ever(product, (_) async {
// stockList.clear();
// stockListFiltered.clear();

// await fetchStock();
// });
// ever(store, (_) async {
// stockList.clear();
// stockListFiltered.clear();
// await fetchStock();
// });
super.onInit();
}

void changeStateShip(String value) {
if (_isFetching) return; // Ignore state changes when fetching data

if (value == "tanger") {
tangerStore.value = !tangerStore.value;
casaStore.value = false;
isLoading.value = true;
} else if (value == "casa") {
casaStore.value = !casaStore.value;
tangerStore.value = false;

isLoading.value = true;
} else {
tangerStore.value = false;
casaStore.value = false;
}

_isFetching = true; // Mark that we are fetching data

//! Wait for 800 milliseconds before fetching data
Future.delayed(Duration(milliseconds: 1), () {
fetchStock().then((_) {
_isFetching = false; // Mark that data fetching is done
});
});
}

Future<void> fetchStock() async {
isLoading.value = true;

String status;
if (tangerStore.value == true) {
status = "tanger";
} else if (casaStore.value == true) {
status = "casa";
} else {
status = "all";
}
List<MovementGet> data = await MovementService.GetMovements();
print(data);

// Clear the lists before adding new data
stockList.clear();
stockListFiltered.clear();

stockList.addAll(data);
stockListFiltered.addAll(data);

isLoading.value = false;
}

// void filterSearchResults(String query) {
// List<MovementGet> result = [];

// if (query.isNotEmpty) {
// result = stockList.where((element) {
// bool matchReference = element.Reference != null &&
// element.Reference!.toLowerCase().contains(query.toLowerCase());

// bool matchDesignation = element.Designation != null &&
// element.Designation!
// .toString()
// .toLowerCase()
// .contains(query.toLowerCase());
// bool matchStore = element.id_Store != null &&
// element.id_Store!.toLowerCase().contains(query.toLowerCase());
// bool matchQte = element.Qte != null &&
// element.Qte!.toString().toLowerCase().contains(query.toLowerCase());
// return matchReference || matchDesignation || matchStore || matchQte;
// }).toList();
// } else {
// result = List.from(stockList);
// }

// stockListFiltered.value = result;
// }
}
88 changes: 88 additions & 0 deletions lib/model/movementGet.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import 'dart:convert';

import 'package:get/get_rx/src/rx_types/rx_types.dart';

List<MovementGet> clientFromJson(String str) => List<MovementGet>.from(
json.decode(str).map((x) => MovementGet.fromJson(x)));

class MovementGet {
int? id;
String? id_Store;
String? id_delivery;
String? TypeMvt;
String? id_Vehicule;
String? id_TypePanne;
int? DateMvt;
String? Reference;
String? NumBon;
int? Qte;
int? Tva;
int? Price;
String? observation;
int? kilometrage;
int? Extra;
String? idCiterne;
String? Designation;
String? login;
MovementGet({
this.id,
this.id_Store,
this.id_delivery,
this.TypeMvt,
this.id_Vehicule,
this.id_TypePanne,
this.DateMvt,
this.Reference,
this.NumBon,
this.Qte,
this.Tva,
this.Price,
this.observation,
this.kilometrage,
this.Extra,
this.idCiterne,
this.Designation,
this.login,
});

factory MovementGet.fromJson(Map<String, dynamic> json) => MovementGet(
id: json["id"],
id_Store: json["id_Store"],
id_delivery: json["id_delivery"],
TypeMvt: json["TypeMvt"],
id_Vehicule: json["id_Vehicule"],
id_TypePanne: json["id_TypePanne"],
DateMvt: json["DateMvt"],
Reference: json["Reference"],
NumBon: json["NumBon"],
Qte: json["Qte"],
Tva: json["Tva"],
Price: json["Price"],
observation: json["observation"],
kilometrage: json["kilometrage"],
Extra: json["Extra"],
idCiterne: json["idCiterne"],
Designation: json["Designation"],
login: json["login"],
);
Map<String, dynamic> toJson() => {
"id": id,
"id_Store": id_Store,
"id_delivery": id_delivery,
"TypeMvt": TypeMvt,
"id_Vehicule": id_Vehicule,
"id_TypePanne": id_TypePanne,
"DateMvt": DateMvt,
"Reference": Reference,
"NumBon": NumBon,
"Qte": Qte,
"Tva": Tva,
"Price": Price,
"observation": observation,
"kilometrage": kilometrage,
"Extra": Extra,
"idCiterne": idCiterne,
"Designation": Designation,
"login": login,
};
}
4 changes: 3 additions & 1 deletion lib/screens/managementStockScreen/movementLogsScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '../../../constant.dart';
import 'package:sendatrack/screens/managementStockScreen/dashboardStockScreen.dart';
import 'package:get/get.dart';
import 'package:sendatrack/widgets/dashboardStock/movementCard.dart';
import 'package:sendatrack/widgets/dashboardStock/headerMovementList.dart';

class MovementLogsScreen extends StatefulWidget {
const MovementLogsScreen({super.key});
Expand Down Expand Up @@ -50,6 +51,7 @@ class _MovementLogsScreenState extends State<MovementLogsScreen> {
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
HeaderMovementList(),
Expanded(
child: ListView.builder(
itemCount: 5,
Expand Down Expand Up @@ -131,7 +133,7 @@ class _MovementLogsScreenState extends State<MovementLogsScreen> {
color: lightGray),
),
Gap(7),
Text("12-Nov-2023",
Text("12Nov-2023",
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
Expand Down
26 changes: 26 additions & 0 deletions lib/services/movement.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:convert';
import 'package:sendatrack/utils/token.dart';

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:sendatrack/model/movement.dart';
import 'package:sendatrack/model/movementGet.dart';

import 'dart:convert';
import 'package:http/http.dart' as http;
Expand All @@ -27,4 +29,28 @@ class MovementService {
print('Error: $e');
}
}

static Future GetMovements() async {
String serverUrl =
'http://backup.sendatrack.com/sendatrack-dev-othman/public/api/loadMvt';
Map<String, String> headers = await TokenService.getHeaders();

try {
final response = await http.get(Uri.parse(serverUrl), headers: headers);

if (response.statusCode == 200) {
var data = json.decode(response.body);
List<MovementGet> movements = [];
for (var item in data) {
movements.add(MovementGet.fromJson(item));
}

return movements;
} else {
print('Request failed with status: ${response.statusCode}');
}
} catch (e) {
print('Error: $e');
}
}
}
129 changes: 129 additions & 0 deletions lib/widgets/dashboardStock/headerMovementList.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import 'package:flutter/material.dart';
import 'package:sendatrack/constant.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:sendatrack/constant.dart';
import 'package:get/get.dart';
import 'package:sendatrack/controllers/stock/logMovementController.dart';

class HeaderMovementList extends StatefulWidget {
const HeaderMovementList({super.key});

@override
State<HeaderMovementList> createState() => _HeaderMovementListState();
}

class _HeaderMovementListState extends State<HeaderMovementList> {
LogMovementController controller = Get.put(LogMovementController());

@override
Widget build(BuildContext context) {
return Obx(() => Padding(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
child: Column(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: 40,
margin: EdgeInsets.only(top: 8),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey, width: 1),
borderRadius: BorderRadius.circular(10),
),
child: TextField(
// onChanged: (value) => controller.filterSearchResults(value),
autofocus: false,
cursorColor: Color.fromARGB(255, 73, 73, 73),
style: const TextStyle(color: Colors.grey),
decoration: const InputDecoration(
hintText: 'Search',
hintStyle: TextStyle(color: Colors.grey),
prefixIcon: Icon(Icons.search, color: TestColor, size: 20),
fillColor: Colors.white,
focusedBorder: UnderlineInputBorder(
// borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(
color: Color.fromARGB(255, 255, 255, 255), width: 0),
),
enabledBorder: UnderlineInputBorder(
// borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(
color: Color.fromARGB(255, 255, 255, 255), width: 0),
),
),
),
),
const Gap(10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: GestureDetector(
onTap: () {
controller.changeStateShip('tanger');
},
child: Container(
// color: Colors.black38,
alignment: Alignment.center,
height: 40,
padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
decoration: BoxDecoration(
color: controller.tangerStore.value
? TestColor
: Colors.white,
border: Border.all(
color: controller.tangerStore.value
? Colors.white
: TestColor,
width: 1),
borderRadius: BorderRadius.circular(8),
),
child: Text(
'tanger',
style: TextStyle(
color: controller.tangerStore.value
? Colors.white
: TestColor,
fontWeight: FontWeight.w900,
fontSize: 15),
),
))),
const Gap(15),
Expanded(
child: GestureDetector(
onTap: () {
controller.changeStateShip('casa');
},
child: Container(
alignment: Alignment.center,
height: 40,
padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
decoration: BoxDecoration(
color: controller.casaStore.value
? TestColor
: Colors.white,
border: Border.all(
color: controller.casaStore.value
? Colors.white
: TestColor,
width: 1),
borderRadius: BorderRadius.circular(8),
),
child: Text(
'casa',
style: TextStyle(
color: controller.casaStore.value
? Colors.white
: TestColor,
fontWeight: FontWeight.w900,
fontSize: 15),
),
))),
]),
],
)));
}
}

0 comments on commit d463584

Please sign in to comment.