扑Android的电视ListView焦点项目更改背景和文本 [英] flutter android tv listview focused item change background and text

查看:43
本文介绍了扑Android的电视ListView焦点项目更改背景和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flutter构建一个用于Android TV的简单应用.

I'm building a simple app for Android TV with Flutter.

我有一个项目的列表视图,想在不使用onPressed ...的情况下更改每个重点项目的页面背景和页面文本.

I have a listview of items and would like to change the page background and page text on each focused item without onPressed...

感谢您的帮助!

推荐答案

处理需要重点关注的项目的最佳方法是使用

The best way to work with items that need focus, is using the Focus class.

为此,您需要设置一个 FocusScope ,其中每个ListView项目都是一个 Focus ,每个 FocusNode .

To do so, you need to set a FocusScope, where each of your ListView items are a Focus with a FocusNode each.

您的窗口小部件树应如下所示:

Your widget tree should look like this:

FocusScope-> ListView-> Focus->生成器(或任何可以提供上下文的小部件)

FocusScope --> ListView --> Focus --> Builder (or any widget that can provide a context)

要检查最后一个小部件是否为焦点,您可以这样操作:

To check if the last widget is focused, you can do it like this:

final FocusNode focusNode = Focus.of(context);
final bool hasFocus = focusNode.hasFocus;

因此,如果hasFocus为true,则更改颜色或TextStyle或所需的任何内容.

And so, if hasFocus is true, you change color or TextStyle or whatever you need.

修改

我忘记了这一点.如果需要更改其他内容(而不是重点项目),则可以使用 FocusScope onFocusChange 属性.

I forgot this. If you need to change something else (not the focused item) you can use the onFocusChange property of the FocusScope.

我希望这会有所帮助,并且您可以进一步开发您的应用程序!

I hope this helps and you can develop your app further!

这篇关于扑Android的电视ListView焦点项目更改背景和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆