我花了很多时间去理解为什么它不起作用,但仍然不知道如何修复它。
我发现了这个解决方案https://github.com/flutter/flutter/issues/49783,但这并没有帮助我。
我的问题是在按下RC上的选择按钮时,开关无法更改(我正在使用模拟器)。
复现步骤:
- 启动应用程序
- 在第一个文本控件中输入一些文本 (在我的情况下,我需要按下和释放按钮才能显示键盘,这是另一个可以解决的错误)
- 按下RC返回按钮以隐藏键盘
- 按下RC向下按钮-焦点将移到开关
- 然后尝试按下选择按钮(它对第一次有效)
- 然后返回到文本字段,按下返回和然后按下RC按钮
- 目前,开关在按下选择按钮时不起作用
- 按下选择仍无法在按钮上工作
- 按下向上箭头-选择正在切换开关,并且按钮再次起作用!如果您进入文本字段,则会重复此过程
我的示例代码:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
},
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool switchValue = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Demo Home Page'),
),
body: Center(
child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 10),
child: Focus(
canRequestFocus: false,
onKey: (FocusNode node, RawKeyEvent event) {
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
FocusManager.instance.primaryFocus!
.focusInDirection(TraversalDirection.left);
} else if (event.logicalKey ==
LogicalKeyboardKey.arrowRight) {
FocusManager.instance.primaryFocus!
.focusInDirection(TraversalDirection.right);
} else if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
FocusManager.instance.primaryFocus!
.focusInDirection(TraversalDirection.up);
} else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
FocusManager.instance.primaryFocus!
.focusInDirection(TraversalDirection.down);
}
return KeyEventResult.handled;
},
child: TextField(
autofocus: true,
),
),
),
Switch(
value: switchValue,
onChanged: (value) {
setState(() {
switchValue = value;
});
},
),
TextButton(
onPressed: () => print('Button pressed'),
style: TextButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
),
child: const Text('Test'),
),
],
),
),
);
}
}
这是我的 AndroidManifest.xml 文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testfocus.test_focus">
<application
android:label="test_focus"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<uses-feature android:name="android.software.leanback" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />