cdb_ui/lib/pages/consume.dart

116 lines
3.4 KiB
Dart
Raw Normal View History

2024-09-07 22:24:13 +00:00
import 'package:cdb_ui/api.dart';
2024-09-17 14:33:55 +00:00
import 'package:cdb_ui/pages/supply.dart';
2024-09-07 22:24:13 +00:00
import 'package:flutter/material.dart';
class ConsumePage extends StatefulWidget {
2024-09-23 07:03:40 +00:00
final Transaction transaction;
2024-09-15 15:49:50 +00:00
final Function refresh;
2024-09-07 22:24:13 +00:00
2024-09-23 07:03:40 +00:00
const ConsumePage(this.transaction, this.refresh, {super.key});
2024-09-07 22:24:13 +00:00
@override
State<ConsumePage> createState() => _ConsumePageState();
}
class _ConsumePageState extends State<ConsumePage> {
final _formKey = GlobalKey<FormState>();
String _selectedDestination = "";
String _price = "";
@override
void initState() {
super.initState();
}
2024-09-16 07:42:55 +00:00
void _consume() {
2024-09-07 22:24:13 +00:00
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
2024-09-16 07:42:55 +00:00
API()
2024-09-23 07:03:40 +00:00
.consumeItem(
widget.transaction.uuid, _selectedDestination, "$_price")
2024-09-16 07:42:55 +00:00
.then((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Item consumed successfully!')),
);
Navigator.of(context).pop();
widget.refresh();
});
2024-09-07 22:24:13 +00:00
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Consume Item'),
),
body: FutureBuilder(
2024-09-23 07:03:40 +00:00
future: API().getUniqueField(
widget.transaction.item, widget.transaction.variant, "destination"),
2024-09-07 22:24:13 +00:00
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
2024-09-17 14:33:55 +00:00
AutocompletedTextField(
options: destinations,
getValue: () => _selectedDestination,
onChanged: (value) {
_selectedDestination = value;
},
onSelection: (selection) {
setState(() {
_selectedDestination = selection;
});
},
label: "Destination"),
2024-09-07 22:24:13 +00:00
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'),
),
],
),
),
);
},
),
);
}
}