'flutter TextField use cause KeyboardListener alway show keyboard on event
using KeyboardListener to gain barcode from scanner, i can't resolve a incredibly stupid problem. scanning first with keyboardListener work perfectly. events are gained, barcode too, no virtual keyboard, perfect..
but if i use any Textfield, in the same screen or anywhere in the program, after that, any event coming to keyboardListener show Virtual Keyboard, without any textfield or else in the screen. it come to be a nightmare..
I WANT TO AVOID THE KEYBOARD SHOWING, without any input..
don't want to close keyboard, many way on stack overflow to do it.
Step to reproduce:
1:use Physical keyboard or HID to enter serialcode or else whith KeyboardListener
2: Tap on textfield, and write anything and valid text
3: normay, KeyboardListener regain control and get physical events, and the Keyboard show on each...and this is the problem..
youtube video to illustrate (52s)
strange thing. if you use square key to set app background and get foreground, problem disapear.. virtual keyboard dont show again on physical keyboard or HID use... until next textfield use..
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'KbdListener with TextField'),
);
}}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<FocusNode> ListFocusNode = [FocusNode(), FocusNode()];
DateTime whenlastchar = DateTime.now();
List<String> scanned4 = [];
String _receivedtext = "Scanned text here..";
final TextEditingController _myTextControler =
TextEditingController(text: "");
@override
void initState() {
ListFocusNode.first.requestFocus();
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
KeyboardListener(
key: const Key('KeyboardListener-files'),
focusNode: ListFocusNode.first,
autofocus: true,
onKeyEvent: (event) async {
var difference = DateTime.now().difference(whenlastchar);
whenlastchar = DateTime.now();
if (event.character != null) {
if (difference.inMilliseconds > 1000) {
scanned4.clear();
}
scanned4.add(event.character.toString());
if ((event.character == "\n") ||
(event.character == " ") ||
(event.character == 0x09)) {
String tempo =
scanned4.reduce((first, second) => first + second);
scanned4.clear();
tempo = tempo.trim();
// update
setState(() {
_receivedtext = tempo;
});
}
}
},
child: Column(
children: <Widget>[
Text(
_receivedtext,
style: Theme.of(context).textTheme.headlineSmall,
),
],
),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
TextField(
controller: _myTextControler,
autofocus: false,
focusNode: ListFocusNode.last,
keyboardType: TextInputType.text,
style: const TextStyle(
fontSize: 20,
color: Colors.black,
fontWeight: FontWeight.w400,
),
textInputAction: TextInputAction.done,
onSubmitted: (value) {
print("textfield value: '$value'");
setState(() {
_receivedtext = value;
});
_myTextControler.clear();
FocusScope.of(context)
.requestFocus(ListFocusNode.first);
},
),
Row(children: [
TextButton(
child: const Text("KeyboardListener Focus"),
onPressed: () {
setState(() {
FocusScope.of(context)
.requestFocus(ListFocusNode.first);
});
},
),
]),
],
),
),
],
),
),
);
}
}
Solution 1:[1]
ok, so, there is no flutter software solution at this time.
its a google keyboard bug or flutter bug.. or both.
seem that the google keyboard don't dismiss from textfield, TextControler, focusnode or else. or flutter don't destroy callback to google keyboard. don't know.
But, trying other app keyboard, and its magical. its working.. normaly.. fluently.. as expected.
to be honest and complete, perhaps this replacement keyboard don't implement a callback or else.
but he work, the app work, my customers can use it fluently..
without changing any part of my code or flutter code..
the cost? just say to customer. 'install and use this keyboard..'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |