How can I dismiss the on screen keyboard?

Cover Image for How can I dismiss the on screen keyboard?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝📲💻 Blog Post: How to Dismiss the On-Screen Keyboard in Flutter

Hey there, Flutter fans! Today, we're going to tackle a common issue many of you might have encountered while developing your Flutter apps - dismissing the on-screen keyboard. 😮

Picture this: you have a cool app that collects user input using a TextFormField widget, and when the user is done inputting their text, they tap a FloatingActionButton to indicate they're finished. But, uh-oh! The on-screen keyboard just stays there, obscuring the view and preventing a smooth user experience. 😫

Don't worry, though! We've got you covered with some easy-peasy solutions. Let's dive into the code and see how we can make the keyboard go away automatically. 🚀

import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  MyHomePageState createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  TextEditingController _controller = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.send),
        onPressed: () {
          setState(() {
            // send message
            // dismiss on screen keyboard here
            _controller.clear();
          });
        },
      ),
      body: new Container(
        alignment: FractionalOffset.center,
        padding: new EdgeInsets.all(20.0),
        child: new TextFormField(
          controller: _controller,
          decoration: new InputDecoration(labelText: 'Example Text'),
        ),
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

void main() {
  runApp(new MyApp());
}

🔎 So, how can we dismiss that pesky on-screen keyboard? Let's check out two viable solutions in Flutter:

  1. Using the FocusScope class and unfocus method:

import 'package:flutter/services.dart;

// Inside your function
FocusScope.of(context).unfocus();

With this approach, you call the unfocus() method after the user taps the FloatingActionButton. This method will remove focus from any focused input fields and also dismiss the keyboard!

  1. Utilize the TextInputAction and the onFieldSubmitted property:

TextFormField(
  controller: _controller,
  decoration: new InputDecoration(labelText: 'Example Text'),
  textInputAction: TextInputAction.send,
  onFieldSubmitted: (_) {
    // send message
    // dismiss on screen keyboard here
    _controller.clear();
  },
),

By setting the textInputAction property to TextInputAction.send, you can trigger the keyboard's "send" button. Combine this with the onFieldSubmitted property, where you can handle the text submission and dismiss the keyboard simultaneously.

💡 Remember to wrap your TextFormField with a Form widget if you haven't already, as it allows for validation and easy access to the on-screen keyboard.

🎉 And there you have it, Flutter folks! Two simple ways to dismiss the on-screen keyboard in your Flutter app. Give them a try and let us know how it goes! If you have any other Flutter-related questions or tips, feel free to drop a comment below. We love hearing from our Flutter community! 😊

✨ Keep calm and Flutter on! ✨


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello