From a81dacb3d02603685938d6f921f895088765dddd Mon Sep 17 00:00:00 2001 From: Seyyed Date: Sat, 31 Oct 2026 17:57:16 +0330 Subject: [PATCH] fix(quick-sales): enable mobile product suggestions --- .../lib/pages/business/quick_sales_page.dart | 86 +++++++++++++------ .../utils/product_search_overlay_policy.dart | 7 +- .../product_search_overlay_policy_test.dart | 17 ++++ 3 files changed, 83 insertions(+), 27 deletions(-) diff --git a/hesabixUI/hesabix_ui/lib/pages/business/quick_sales_page.dart b/hesabixUI/hesabix_ui/lib/pages/business/quick_sales_page.dart index ed515c54..59a677bd 100755 --- a/hesabixUI/hesabix_ui/lib/pages/business/quick_sales_page.dart +++ b/hesabixUI/hesabix_ui/lib/pages/business/quick_sales_page.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:math' as math; import 'package:dio/dio.dart' as dio; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -261,6 +262,7 @@ class _QuickSalesPageState extends State final FocusNode _barcodeFocus = FocusNode(); final FocusNode _keyboardListenerFocus = FocusNode(); final LayerLink _barcodeFieldLayerLink = LayerLink(); + final GlobalKey _barcodeFieldKey = GlobalKey(); final ScrollController _barcodeOverlayScrollController = ScrollController(); OverlayEntry? _barcodeOverlayEntry; List> _barcodeSuggestions = @@ -568,6 +570,10 @@ class _QuickSalesPageState extends State if (_mobileTabController.indexIsChanging) return; final index = _mobileTabController.index; if (_mobileTabIndex == index && _mobileTabsBuilt.contains(index)) return; + if (index != 0) { + _barcodeFocus.unfocus(); + _clearBarcodeSuggestions(); + } setState(() { _mobileTabIndex = index; _mobileTabsBuilt.add(index); @@ -3646,12 +3652,8 @@ class _QuickSalesPageState extends State ctx.findAncestorWidgetOfExactType() != null; } - bool get _isDesktopLike => - MediaQuery.sizeOf(context).width >= - ResponsiveHelper.shellPersistentNavMinWidth; - void _onBarcodeFocusChanged() { - if (!mounted || !_isDesktopLike) return; + if (!mounted) return; if (_barcodeFocus.hasFocus) { final query = _barcodeController.text; if (shouldShowProductSearchSuggestions(query)) { @@ -3667,7 +3669,7 @@ class _QuickSalesPageState extends State } void _showBarcodeOverlay() { - if (!mounted || !_isDesktopLike) return; + if (!mounted) return; if (_barcodeOverlayEntry != null) { _barcodeOverlayEntry!.markNeedsBuild(); return; @@ -3723,13 +3725,46 @@ class _QuickSalesPageState extends State } Widget _buildBarcodeOverlay(BuildContext overlayContext) { + final mediaQuery = MediaQuery.of(overlayContext); + final viewportSize = mediaQuery.size; + final fieldRenderObject = _barcodeFieldKey.currentContext + ?.findRenderObject(); + final fieldBox = fieldRenderObject is RenderBox && fieldRenderObject.hasSize + ? fieldRenderObject + : null; + final fieldSize = fieldBox?.size; + final fieldTopLeft = fieldBox?.localToGlobal(Offset.zero); + final fallbackWidth = math.max(0.0, viewportSize.width - 24).toDouble(); + final overlayWidth = math + .min( + 620.0, + fieldSize != null && fieldSize.width > 0 + ? fieldSize.width + : fallbackWidth, + ) + .toDouble(); + final desiredHeight = _barcodeOverlayHeight(); + final keyboardTop = viewportSize.height - mediaQuery.viewInsets.bottom; + final fieldTop = fieldTopLeft?.dy ?? mediaQuery.padding.top; + final fieldBottom = fieldTop + (fieldSize?.height ?? 56); + final spaceBelow = math.max(0.0, keyboardTop - fieldBottom - 12).toDouble(); + final spaceAbove = math + .max(0.0, fieldTop - mediaQuery.padding.top - 12) + .toDouble(); + final showAbove = + spaceBelow < math.min(140.0, desiredHeight) && spaceAbove > spaceBelow; + final availableHeight = showAbove ? spaceAbove : spaceBelow; + final overlayHeight = math + .min(desiredHeight, math.max(90.0, availableHeight)) + .toDouble(); + return Positioned.fill( child: CompositedTransformFollower( link: _barcodeFieldLayerLink, showWhenUnlinked: false, - targetAnchor: Alignment.bottomLeft, - followerAnchor: Alignment.topLeft, - offset: const Offset(0, 6), + targetAnchor: showAbove ? Alignment.topLeft : Alignment.bottomLeft, + followerAnchor: showAbove ? Alignment.bottomLeft : Alignment.topLeft, + offset: Offset(0, showAbove ? -6 : 6), child: Align( alignment: Alignment.topLeft, child: TextFieldTapRegion( @@ -3737,13 +3772,10 @@ class _QuickSalesPageState extends State elevation: 10, borderRadius: BorderRadius.circular(10), clipBehavior: Clip.antiAlias, - child: ConstrainedBox( - constraints: const BoxConstraints(maxWidth: 620, minWidth: 300), - child: SizedBox( - width: 620, - height: _barcodeOverlayHeight(), - child: _buildBarcodeSuggestionList(overlayContext), - ), + child: SizedBox( + width: overlayWidth, + height: overlayHeight, + child: _buildBarcodeSuggestionList(overlayContext), ), ), ), @@ -3839,7 +3871,6 @@ class _QuickSalesPageState extends State } void _scheduleBarcodeSuggestionSearch(String rawQuery) { - if (!_isDesktopLike) return; _searchDebounce?.cancel(); final query = rawQuery.trim(); _barcodeSuggestionNavigatedByKeyboard = false; @@ -3866,7 +3897,7 @@ class _QuickSalesPageState extends State } Future _loadBarcodeSuggestions(String query) async { - if (!mounted || !_isDesktopLike) return; + if (!mounted) return; if (query.isEmpty) { _clearBarcodeSuggestions(); return; @@ -5650,6 +5681,7 @@ class _QuickSalesPageState extends State ); Widget searchField = CompositedTransformTarget( + key: _barcodeFieldKey, link: _barcodeFieldLayerLink, child: TextField( controller: _barcodeController, @@ -5733,6 +5765,10 @@ class _QuickSalesPageState extends State suggestionCount: _barcodeSuggestions.length, hasMoreSuggestions: _barcodeSuggestionsHasMore, navigatedByKeyboard: _barcodeSuggestionNavigatedByKeyboard, + // Hardware scanners type the whole barcode and press Enter before + // the suggestion debounce settles. The current field must win over + // any previously loaded/highlighted suggestion in that window. + preferDirectSearch: _searchDebounce?.isActive == true, ); switch (action) { case QuickSalesProductSearchSubmitAction.selectSuggestion: @@ -5754,15 +5790,13 @@ class _QuickSalesPageState extends State _lastFailedSearchQuery = null; }); } - if (_isDesktopLike) { - if (!shouldShowProductSearchSuggestions(value)) { - _clearBarcodeSuggestions(); - } else { - if (_barcodeOverlayEntry == null && _barcodeFocus.hasFocus) { - _showBarcodeOverlay(); - } - _scheduleBarcodeSuggestionSearch(value); + if (!shouldShowProductSearchSuggestions(value)) { + _clearBarcodeSuggestions(); + } else { + if (_barcodeOverlayEntry == null && _barcodeFocus.hasFocus) { + _showBarcodeOverlay(); } + _scheduleBarcodeSuggestionSearch(value); } }, textInputAction: TextInputAction.search, diff --git a/hesabixUI/hesabix_ui/lib/utils/product_search_overlay_policy.dart b/hesabixUI/hesabix_ui/lib/utils/product_search_overlay_policy.dart index dce15ada..e06eaa0e 100644 --- a/hesabixUI/hesabix_ui/lib/utils/product_search_overlay_policy.dart +++ b/hesabixUI/hesabix_ui/lib/utils/product_search_overlay_policy.dart @@ -1,4 +1,4 @@ -/// سیاست نمایش و ثبت پیشنهادهای جست‌وجوی کالا در فیلدهای دسکتاپ. +/// سیاست نمایش و ثبت پیشنهادهای جست‌وجوی کالا در فیلدهای واکنش‌گرا. /// /// منوی پیشنهاد فقط پس از ورود متن باز می‌شود. در فروش سریع، Enter پیشنهاد /// انتخاب‌شده با صفحه‌کلید یا تنها نتیجهٔ موجود را ثبت می‌کند. اگر نتایج جاری @@ -18,12 +18,17 @@ QuickSalesProductSearchSubmitAction resolveQuickSalesProductSearchSubmitAction({ required int suggestionCount, required bool hasMoreSuggestions, required bool navigatedByKeyboard, + bool preferDirectSearch = false, }) { final normalizedInput = input.trim(); if (normalizedInput.isEmpty) { return QuickSalesProductSearchSubmitAction.waitForSuggestionSelection; } + if (preferDirectSearch) { + return QuickSalesProductSearchSubmitAction.searchField; + } + final suggestionsBelongToInput = suggestionCount > 0 && loadedQuery.trim() == normalizedInput; if (!suggestionsBelongToInput) { diff --git a/hesabixUI/hesabix_ui/test/product_search_overlay_policy_test.dart b/hesabixUI/hesabix_ui/test/product_search_overlay_policy_test.dart index 68227195..db1bd595 100644 --- a/hesabixUI/hesabix_ui/test/product_search_overlay_policy_test.dart +++ b/hesabixUI/hesabix_ui/test/product_search_overlay_policy_test.dart @@ -28,6 +28,23 @@ void main() { ); }); + test( + 'rapid scanner Enter prefers the current barcode over suggestions', + () { + expect( + resolveQuickSalesProductSearchSubmitAction( + input: '6260151234567', + loadedQuery: '6260151234567', + suggestionCount: 3, + hasMoreSuggestions: false, + navigatedByKeyboard: true, + preferDirectSearch: true, + ), + QuickSalesProductSearchSubmitAction.searchField, + ); + }, + ); + test('commits the highlighted row after keyboard navigation', () { expect( resolveQuickSalesProductSearchSubmitAction( -- 2.43.0