From 5d5ae63c160f105d8f897967c3f21190a70c2ab7 Mon Sep 17 00:00:00 2001 From: Kaj Rusilowski <2020804@stud.hs-mannheim.de> Date: Tue, 9 Apr 2024 21:39:59 +0200 Subject: [PATCH] added keyboard input --- lib/main.dart | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index a50cd80..540230b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; void main() { runApp(const MyApp()); @@ -51,24 +52,32 @@ class _MyHomePageState extends State { backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, - ), - ], + body: RawKeyboardListener( + focusNode: FocusNode(), + autofocus: true, + onKey: (RawKeyEvent event) { + if (event.isKeyPressed(LogicalKeyboardKey.keyD)) _decrementCounter(); + if (event.isKeyPressed(LogicalKeyboardKey.keyI)) _incrementCounter(); + }, + child: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text( + 'You have pushed the button this many times:', + ), + Text( + '$_counter', + style: Theme.of(context).textTheme.headlineMedium, + ), + ], + ), ), ), floatingActionButton: Stack( children: [ Padding( - padding: EdgeInsets.only(left: 31), + padding: const EdgeInsets.only(left: 31), child: Align( alignment: Alignment.bottomLeft, child: FloatingActionButton(