add consume
This commit is contained in:
parent
310102e4ba
commit
86fb3d6370
4 changed files with 243 additions and 68 deletions
|
@ -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
144
lib/pages/consume.dart
Normal 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'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,30 +54,38 @@ 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(
|
||||||
|
onTap: () {
|
||||||
|
Navigator.of(context).push(MaterialPageRoute(
|
||||||
|
builder: (context) {
|
||||||
|
return ConsumePage(t.uuid, t.item, t.variant, this.refresh);
|
||||||
|
},
|
||||||
|
));
|
||||||
|
},
|
||||||
|
child: Card(
|
||||||
color: t.expired ? Colors.red[100] : Colors.white,
|
color: t.expired ? Colors.red[100] : Colors.white,
|
||||||
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
|
@ -130,6 +150,7 @@ class TransactionCard extends StatelessWidget {
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(
|
||||||
|
|
Loading…
Reference in a new issue