我使用RawKeyboardListener
来捕获web上的键盘事件,它在调试模式下运行良好,但当我为发布版本构建它时,它不会捕获键盘事件。我用一个基本的应用程序尝试过:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
Future<void> _onEventKey(RawKeyEvent event) async {
if (event.runtimeType.toString() == 'RawKeyDownEvent') {
if (event.isKeyPressed(LogicalKeyboardKey.arrowLeft)) {
_incrementCounter();
}
}
}
@override
Widget build(BuildContext context) {
return RawKeyboardListener(
focusNode: FocusNode(),
onKey: (RawKeyEvent event) async {
await _onEventKey(event);
},
autofocus: true,
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
),
);
}
}
我做错什么了吗?
由于生成的javascript代码在发布模式下被缩小,因此没有更多的类型RawKeyDownEvent,而是类似于缩小的类型:qN。
代替
if (event.runtimeType.toString() == 'RawKeyDownEvent') {
你必须使用一个更准确的比较:
if (event.runtimeType == RawKeyDownEvent) {
以下是固定代码(也删除了不必要的异步/等待(:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
void _onEventKey(RawKeyEvent event) {
// next line prints something like 'minified:qN' in production mode
print(event.runtimeType.toString());
// if (event.runtimeType.toString() == 'RawKeyDownEvent') {
if (event.runtimeType == RawKeyDownEvent) {
if (event.isKeyPressed(LogicalKeyboardKey.arrowLeft)) {
_incrementCounter();
}
}
}
@override
Widget build(BuildContext context) {
return RawKeyboardListener(
focusNode: FocusNode(),
onKey: (RawKeyEvent event) {
_onEventKey(event);
},
autofocus: true,
child: Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('You have pushed the button this many times:'),
Text('$_counter', style: Theme.of(context).textTheme.headline4),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
),
);
}
}