'onPressed in D-PAD not working in Flutter running in Android TV after focus TextField
I have developed a small app for Android Tv, everything works great, but if there is a TextField on the screen, the focus control becomes quite unstable, especially onPressed/onTap, etc. events stop working (when using D-Pad, in simulator o real AndroidTV device)
I've been trying to fix this for several days. I've tried using FocusNode, change the focus manually in onSubmitted, etc.
In the main I use Shortcuts with LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(), And I have followed several examples.
The application uses other forms, some with dinamically ListView (with buttons and checks in the rows) and none of them gives focus problem.
I just need to know why in a simple screen, with two textfields and a button, after entering a textfield and selecting the button, onPressed stops working. If no textfield has been selected and the button gets focus first, then no problem.
Very important to clarify that the button receives the focus and is highlighted, but onPressed does not work. I have tried different types of buttons and InkWell. The problem is not in the focus I think, but in performing the action of the button
Even using a FocusNode.listener, I can check that the button gets the focus, but I onPressed is not working. Same result assigning a focusNode to each element
Sometimes with a single textfield it seems to work but it's random
I leave a simple example, using Flutter 3, but same result in previous versions
Thanks in advance
import 'package:flutter/material.dart';
class TVForm extends StatefulWidget {
const TVForm({Key? key}) : super(key: key);
@override
State<TVForm> createState() => _TVFormState();
}
class _TVFormState extends State<TVForm> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(50.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: const [
Expanded(child: TextField()),
SizedBox(width: 20),
Expanded(child: TextField()),
],
),
ElevatedButton(
onPressed: () {
debugPrint("pressed!");
},
child: const Text("ok"))
],
),
),
);
}
}
If you want to test this in a AndroidTV emulator or real device, it's necessary to add
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
into intent-filter of manifest, and also add
<uses-feature android:name="android.software.leanback"
android:required="false" />
More info about configure Flutter app for Android tv here
UPDATE: If instead of a button there are two buttons in a row, then after returning from the textfield, the first button gets the focus but it doesn't work on Pressed, but the second one does!, and when returning to the first one it works again. That is, the following widget that captures the focus after the textfield does not work onpressed, but the following one does.
Solution 1:[1]
Of course, relevant xkcd.
I'm sure you'll get many different answers, but after struggling for months with environment management when I first started coding, I finally ended up finding that pyenv was the best option for me.
You can use Homebrew to install pyenv and virtualenv.
Pyenv installation instructions for MacOS: https://github.com/pyenv/pyenv#homebrew-in-macos Virtualenv installation instructions for MacOS: https://github.com/pyenv/pyenv-virtualenv
Of course you'll have to make sure to carefully follow the installation instructions for whatever terminal shell you use, zsh, bash, etc. The linked GitHub installation instructions explain this.
After you've successfully installed the two, you can use pyenv to install specific base versions of Python, e.g., pyenv install 3.8.8.
You can then use virtualenv to create specific closed environments for each Python version. I use this frequently for building/testing with packages that aren't compatible with one another.
You can activate the environments with commands as so: pyenv virtualenv 3.8.8 app_build, and to create another one, pyenv virtualenv 3.8.8 app_testing.
Then you can activate each environment for usage in your terminal or IDE by using pyenv activate app_build, etc. After activation, you can install packages, and they will only be installed in the activated environment.
Pyenv integrates very nicely with Visual Studio Code as well. I can speak more to that if you're interested.
I used anaconda for a while, but it wasn't well suited for app development/PyQt5, so I abandoned it. I personally like much pyenv much more, as I feel more in control with it.
Solution 2:[2]
"python3" is the python of the system. Same as "/usr/bin/python3":
/usr/bin/python3
Python 3.8.9 (default, Oct 26 2021, 07:25:54)
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
You want to use "/usr/local/bin/python3":
/usr/local/bin/python3
Python 3.9.9 (main, Nov 21 2021, 03:23:44)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Solution 3:[3]
Choose the python version you would like to install from:
pyenv install --list
I have chosen 3.10.1.
Install python:
pyenv install 3.10.1
Switch to it in the current shell:
pyenv shell 3.10.1
Create virtual environment for the project:
python -m venv <new_env_directory>
Activate the newly created environment:
<new_env_directory>/bin/activate
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 | Jacob Bumgarner |
| Solution 2 | Nineteendo |
| Solution 3 | K4liber |
