This commit is contained in:
JMARyA 2024-09-17 16:33:55 +02:00
parent 47876a8c7c
commit ca68b8e951
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 79 additions and 78 deletions

View file

@ -1,4 +1,5 @@
import 'package:cdb_ui/api.dart'; import 'package:cdb_ui/api.dart';
import 'package:cdb_ui/pages/supply.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class ConsumePage extends StatefulWidget { class ConsumePage extends StatefulWidget {
@ -64,45 +65,18 @@ class _ConsumePageState extends State<ConsumePage> {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
// Destination Field with Dropdown and Text Input // Destination Field with Dropdown and Text Input
Autocomplete<String>( AutocompletedTextField(
optionsBuilder: (TextEditingValue textEditingValue) { options: destinations,
if (textEditingValue.text.isEmpty) { getValue: () => _selectedDestination,
return destinations; onChanged: (value) {
} _selectedDestination = value;
return destinations.where((String option) { },
return option onSelection: (selection) {
.toLowerCase() setState(() {
.contains(textEditingValue.text.toLowerCase()); _selectedDestination = selection;
}); });
}, },
onSelected: (String selection) { label: "Destination"),
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), const SizedBox(height: 16),

View file

@ -89,45 +89,25 @@ 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
Autocomplete<String>( AutocompletedTextField(
optionsBuilder: (TextEditingValue textEditingValue) { options: origins,
if (textEditingValue.text.isEmpty) { getValue: () => _selectedOrigin,
return origins; onChanged: (value) {
} _selectedOrigin = value;
return origins.where((String option) { },
return option onSelection: (String selection) async {
.toLowerCase() var price = _price.isEmpty
.contains(textEditingValue.text.toLowerCase()); ? await API()
}); .getLatestPrice(widget.item.id, variant,
}, origin: selection)
onSelected: (String selection) async { .then((x) => x.value.toStringAsFixed(2))
var price = _price.isEmpty : _price;
? await API() setState(() {
.getLatestPrice(widget.item.id, variant, _price = price;
origin: selection) _selectedOrigin = selection;
.then((x) => x.value.toStringAsFixed(2)) });
: _price; },
setState(() { label: "Origin"),
_price = price;
_selectedOrigin = selection;
});
},
fieldViewBuilder: (context, textEditingController,
focusNode, onFieldSubmitted) {
textEditingController.text = _selectedOrigin;
return TextFormField(
onChanged: (value) {
_selectedOrigin = value;
},
controller: textEditingController,
focusNode: focusNode,
decoration: const InputDecoration(
labelText: 'Origin',
border: OutlineInputBorder(),
),
);
},
),
const SizedBox(height: 16), const SizedBox(height: 16),
@ -220,3 +200,50 @@ class _SupplyPageState extends State<SupplyPage> {
return {'locations': locations, 'origins': origins}; return {'locations': locations, 'origins': origins};
} }
} }
// ignore: must_be_immutable
class AutocompletedTextField extends StatelessWidget {
late List<String> options;
late Function(String) onChanged;
late String Function() getValue;
late Function(String) onSelection;
late String label;
AutocompletedTextField(
{super.key,
required this.options,
required this.getValue,
required this.onChanged,
required this.onSelection,
required this.label});
@override
Widget build(BuildContext context) {
return Autocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text.isEmpty) {
return options;
}
return options.where((String option) {
return option
.toLowerCase()
.contains(textEditingValue.text.toLowerCase());
});
},
onSelected: onSelection,
fieldViewBuilder:
(context, textEditingController, focusNode, onFieldSubmitted) {
textEditingController.text = getValue();
return TextFormField(
onChanged: onChanged,
controller: textEditingController,
focusNode: focusNode,
decoration: InputDecoration(
labelText: label,
border: const OutlineInputBorder(),
),
);
},
);
}
}