Flutter: How to detect delete key is pressed on TextField

5,711

Solution 1

The best solution is to wrap your TextField with RawKeyboardListener, like so:

RawKeyboardListener(
      focusNode: _focusNode, // or FocusNode()
      onKey: (event){
        if (event.logicalKey == LogicalKeyboardKey.backspace) {
          // here you can check if textfield is focused
          print('backspace clicked');
        }
        
      },
      child: TextField()
)

You can place any widget inside RawKeyboardListener instead on TextField.

Please refer to this link, Good luck!

Solution 2

You can declare a variable globally;

var val = "";

And then in your onChanged method;

onChanged: (value) {
    if(!val.contains(value))
     updateSearchQuery(query);
    val = value;
  },

You can use it like this. When val contains value(like "aabb" and "aab") do not search.

Share:
5,711
Paresh Mangukiya
Author by

Paresh Mangukiya

I am a Mobile App Developer and Team Leader at BlueBit Technologies, in this My core competency lies in the complete end-to-end management and completing the entire project (iOS, Android and Backend side). I am seeking opportunities to build Mobile Apps from the ground up for you or your business. Please feel free to contact me here ⬇ Skype: pkmangukiya_1 Mobile or WhatsApp: +91 9723680658 Email: [email protected] I have spent 4+ years in the IT industry, I have developed a wide range of (iOS and Android) Applications using iOS (Swift, Objective-C), Flutter(Dart) and Android (Kotlin, Java). I work with B2B clients - Software companies and as well as B2C - Small and Medium Enterprises (SME) in many vertical industries and I passionate about good service & deliver a high-quality product. I have experience in mobile Application Development, App principles, processes, theories and concepts. I am involved in the full life cycle of designing, developing, testing, Analysis, and maintaining applications. And I have also enough knowledge and experience of how to use and where to use Encryption, Exception Handling, Token-based Authentication and other security features in applications. My core competency lies in complete end-to-end management of new Mobile Applications and I am seeking opportunities to build Mobile Apps from the ground up for you or your business. My experience has been awesome and good so far, I have learned a lot of new things and worked during this time. My years of experience have prepared me well for this position. Skills: Programming Languages: iOS: Swift and Objective C Flutter: Dart Android: Java and Kotlin C/C++ Professional Experience: Socket Programming DB & API Design Google API, Facebook API, Google Maps and Direction, Location Services Integrating ads ( Google Ads, Admob, Facebook Ads), Media Player Functionalities API security with JWT JSON, XML In-app purchase User Authentication Chat and Messaging Hosting (App Store, Play Store) Payment Gateways Social Media Integration SQLite Database, Firebase Database, MySQL Database Advanced Analytics Mobile Application Design Document Conversion Projects Variations: Chatting and Messaging: Text and Voice messaging, video communication, photo & video sharing Loan Management System Online Music Streaming QR & Barcode Scanner Parking Facility Social Media: Professional networks, social networks and Data Sharing Lifestyle: Religion, travel, blogs, fashion, health, fitness Music + Audio Video Mixer with dynamic features Football Training Club and Academy Photo and Video Editor App Organization Management Society management Quiz - Play & Win I really enjoy programming a lot!

Updated on December 29, 2022

Comments

  • Paresh Mangukiya
    Paresh Mangukiya over 1 year

    I am developing a flutter app there the one where a user enters text in the text field and then applying a search from the server database which searches from DB. The issue is that the API call is happening when I input it further, but even when I press the back key, the API call is still happening.

    So I want that there is no API call when the user presses the return key.

    Here is my code

    TextField(
      controller: searchTextController,
      autofocus: true,
      decoration: InputDecoration(
        fillColor: colorWhite.withOpacity(0.20),
        filled: true,
        prefixIcon: Icon(Icons.search),
        hintText: "Search Data...",
        border: InputBorder.none,
        hintStyle: TextStyle(color: Colors.white30),
      ),
      onChanged: (value) {
        updateSearchQuery(query);
      },
      style: TextStyle(color: Colors.white, fontSize: 16.0),
    )