0

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.

Salva Burrezo
  • 23
  • 1
  • 5
  • You could try using a StatefulWidget instead of StatelessWidget. Stateless will be redrawn whenever state has changed and will not retain any previous values. – Dan Harms May 20 '22 at 14:03
  • Ah, I apologize for the poor example, in my application I use Statefull, and I have created this example as the least code possible where I can reproduce the problem. I have edited the question. Thank you – Salva Burrezo May 20 '22 at 14:09
  • It looks like this is a known issue that is still unresolved: https://github.com/flutter/flutter/issues/49783 – Dan Harms May 20 '22 at 14:47
  • Could you check if `onEditingComplete` is invoked when you leave the TextField but prior to the attempted button click? If so, you could manually remove focus at that point so the button click can go through. My theory is focus is being maintained after leaving the TextField, so the first click removes focus, and the second click will work as expected. – Dan Harms May 20 '22 at 14:56
  • Same result, I already performed that operation with onSubmitted, and effectively the button gets the focus and lights up, but onPressed doesn't respond, unless you go to the second button and come back – Salva Burrezo May 20 '22 at 15:06
  • I've tried everything posted on the subject and can't get it to work. I could pass to RawKeyboardListener, but I see it very complicated. – Salva Burrezo May 20 '22 at 15:08
  • So multiple clicks on the same button always fail? – Dan Harms May 20 '22 at 15:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244913/discussion-between-salva-burrezo-and-dan-harms). – Salva Burrezo May 20 '22 at 18:44

0 Answers0