2024-09-07 22:24:13 +00:00
|
|
|
import 'package:cdb_ui/api.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class ConsumePage extends StatefulWidget {
|
|
|
|
final String item;
|
|
|
|
final String variant;
|
|
|
|
final String transaction;
|
2024-09-15 15:49:50 +00:00
|
|
|
final Function refresh;
|
2024-09-07 22:24:13 +00:00
|
|
|
|
2024-09-15 15:49:50 +00:00
|
|
|
const ConsumePage(this.transaction, this.item, this.variant, this.refresh,
|
2024-09-07 22:24:13 +00:00
|
|
|
{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();
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
.consumeItem(widget.transaction, _selectedDestination, "$_price €")
|
|
|
|
.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(
|
|
|
|
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'),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|