FIX keyboard
This commit is contained in:
@@ -307,17 +307,20 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
return _formatDuration(duration);
|
||||
}
|
||||
|
||||
// Update keyboard handler method to use modern KeyEvent API
|
||||
void _handleKeyEvent(KeyEvent event) {
|
||||
// Only process key down events to avoid duplicate triggers
|
||||
if (event is KeyDownEvent) {
|
||||
// Handle Space key
|
||||
if (event.logicalKey == LogicalKeyboardKey.space) {
|
||||
// Space: Start if paused, Lap if running
|
||||
if (_isRunning) {
|
||||
_recordLap();
|
||||
} else {
|
||||
_startTimer();
|
||||
}
|
||||
} else if (event.logicalKey == LogicalKeyboardKey.enter) {
|
||||
// Enter: Stop if paused, Pause if running
|
||||
}
|
||||
// Handle Enter key
|
||||
else if (event.logicalKey == LogicalKeyboardKey.enter) {
|
||||
if (_isRunning) {
|
||||
_pauseTimer();
|
||||
} else {
|
||||
@@ -336,16 +339,25 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return KeyboardListener(
|
||||
// Use Focus directly to capture key events
|
||||
return Focus(
|
||||
focusNode: _focusNode,
|
||||
onKeyEvent: _handleKeyEvent,
|
||||
autofocus: true,
|
||||
onKeyEvent: (FocusNode node, KeyEvent event) {
|
||||
_handleKeyEvent(event);
|
||||
// Always return KeyEventResult.handled to indicate we've processed the event
|
||||
return KeyEventResult.handled;
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: Center(
|
||||
body: GestureDetector(
|
||||
// This ensures tapping anywhere gives focus back to our listener
|
||||
onTap: () => _focusNode.requestFocus(),
|
||||
behavior: HitTestBehavior.translucent,
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
@@ -407,6 +419,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user