简体   繁体   中英

Get new texfield input with bar code scanner flutter

Working on a desktop application. I have an external bar code scanner which scans bar code and writes the value in any text field.

Now I have a textfield which does 2 job, search by name and search by barcode.

  Widget _buildSearch() {
    return Container(
      height: 35,
      width: MediaQuery.of(context).size.width / 6,
      padding: EdgeInsets.symmetric(horizontal: 10),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
      ),
      child: TextField(
        textAlignVertical: TextAlignVertical.center,
        controller: _searchController,
        keyboardType: TextInputType.text,
        autofocus: true,
        focusNode: _searchFocusNode,
        decoration: InputDecoration(
          border: InputBorder.none,
          hintText: "Search",
          hintStyle: TextStyle(
            color: Colors.grey,
          ),
        ),
        onChanged: (value) {
          _handleOnChanged(value);
        },
        onSubmitted: (value) {
          
        },
      ),
    );
  }

_handleOnChanged is simple:

  _handleOnChanged(String value) {
    setState(() {
      _isSearching = true;
    });
    _products = _productsList.search(value); //_products is a List<Product>
    if (value.isEmpty) {
      setState(() {
        _isSearching = false;
      });
    }
  }

What I want is to add some sort of listener to the textfield so whenever the user scans a new search will be initiated.

SO let's say the user scans a bar code whose value is GR13 , a product has been found. Now if the user scans again the textfield should be cleared and the new bar code will be scanned.

How can I make this happen?

normally the scanners will send submit signal after reading the code, which in your case is 'Enter'. so you could implement onSubmitted to pass the value or call a method that will get _searchController.text

then it will initiate a _searchController.clear()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM