use selected model in add device event

This commit is contained in:
Rafeek Alkhoudare
2025-05-26 06:11:48 -05:00
parent c97dd40b05
commit 766a39f161
3 changed files with 28 additions and 35 deletions

View File

@ -17,7 +17,8 @@ class TagDialogTextfieldDropdown extends StatefulWidget {
}) : super(key: key); }) : super(key: key);
@override @override
_DialogTextfieldDropdownState createState() => _DialogTextfieldDropdownState(); _DialogTextfieldDropdownState createState() =>
_DialogTextfieldDropdownState();
} }
class _DialogTextfieldDropdownState extends State<TagDialogTextfieldDropdown> { class _DialogTextfieldDropdownState extends State<TagDialogTextfieldDropdown> {
@ -36,6 +37,12 @@ class _DialogTextfieldDropdownState extends State<TagDialogTextfieldDropdown> {
_focusNode.addListener(() { _focusNode.addListener(() {
if (!_focusNode.hasFocus) { if (!_focusNode.hasFocus) {
// Call onSelected when focus is lost
final selectedTag = _filteredItems.firstWhere(
(tag) => tag.tag == _controller.text,
orElse: () => Tag(tag: _controller.text),
);
widget.onSelected(selectedTag);
_closeDropdown(); _closeDropdown();
} }
}); });
@ -43,7 +50,9 @@ class _DialogTextfieldDropdownState extends State<TagDialogTextfieldDropdown> {
void _filterItems() { void _filterItems() {
setState(() { setState(() {
_filteredItems = widget.items.where((tag) => tag.product?.uuid == widget.product).toList(); _filteredItems = widget.items
.where((tag) => tag.product?.uuid == widget.product)
.toList();
}); });
} }
@ -112,7 +121,9 @@ class _DialogTextfieldDropdownState extends State<TagDialogTextfieldDropdown> {
style: Theme.of(context) style: Theme.of(context)
.textTheme .textTheme
.bodyMedium .bodyMedium
?.copyWith(color: ColorsManager.textPrimaryColor)), ?.copyWith(
color: ColorsManager
.textPrimaryColor)),
onTap: () { onTap: () {
_controller.text = tag.tag ?? ''; _controller.text = tag.tag ?? '';
widget.onSelected(tag); widget.onSelected(tag);
@ -156,13 +167,15 @@ class _DialogTextfieldDropdownState extends State<TagDialogTextfieldDropdown> {
controller: _controller, controller: _controller,
focusNode: _focusNode, focusNode: _focusNode,
onFieldSubmitted: (value) { onFieldSubmitted: (value) {
final selectedTag = _filteredItems.firstWhere((tag) => tag.tag == value, final selectedTag = _filteredItems.firstWhere(
(tag) => tag.tag == value,
orElse: () => Tag(tag: value)); orElse: () => Tag(tag: value));
widget.onSelected(selectedTag); widget.onSelected(selectedTag);
_closeDropdown(); _closeDropdown();
}, },
onTapOutside: (event) { onTapOutside: (event) {
widget.onSelected(_filteredItems.firstWhere((tag) => tag.tag == _controller.text, widget.onSelected(_filteredItems.firstWhere(
(tag) => tag.tag == _controller.text,
orElse: () => Tag(tag: _controller.text))); orElse: () => Tag(tag: _controller.text)));
_closeDropdown(); _closeDropdown();
}, },

View File

@ -24,37 +24,22 @@ class AddDeviceTypeModelBloc
if (currentState is AddDeviceModelLoaded) { if (currentState is AddDeviceModelLoaded) {
final existingProduct = currentState.selectedProducts.firstWhere( final existingProduct = currentState.selectedProducts.firstWhere(
(p) => p.productId == event.productId, (p) => p.productId == event.selectedProduct.productId,
orElse: () => SelectedProduct( orElse: () => event.selectedProduct,
productId: event.productId,
count: 0,
productName: event.productName,
product: event.product,
),
); );
List<SelectedProduct> updatedProducts; List<SelectedProduct> updatedProducts;
if (event.count > 0) { if (event.selectedProduct.count > 0) {
if (!currentState.selectedProducts.contains(existingProduct)) { if (!currentState.selectedProducts.contains(existingProduct)) {
updatedProducts = [ updatedProducts = [
...currentState.selectedProducts, ...currentState.selectedProducts,
SelectedProduct( event.selectedProduct,
productId: event.productId,
count: event.count,
productName: event.productName,
product: event.product,
),
]; ];
} else { } else {
updatedProducts = currentState.selectedProducts.map((p) { updatedProducts = currentState.selectedProducts.map((p) {
if (p.productId == event.productId) { if (p.productId == event.selectedProduct.productId) {
return SelectedProduct( return event.selectedProduct;
productId: p.productId,
count: event.count,
productName: p.productName,
product: p.product,
);
} }
return p; return p;
}).toList(); }).toList();
@ -62,7 +47,7 @@ class AddDeviceTypeModelBloc
} else { } else {
// Remove the product if the count is 0 // Remove the product if the count is 0
updatedProducts = currentState.selectedProducts updatedProducts = currentState.selectedProducts
.where((p) => p.productId != event.productId) .where((p) => p.productId != event.selectedProduct.productId)
.toList(); .toList();
} }

View File

@ -10,20 +10,15 @@ abstract class AddDeviceTypeModelEvent extends Equatable {
List<Object> get props => []; List<Object> get props => [];
} }
class UpdateProductCountEvent extends AddDeviceTypeModelEvent { class UpdateProductCountEvent extends AddDeviceTypeModelEvent {
final String productId; final SelectedProduct selectedProduct;
final int count;
final String productName;
final ProductModel product;
UpdateProductCountEvent({required this.productId, required this.count, required this.productName, required this.product}); UpdateProductCountEvent({required this.selectedProduct});
@override @override
List<Object> get props => [productId, count]; List<Object> get props => [selectedProduct];
} }
class InitializeDeviceTypeModel extends AddDeviceTypeModelEvent { class InitializeDeviceTypeModel extends AddDeviceTypeModelEvent {
final List<Tag> initialTags; final List<Tag> initialTags;
final List<SelectedProduct> addedProducts; final List<SelectedProduct> addedProducts;