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
.toLowerCase()
.contains(textEditingValue.text.toLowerCase());
});
}, },
onSelected: (String selection) { onSelection: (selection) {
setState(() { setState(() {
_selectedDestination = selection; _selectedDestination = selection;
}); });
}, },
fieldViewBuilder: (context, textEditingController, label: "Destination"),
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,18 +89,13 @@ 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
.toLowerCase()
.contains(textEditingValue.text.toLowerCase());
});
}, },
onSelected: (String selection) async { onSelection: (String selection) async {
var price = _price.isEmpty var price = _price.isEmpty
? await API() ? await API()
.getLatestPrice(widget.item.id, variant, .getLatestPrice(widget.item.id, variant,
@ -112,22 +107,7 @@ class _SupplyPageState extends State<SupplyPage> {
_selectedOrigin = selection; _selectedOrigin = selection;
}); });
}, },
fieldViewBuilder: (context, textEditingController, label: "Origin"),
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(),
),
);
},
);
}
}