55208

How to intercept flutter back-button when keyboard is shown

<h3>Question</h3>

I want to intercept the back-button of the soft keyboard in flutter. So when I want to close the keyboard by pressing the back-button I want an additional function to be called. How can I do that?

Keyboard Back button


<h3>Answer1:</h3>

you can use the keyboard_visibility package to achieve this.

Working Example

the following code displays a SnackBar once the keyboard is dismissed.

<pre class="lang-dart prettyprint-override">import 'package:flutter/material.dart'; import 'package:keyboard_visibility/keyboard_visibility.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { GlobalKey<ScaffoldState> _key; @override void initState() { super.initState(); _key = GlobalKey<ScaffoldState>(); KeyboardVisibilityNotification().addNewListener( onHide: () { _key.currentState.showSnackBar( SnackBar( content: Text("Keyboard closed"), ), ); }, ); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( key: _key, body: Center( child: TextField(), ), ), ); } }

来源:https://stackoverflow.com/questions/57727350/how-to-intercept-flutter-back-button-when-keyboard-is-shown

Recommend

  • compile-time safe array in C++?
  • adding and subtracting for BODMAS system
  • JGit/EGit crashing on branch deletion/creation
  • Multiple database in single query is possible?
  • Compare multiple algorithms with sklearn pipeline
  • Fixing malformed HTML in PHP?
  • How can I use JSON_EXTRACT in MySQL and get a string without the quotes?
  • ARKit 2 - Crop Recognizing Images
  • Open default mail app from within Qt with some html
  • Xamarin.iOS Status Bar Disappears When Changing Its Text Color
  • Convert a Treeview to JSON using C#
  • Error when build LineageOS: “make: *** [ninja_wrapper] Error 1”
  • Optimization of Neural Network input data
  • Haskell function that outputs all combinations within the input list that add to the input number
  • Multiple angular 2 apps and /api on same host with nginx
  • Display standard razor/mvc 3 validation messages displayed in another language
  • How do I set context and followup event in one intent?
  • Velocity (VM) template request parameters: Getting GET variables
  • Passing HTTP Post from AWS API GW to Lambda
  • Node.js custom error handling
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • How to use AJAX to upload large CSV file? [closed]
  • How to create subsets of a single set of elements with XSLT?
  • Multiplying polynomials/simplifying like terms
  • how to run a different select statement based on condition in Hive SQL
  • Conflicting declaration using constexpr and auto in C++11
  • Spring Boot fails to start
  • Codeigniniter insert data through models and controller
  • VLOOKUP in IMPORTRANGE
  • Sign a Pdf using custom digital signature in Java
  • Using Service Component Runtime
  • Spring Boot not autowiring @Repository
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}