fix(quick-sales): enable mobile product suggestions #39

Merged
morrning merged 1 commit from Seyyed/Seyyed_arc:codex/fix-quick-sales-mobile-suggestions into master 2026-10-31 21:39:45 +03:30
3 changed files with 83 additions and 27 deletions

View file

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:math' as math;
import 'package:dio/dio.dart' as dio; import 'package:dio/dio.dart' as dio;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -261,6 +262,7 @@ class _QuickSalesPageState extends State<QuickSalesPage>
final FocusNode _barcodeFocus = FocusNode(); final FocusNode _barcodeFocus = FocusNode();
final FocusNode _keyboardListenerFocus = FocusNode(); final FocusNode _keyboardListenerFocus = FocusNode();
final LayerLink _barcodeFieldLayerLink = LayerLink(); final LayerLink _barcodeFieldLayerLink = LayerLink();
final GlobalKey _barcodeFieldKey = GlobalKey();
final ScrollController _barcodeOverlayScrollController = ScrollController(); final ScrollController _barcodeOverlayScrollController = ScrollController();
OverlayEntry? _barcodeOverlayEntry; OverlayEntry? _barcodeOverlayEntry;
List<Map<String, dynamic>> _barcodeSuggestions = List<Map<String, dynamic>> _barcodeSuggestions =
@ -568,6 +570,10 @@ class _QuickSalesPageState extends State<QuickSalesPage>
if (_mobileTabController.indexIsChanging) return; if (_mobileTabController.indexIsChanging) return;
final index = _mobileTabController.index; final index = _mobileTabController.index;
if (_mobileTabIndex == index && _mobileTabsBuilt.contains(index)) return; if (_mobileTabIndex == index && _mobileTabsBuilt.contains(index)) return;
if (index != 0) {
_barcodeFocus.unfocus();
_clearBarcodeSuggestions();
}
setState(() { setState(() {
_mobileTabIndex = index; _mobileTabIndex = index;
_mobileTabsBuilt.add(index); _mobileTabsBuilt.add(index);
@ -3646,12 +3652,8 @@ class _QuickSalesPageState extends State<QuickSalesPage>
ctx.findAncestorWidgetOfExactType<EditableText>() != null; ctx.findAncestorWidgetOfExactType<EditableText>() != null;
} }
bool get _isDesktopLike =>
MediaQuery.sizeOf(context).width >=
ResponsiveHelper.shellPersistentNavMinWidth;
void _onBarcodeFocusChanged() { void _onBarcodeFocusChanged() {
if (!mounted || !_isDesktopLike) return; if (!mounted) return;
if (_barcodeFocus.hasFocus) { if (_barcodeFocus.hasFocus) {
final query = _barcodeController.text; final query = _barcodeController.text;
if (shouldShowProductSearchSuggestions(query)) { if (shouldShowProductSearchSuggestions(query)) {
@ -3667,7 +3669,7 @@ class _QuickSalesPageState extends State<QuickSalesPage>
} }
void _showBarcodeOverlay() { void _showBarcodeOverlay() {
if (!mounted || !_isDesktopLike) return; if (!mounted) return;
if (_barcodeOverlayEntry != null) { if (_barcodeOverlayEntry != null) {
_barcodeOverlayEntry!.markNeedsBuild(); _barcodeOverlayEntry!.markNeedsBuild();
return; return;
@ -3723,13 +3725,46 @@ class _QuickSalesPageState extends State<QuickSalesPage>
} }
Widget _buildBarcodeOverlay(BuildContext overlayContext) { 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( return Positioned.fill(
child: CompositedTransformFollower( child: CompositedTransformFollower(
link: _barcodeFieldLayerLink, link: _barcodeFieldLayerLink,
showWhenUnlinked: false, showWhenUnlinked: false,
targetAnchor: Alignment.bottomLeft, targetAnchor: showAbove ? Alignment.topLeft : Alignment.bottomLeft,
followerAnchor: Alignment.topLeft, followerAnchor: showAbove ? Alignment.bottomLeft : Alignment.topLeft,
offset: const Offset(0, 6), offset: Offset(0, showAbove ? -6 : 6),
child: Align( child: Align(
alignment: Alignment.topLeft, alignment: Alignment.topLeft,
child: TextFieldTapRegion( child: TextFieldTapRegion(
@ -3737,18 +3772,15 @@ class _QuickSalesPageState extends State<QuickSalesPage>
elevation: 10, elevation: 10,
borderRadius: BorderRadius.circular(10), borderRadius: BorderRadius.circular(10),
clipBehavior: Clip.antiAlias, clipBehavior: Clip.antiAlias,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 620, minWidth: 300),
child: SizedBox( child: SizedBox(
width: 620, width: overlayWidth,
height: _barcodeOverlayHeight(), height: overlayHeight,
child: _buildBarcodeSuggestionList(overlayContext), child: _buildBarcodeSuggestionList(overlayContext),
), ),
), ),
), ),
), ),
), ),
),
); );
} }
@ -3839,7 +3871,6 @@ class _QuickSalesPageState extends State<QuickSalesPage>
} }
void _scheduleBarcodeSuggestionSearch(String rawQuery) { void _scheduleBarcodeSuggestionSearch(String rawQuery) {
if (!_isDesktopLike) return;
_searchDebounce?.cancel(); _searchDebounce?.cancel();
final query = rawQuery.trim(); final query = rawQuery.trim();
_barcodeSuggestionNavigatedByKeyboard = false; _barcodeSuggestionNavigatedByKeyboard = false;
@ -3866,7 +3897,7 @@ class _QuickSalesPageState extends State<QuickSalesPage>
} }
Future<void> _loadBarcodeSuggestions(String query) async { Future<void> _loadBarcodeSuggestions(String query) async {
if (!mounted || !_isDesktopLike) return; if (!mounted) return;
if (query.isEmpty) { if (query.isEmpty) {
_clearBarcodeSuggestions(); _clearBarcodeSuggestions();
return; return;
@ -5650,6 +5681,7 @@ class _QuickSalesPageState extends State<QuickSalesPage>
); );
Widget searchField = CompositedTransformTarget( Widget searchField = CompositedTransformTarget(
key: _barcodeFieldKey,
link: _barcodeFieldLayerLink, link: _barcodeFieldLayerLink,
child: TextField( child: TextField(
controller: _barcodeController, controller: _barcodeController,
@ -5733,6 +5765,10 @@ class _QuickSalesPageState extends State<QuickSalesPage>
suggestionCount: _barcodeSuggestions.length, suggestionCount: _barcodeSuggestions.length,
hasMoreSuggestions: _barcodeSuggestionsHasMore, hasMoreSuggestions: _barcodeSuggestionsHasMore,
navigatedByKeyboard: _barcodeSuggestionNavigatedByKeyboard, 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) { switch (action) {
case QuickSalesProductSearchSubmitAction.selectSuggestion: case QuickSalesProductSearchSubmitAction.selectSuggestion:
@ -5754,7 +5790,6 @@ class _QuickSalesPageState extends State<QuickSalesPage>
_lastFailedSearchQuery = null; _lastFailedSearchQuery = null;
}); });
} }
if (_isDesktopLike) {
if (!shouldShowProductSearchSuggestions(value)) { if (!shouldShowProductSearchSuggestions(value)) {
_clearBarcodeSuggestions(); _clearBarcodeSuggestions();
} else { } else {
@ -5763,7 +5798,6 @@ class _QuickSalesPageState extends State<QuickSalesPage>
} }
_scheduleBarcodeSuggestionSearch(value); _scheduleBarcodeSuggestionSearch(value);
} }
}
}, },
textInputAction: TextInputAction.search, textInputAction: TextInputAction.search,
), ),

View file

@ -1,4 +1,4 @@
/// سیاست نمایش و ثبت پیشنهادهای جست‌وجوی کالا در فیلدهای دسکتاپ. /// سیاست نمایش و ثبت پیشنهادهای جست‌وجوی کالا در فیلدهای واکنش‌گرا.
/// ///
/// منوی پیشنهاد فقط پس از ورود متن باز می‌شود. در فروش سریع، Enter پیشنهاد /// منوی پیشنهاد فقط پس از ورود متن باز می‌شود. در فروش سریع، Enter پیشنهاد
/// انتخاب‌شده با صفحه‌کلید یا تنها نتیجهٔ موجود را ثبت می‌کند. اگر نتایج جاری /// انتخاب‌شده با صفحه‌کلید یا تنها نتیجهٔ موجود را ثبت می‌کند. اگر نتایج جاری
@ -18,12 +18,17 @@ QuickSalesProductSearchSubmitAction resolveQuickSalesProductSearchSubmitAction({
required int suggestionCount, required int suggestionCount,
required bool hasMoreSuggestions, required bool hasMoreSuggestions,
required bool navigatedByKeyboard, required bool navigatedByKeyboard,
bool preferDirectSearch = false,
}) { }) {
final normalizedInput = input.trim(); final normalizedInput = input.trim();
if (normalizedInput.isEmpty) { if (normalizedInput.isEmpty) {
return QuickSalesProductSearchSubmitAction.waitForSuggestionSelection; return QuickSalesProductSearchSubmitAction.waitForSuggestionSelection;
} }
if (preferDirectSearch) {
return QuickSalesProductSearchSubmitAction.searchField;
}
final suggestionsBelongToInput = final suggestionsBelongToInput =
suggestionCount > 0 && loadedQuery.trim() == normalizedInput; suggestionCount > 0 && loadedQuery.trim() == normalizedInput;
if (!suggestionsBelongToInput) { if (!suggestionsBelongToInput) {

View file

@ -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', () { test('commits the highlighted row after keyboard navigation', () {
expect( expect(
resolveQuickSalesProductSearchSubmitAction( resolveQuickSalesProductSearchSubmitAction(