diff --git a/lib/widget/search.dart b/lib/widget/search.dart index c8078187..ccfb1802 100644 --- a/lib/widget/search.dart +++ b/lib/widget/search.dart @@ -36,6 +36,8 @@ class _SearchDisplayState extends RefreshableState { _SearchDisplayState(this.hasAppBar) : super(); + final _formKey = GlobalKey(); + final bool hasAppBar; @override @@ -105,6 +107,9 @@ class _SearchDisplayState extends RefreshableState { } else { debounceTimer = Timer(Duration(milliseconds: 250), () { search(text); + if (!_focusNode.hasFocus) { + _focusNode.requestFocus(); + } }); } } @@ -120,6 +125,10 @@ class _SearchDisplayState extends RefreshableState { var api = InvenTreeAPI(); + if (!mounted) { + return; + } + setState(() { // Do not search on an empty string nPartResults = 0; @@ -140,10 +149,12 @@ class _SearchDisplayState extends RefreshableState { if (api.checkPermission("part", "view")) { InvenTreePart().count(searchQuery: term).then((int n) { if (term == searchController.text) { - setState(() { - nPartResults = n; - nSearchResults++; - }); + if (mounted) { + setState(() { + nPartResults = n; + nSearchResults++; + }); + } } }); } @@ -152,10 +163,12 @@ class _SearchDisplayState extends RefreshableState { if (api.checkPermission("part_category", "view")) { InvenTreePartCategory().count(searchQuery: term,).then((int n) { if (term == searchController.text) { - setState(() { - nCategoryResults = n; - nSearchResults++; - }); + if (mounted) { + setState(() { + nCategoryResults = n; + nSearchResults++; + }); + } } }); } @@ -164,10 +177,12 @@ class _SearchDisplayState extends RefreshableState { if (api.checkPermission("stock", "view")) { InvenTreeStockItem().count(searchQuery: term).then((int n) { if (term == searchController.text) { - setState(() { - nStockResults = n; - nSearchResults++; - }); + if (mounted) { + setState(() { + nStockResults = n; + nSearchResults++; + }); + } } }); } @@ -176,11 +191,12 @@ class _SearchDisplayState extends RefreshableState { if (api.checkPermission("stock_location", "view")) { InvenTreeStockLocation().count(searchQuery: term).then((int n) { if (term == searchController.text) { - setState(() { - nLocationResults = n; - - nSearchResults++; - }); + if (mounted) { + setState(() { + nLocationResults = n; + nSearchResults++; + }); + } } }); } @@ -209,10 +225,12 @@ class _SearchDisplayState extends RefreshableState { } ).then((int n) { if (term == searchController.text) { - setState(() { - nPurchaseOrderResults = n; - nSearchResults++; - }); + if (mounted) { + setState(() { + nPurchaseOrderResults = n; + nSearchResults++; + }); + } } }); } @@ -229,13 +247,18 @@ class _SearchDisplayState extends RefreshableState { decoration: InputDecoration( hintText: L10().queryEmpty, ), + key: _formKey, readOnly: false, - autofocus: true, + autofocus: false, autocorrect: false, focusNode: _focusNode, controller: searchController, onChanged: (String text) { onSearchTextChanged(text); + _focusNode.requestFocus(); + }, + onFieldSubmitted: (String text) { + _focusNode.requestFocus(); }, ), trailing: GestureDetector( @@ -245,6 +268,7 @@ class _SearchDisplayState extends RefreshableState { ), onTap: () { searchController.clear(); + _focusNode.requestFocus(); onSearchTextChanged("", immediate: true); }, ),