add consume

This commit is contained in:
JMARyA 2024-09-08 00:24:13 +02:00
parent 310102e4ba
commit 86fb3d6370
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 243 additions and 68 deletions

View file

@ -96,6 +96,12 @@ class API {
return resp["uuid"]; return resp["uuid"];
} }
Future<void> consumeItem(
String transaction, String destination, String price) async {
await postRequest("$instance/demand",
{"uuid": transaction, "destination": destination, "price": price});
}
String getImageURL(String item) { String getImageURL(String item) {
return "$instance/$item/image"; return "$instance/$item/image";
} }

144
lib/pages/consume.dart Normal file
View file

@ -0,0 +1,144 @@
import 'package:cdb_ui/api.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'supply.dart';
class ConsumePage extends StatefulWidget {
final String item;
final String variant;
final String transaction;
Function refresh;
ConsumePage(this.transaction, this.item, this.variant, this.refresh,
{super.key});
@override
State<ConsumePage> createState() => _ConsumePageState();
}
class _ConsumePageState extends State<ConsumePage> {
final _formKey = GlobalKey<FormState>();
String _selectedDestination = "";
String _price = "";
@override
void initState() {
super.initState();
}
Future<void> _consume() async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
await API()
.consumeItem(widget.transaction, _selectedDestination, "${_price}");
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Item consumed successfully!')),
);
Navigator.of(context).pop();
widget.refresh();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Consume Item'),
),
body: FutureBuilder(
future:
API().getUniqueField(widget.item, widget.variant, "destination"),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
var destinations = snapshot.data!;
return Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Destination Field with Dropdown and Text Input
Autocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text.isEmpty) {
return destinations;
}
return destinations.where((String option) {
return option
.toLowerCase()
.contains(textEditingValue.text.toLowerCase());
});
},
onSelected: (String selection) {
setState(() {
_selectedDestination = selection;
});
},
fieldViewBuilder: (context, textEditingController,
focusNode, onFieldSubmitted) {
textEditingController.text = _selectedDestination;
return TextFormField(
onChanged: (value) {
_selectedDestination = value;
},
controller: textEditingController,
focusNode: focusNode,
decoration: const InputDecoration(
labelText: 'Destination',
border: OutlineInputBorder(),
),
validator: (value) {
if (value?.isEmpty ?? true) {
return "Please enter a destination";
}
return null;
},
);
},
),
const SizedBox(height: 16),
// Price Field
TextFormField(
decoration: const InputDecoration(labelText: 'Price'),
keyboardType: TextInputType.number,
initialValue: _price,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a price';
}
if (double.tryParse(value) == null) {
return 'Please enter a valid number';
}
return null;
},
onSaved: (value) {
_price = value!;
},
),
const SizedBox(height: 20),
// Submit Button
ElevatedButton(
onPressed: _consume,
child: const Text('Consume Item'),
),
],
),
),
);
},
),
);
}
}

View file

@ -1,11 +1,23 @@
import 'package:cdb_ui/api.dart'; import 'package:cdb_ui/api.dart';
import 'package:cdb_ui/pages/consume.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'supply.dart'; import 'supply.dart';
class ItemView extends StatelessWidget { class ItemView extends StatefulWidget {
final Item item; final Item item;
@override
State<ItemView> createState() => _ItemViewState();
const ItemView({super.key, required this.item});
}
class _ItemViewState extends State<ItemView> {
void refresh() {
setState(() {});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -19,21 +31,21 @@ class ItemView extends StatelessWidget {
Column( Column(
children: [ children: [
Text( Text(
item.name, widget.item.name,
style: const TextStyle(fontWeight: FontWeight.bold), style: const TextStyle(fontWeight: FontWeight.bold),
), ),
Text(item.category) Text(widget.item.category)
], ],
) )
], ],
), ),
const SizedBox(height: 10), const SizedBox(height: 10),
Row( Row(
children: item.variants.entries.map((entry) { children: widget.item.variants.entries.map((entry) {
return Text(entry.value.name); return Text(entry.value.name);
}).toList()), }).toList()),
FutureBuilder( FutureBuilder(
future: API().getInventory(item.id), future: API().getInventory(widget.item.id),
builder: (context, snapshot) { builder: (context, snapshot) {
if (!snapshot.hasData) { if (!snapshot.hasData) {
return const CircularProgressIndicator(); return const CircularProgressIndicator();
@ -42,92 +54,101 @@ class ItemView extends StatelessWidget {
return Expanded( return Expanded(
child: ListView( child: ListView(
children: data.map((x) => TransactionCard(x)).toList())); children:
data.map((x) => TransactionCard(x, refresh)).toList()));
}, },
) )
]), ]),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: () { onPressed: () {
Navigator.of(context).push( Navigator.of(context).push(MaterialPageRoute(
MaterialPageRoute(builder: (context) => SupplyPage(item))); builder: (context) => SupplyPage(widget.item, refresh)));
}, },
child: const Icon(Icons.add)), child: const Icon(Icons.add)),
); );
} }
const ItemView({super.key, required this.item});
} }
class TransactionCard extends StatelessWidget { class TransactionCard extends StatelessWidget {
final Transaction t; final Transaction t;
Function refresh;
const TransactionCard(this.t, {super.key}); TransactionCard(this.t, this.refresh, {super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Card( return InkWell(
color: t.expired ? Colors.red[100] : Colors.white, onTap: () {
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16), Navigator.of(context).push(MaterialPageRoute(
shape: RoundedRectangleBorder( builder: (context) {
borderRadius: BorderRadius.circular(10), return ConsumePage(t.uuid, t.item, t.variant, this.refresh);
), },
elevation: 4, ));
child: Padding( },
padding: EdgeInsets.all(12), child: Card(
child: Column( color: t.expired ? Colors.red[100] : Colors.white,
crossAxisAlignment: CrossAxisAlignment.start, margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
children: [ shape: RoundedRectangleBorder(
Row( borderRadius: BorderRadius.circular(10),
mainAxisAlignment: MainAxisAlignment.spaceBetween, ),
children: [ elevation: 4,
Row( child: Padding(
children: [ padding: EdgeInsets.all(12),
Text( child: Column(
t.item, crossAxisAlignment: CrossAxisAlignment.start,
style: TextStyle(fontSize: 16), children: [
),
SizedBox(
width: 4,
),
Text(
t.variant,
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
),
],
),
Text(
tsFormat(t.timestamp),
style: TextStyle(fontSize: 14, color: Colors.grey[700]),
),
],
),
SizedBox(
height: 10,
),
Row(
children: [
Icon(Icons.money, size: 18, color: Colors.green),
SizedBox(width: 6),
Text(
"${t.price.value} ${t.price.currency}",
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
],
),
if (t.origin != null) ...[
SizedBox(height: 8),
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
Icon(Icons.store, size: 18, color: Colors.blue), Row(
SizedBox(width: 6), children: [
Text(
t.item,
style: TextStyle(fontSize: 16),
),
SizedBox(
width: 4,
),
Text(
t.variant,
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
),
],
),
Text( Text(
t.origin!, tsFormat(t.timestamp),
style: TextStyle(fontSize: 14, color: Colors.grey[700]), style: TextStyle(fontSize: 14, color: Colors.grey[700]),
), ),
], ],
), ),
SizedBox(
height: 10,
),
Row(
children: [
Icon(Icons.money, size: 18, color: Colors.green),
SizedBox(width: 6),
Text(
"${t.price.value} ${t.price.currency}",
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
],
),
if (t.origin != null) ...[
SizedBox(height: 8),
Row(
children: [
Icon(Icons.store, size: 18, color: Colors.blue),
SizedBox(width: 6),
Text(
t.origin!,
style: TextStyle(fontSize: 14, color: Colors.grey[700]),
),
],
),
],
], ],
], ),
), ),
), ),
); );

View file

@ -4,8 +4,9 @@ import 'package:qr_bar_code_scanner_dialog/qr_bar_code_scanner_dialog.dart';
class SupplyPage extends StatefulWidget { class SupplyPage extends StatefulWidget {
final Item item; final Item item;
Function refresh;
const SupplyPage(this.item, {super.key}); SupplyPage(this.item, this.refresh, {super.key});
@override @override
State<SupplyPage> createState() => _SupplyPageState(); State<SupplyPage> createState() => _SupplyPageState();
@ -35,6 +36,7 @@ class _SupplyPageState extends State<SupplyPage> {
const SnackBar(content: Text('Item added successfully!')), const SnackBar(content: Text('Item added successfully!')),
); );
Navigator.of(context).pop(); Navigator.of(context).pop();
widget.refresh();
} }
} }
@ -85,7 +87,6 @@ class _SupplyPageState extends State<SupplyPage> {
const SizedBox(height: 16), const SizedBox(height: 16),
// Origin Field with Dropdown and Text Input // Origin Field with Dropdown and Text Input
// todo : fix state
Autocomplete<String>( Autocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) { optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text.isEmpty) { if (textEditingValue.text.isEmpty) {
@ -106,6 +107,9 @@ class _SupplyPageState extends State<SupplyPage> {
focusNode, onFieldSubmitted) { focusNode, onFieldSubmitted) {
textEditingController.text = _selectedOrigin; textEditingController.text = _selectedOrigin;
return TextFormField( return TextFormField(
onChanged: (value) {
_selectedOrigin = value;
},
controller: textEditingController, controller: textEditingController,
focusNode: focusNode, focusNode: focusNode,
decoration: const InputDecoration( decoration: const InputDecoration(