【发布时间】:2022-10-15 07:56:39
【问题描述】:
我花了很多时间来理解为什么它不起作用,但仍然不知道如何解决它。 我找到了这个解决方案 https://github.com/flutter/flutter/issues/49783 ,但它对我没有帮助。 我的问题是按下 RC 上的选择按钮时开关不会改变(我正在使用模拟器)。 重现步骤:
- 启动应用程序
- 在第一个文本控件中输入一些文本(在我的情况下,我需要按向下和向上按钮来显示键盘,这是另一个错误,可以很酷地修复)
- 按回 RC 按钮(顺便说一句,如果您知道如何使用提交,请告诉我)隐藏键盘
- 按下 RC 按钮 - 焦点将切换
- 然后尝试按下选择按钮(它是第一次工作)
- 然后返回文本字段并按返回然后向下 RC 按钮
- 现在按 Select 时开关不起作用
- 按下选择按钮仍然不起作用
- 按下 - 选择正在切换开关,按钮再次起作用! 如果你去文本字段,那么它会重复
我的示例代码:
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" />
【问题讨论】:
标签: android flutter focus textfield television