Nic*_*uir 3 focus d-pad android-tv flutter
我正在创建一个 Android 电视应用程序。我花了很长时间试图弄清楚为什么当我单击遥控器上的向上和向下箭头按钮时,它似乎没有执行任何操作,并且没有选择任何列表项。
最终我发现,如果我使用列表中的提升按钮或其他可聚焦的小部件,我可以使用箭头键,它会工作得很好。以前我使用的是包裹在手势检测器中的卡片小部件。
所以我想知道带有手势检测器的按钮和卡片之间的区别是什么,阻止箭头键选择项目。我怀疑这是焦点。
这就是我使用的不允许遥控器上的向上、向下键选择它的方法:
GestureDetector(
child: Card(
color: color,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
elevation: 10,
child: SizedBox(
width: (width / numberOfCards) - padding * (numberOfCards - 1),
height: (height / 2) - padding * 2,
child: Center(child: Text(cardTitle, style: Theme.of(context).textTheme.bodyText1?.copyWith(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.white),))),
),
onTap: () => onCardTap(),
),
Run Code Online (Sandbox Code Playgroud)
这是我替换它的按钮,然后使上下键和选择起作用:
ElevatedButton(
onPressed: () {},
child: Text('Test 1', style: Theme.of(context).textTheme.bodyText1?.copyWith(color: Colors.white, fontSize: 18, fontWeight: FontWeight.normal)),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.grey.withOpacity(0.3)),
minimumSize: MaterialStateProperty.all(Size(60, 60)),
elevation: MaterialStateProperty.all(10),
shape: MaterialStateProperty.all(RoundedRectangleBorder(borderRadius: new BorderRadius.circular(50)),)),
),
Run Code Online (Sandbox Code Playgroud)
如果需要,这就是我用来获取按键的方法:
Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
},
Run Code Online (Sandbox Code Playgroud)
谢谢
带手势检测器的卡与带有手势检测器的卡之间的区别ElevatedButton在于您没有FocusNode.
如果您深入研究 的实现细节,ElevatedButton您会发现它使用了 anInkWell和 aFocusNode
final Widget result = ConstrainedBox(
constraints: effectiveConstraints,
child: Material(
// ...
child: InkWell(
// ...
focusNode: widget.focusNode,
canRequestFocus: widget.enabled,
onFocusChange: updateMaterialState(MaterialState.focused),
autofocus: widget.autofocus,
// ...
child: IconTheme.merge(
// ....
child: Padding(
padding: padding,
child: // ...
),
),
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
因此,如果您替换GestureDetector为Inkwell,则键盘导航将起作用。
InkWell(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
elevation: 10,
child: const SizedBox(
width: 200,
height: 60,
child: Center(
child: Text(
'Test 1',
),
),
),
),
onTap: () {},
)
Run Code Online (Sandbox Code Playgroud)
(在 Android TV 模拟器 API 30 上测试,使用键盘和方向键。)
| 归档时间: |
|
| 查看次数: |
4890 次 |
| 最近记录: |