fix price auto suggest

This commit is contained in:
JMARyA 2024-09-20 13:10:26 +02:00
parent 7b4720f871
commit 35977e9a61
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -17,12 +17,13 @@ class _SupplyPageState extends State<SupplyPage> {
final _formKey = GlobalKey<FormState>();
String _selectedOrigin = "";
String _selectedLocation = "";
String _price = "";
late TextEditingController _priceController;
@override
void initState() {
super.initState();
variant = widget.item.variants.keys.first;
_priceController = TextEditingController(text: "");
}
void _supply() {
@ -30,8 +31,8 @@ class _SupplyPageState extends State<SupplyPage> {
_formKey.currentState!.save();
API()
.supplyItem(widget.item.name, variant, "$_price", _selectedOrigin,
_selectedLocation)
.supplyItem(widget.item.name, variant, "${_priceController.text}",
_selectedOrigin, _selectedLocation)
.then((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Item added successfully!')),
@ -96,14 +97,14 @@ class _SupplyPageState extends State<SupplyPage> {
_selectedOrigin = value;
},
onSelection: (String selection) async {
var price = _price.isEmpty
var price = _priceController.text.isEmpty
? await API()
.getLatestPrice(widget.item.id, variant,
origin: selection)
.then((x) => x.value.toStringAsFixed(2))
: _price;
: _priceController.text;
setState(() {
_price = price;
_priceController.text = price;
_selectedOrigin = selection;
});
},
@ -115,7 +116,7 @@ class _SupplyPageState extends State<SupplyPage> {
TextFormField(
decoration: const InputDecoration(labelText: 'Price'),
keyboardType: TextInputType.number,
initialValue: _price,
controller: _priceController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a price';
@ -125,9 +126,6 @@ class _SupplyPageState extends State<SupplyPage> {
}
return null;
},
onSaved: (value) {
_price = value!;
},
),
const SizedBox(height: 16),