flet icon indicating copy to clipboard operation
flet copied to clipboard

How to hide keyboard?

Open fuad00 opened this issue 2 years ago • 9 comments

I want to hide my ios keyboard after clicking on ElevatedButton()

fuad00 avatar Jan 01 '24 21:01 fuad00

It should be hidden automatically when moving focus away from TextField, no? Could you provide the repro code please?

FeodorFitsner avatar Jan 08 '24 22:01 FeodorFitsner

Sure! Keyboard is not disappearing on clicking: search_button = ft.ElevatedButton("Поиск", on_click=search, disabled=True)

Full code:

import flet as ft
import requests
import config

def main(page: ft.Page):
    page.title = "Kinofun"
    page.session.set("old_text", "")


    def search(e):

        #page.controls.pop()
        film = search_box.value
        old_film = page.session.get("old_text")

        if film != old_film:
            page.session.set("old_text", film)
        else:
            return

        while len(page.controls) != 4:
            page.remove(page.controls[-1])


        r = requests.get(f"https://REDACTED/?title={film}&api_token=" + config.api_key)
        if r.status_code == 200:
            
            # scroll
            lv = ft.ListView(expand=1, spacing=10, padding=20, )

            found_films.value = f"Найдено: {len(r.json()['data'])}"


            for film in r.json()["data"]:

                lv.controls.append(ft.Text(film[ 'title'], size=22))
                lv.controls.append(ft.WebView('https://REDACTED=' + film["imdb_id"], height=200,))
                # print(film["iframe_src"])

            page.add(lv)
            
        else:

            found_films.value = "Фильмы не найдены!"

        page.update()


    def textbox_changed(e):
        if not search_box.value:
            search_button.disabled = True
        else:
            search_button.disabled = False

        page.update()

 
    old_text = ""

    search_box = ft.TextField(label="Название фильма",on_submit=search, on_change=textbox_changed, expand=True)
    search_button = ft.ElevatedButton("Поиск", on_click=search, disabled=True)
    found_films = ft.Text(size=30,)

    page.add(
        # отступ
        ft.Text(),
        ft.Text(),

        ft.Row(
            [search_box,search_button,],
            ),
        found_films,
    )





ft.app(target=main, web_renderer=ft.WebRenderer.HTML)

fuad00 avatar Jan 10 '24 22:01 fuad00

The code provided hasimport config which is not part of the main source code. I could not replicate this issue. Either provide the config module or maybe change the source code to replicate the error.

LineIndent avatar Jan 11 '24 14:01 LineIndent

import flet as ft
import requests

def main(page: ft.Page):
    page.title = "Kinofun"
    page.session.set("old_text", "")


    def search(e):

        #page.controls.pop()
        film = search_box.value
        old_film = page.session.get("old_text")

        if film != old_film:
            page.session.set("old_text", film)
        else:
            return

        while len(page.controls) != 4:
            page.remove(page.controls[-1])


        r = requests.get(f"https://REDACTED/?title={film}&api_token=")
        if r.status_code == 200:
            
            # scroll
            lv = ft.ListView(expand=1, spacing=10, padding=20, )

            found_films.value = f"Найдено: {len(r.json()['data'])}"


            for film in r.json()["data"]:

                lv.controls.append(ft.Text(film[ 'title'], size=22))
                lv.controls.append(ft.WebView('https://REDACTED/?=' + film["imdb_id"], height=200,))
                # print(film["iframe_src"])

            page.add(lv)
            
        else:

            found_films.value = "Фильмы не найдены!"

        page.update()


    def textbox_changed(e):
        if not search_box.value:
            search_button.disabled = True
        else:
            search_button.disabled = False

        page.update()

 
    old_text = ""

    search_box = ft.TextField(label="Название фильма",on_submit=search, on_change=textbox_changed, expand=True)
    search_button = ft.ElevatedButton("Поиск", on_click=search, disabled=True)
    found_films = ft.Text(size=30,)

    page.add(
        # отступ
        ft.Text(),
        ft.Text(),

        ft.Row(
            [search_box,search_button,],
            ),
        found_films,
    )





ft.app(target=main, web_renderer=ft.WebRenderer.HTML)

fuad00 avatar Jan 11 '24 15:01 fuad00

How should the code be tested please?

ndonkoHenri avatar Jan 11 '24 16:01 ndonkoHenri

How should the code be tested please?

IMG_FAF5EAD8F4A4-1

if i press "done" on keyboard it disappears, but if i press a search_button = ft.ElevatedButton() then not

fuad00 avatar Jan 11 '24 21:01 fuad00

@fuad00 At the beginning of the search function, write the following:

def search(e):
    search_box.read_only = True
    search_box.show_cursor = False

7576457 avatar Jul 25 '24 18:07 7576457

I have the same issue too. On a simple TextField, the IOS keyboard does not hide when clicking on an empty screen or when sliding it down.

The only workaround for me is to use the "Done" button on the IPhone keyboard itself. But unfortunatelly its not visible when using multiline=True, so I am limited to single line inputs for now :(

Single Line

Only way to close by pressing Done ("Fertig" in German), not possible with any clicks or gestures

      self.note_field = ft.TextField(
          label="Notes", 
          width=320, 
          content_padding=10,
          adaptive=False,
      )

Image

Multiline

Without the "Done" button on the keyboard itself its not possible to close the keyboard

      self.note_field = ft.TextField(
          label="Notes", 
          width=320, 
          content_padding=10,
          adaptive=False,
          multiline=True,
          min_lines=3,
      )

Image

JuliusWiedemann avatar Mar 30 '25 09:03 JuliusWiedemann

More than a year and it was never solved? :( I'm having the same issue. I can't find a way to remove the focus from the textfield so the keyboard will be hide automatically. Any idea?

GreenDringo42 avatar Nov 23 '25 05:11 GreenDringo42