onPressed in D-PAD not working in Flutter running in Android TV after focus TextField

1.4k Views Asked by At

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.

0

There are 0 best solutions below