0

Im creating a flutter desktop app and keyboard events are not being detected by the FocusScope in Release mode. Everything works fine in debug mode. Any idea how I can get this working right?

Maybe I can give my client the debug folder since it works fine, and not the Release. Are there any significant differences besides size? I cant seem to identify anything noticeable as both versions load fast.

My code below:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(

      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  int counter = 0;
  late FocusNode _focusNode;

  @override
  dispose() {
    _focusNode.dispose();
  }

  @override
  void initState() {
    _focusNode = FocusNode();
  }

    @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("Test App"),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(

          children: <Widget>[
            Text(
              counter.toString(),
            ),
            FocusScope(
                child: TextField(),
                onKey: (_focusNode, event) {
                  if (event.toString().contains('RawKeyDownEvent') && event.toString().contains('Arrow Up')) {
                    print(event.toString());
                    setState(() {
                      counter--;
                    }
                    );
                    return KeyEventResult.handled;
                  }
                  if (event.toString().contains('RawKeyDownEvent') && event.toString().contains('Arrow Down')) {
                    print(event.toString());
                    setState(() {
                      counter++;
                    }
                    );
                    return KeyEventResult.handled;
                  }
                  if (event.toString().contains('RawKeyDownEvent') && event.toString().contains('Arrow Up')) {
                    print(event.toString());
                    setState(() {
                      counter--;
                    }
                    );
                    return KeyEventResult.handled;
                  }
                  return KeyEventResult.ignored;
                }

            ),

          ],
        ),
      ),
    );
  }
}
West
  • 2,350
  • 5
  • 31
  • 67
  • 1
    most likely its because of that tricky `event.toString().contains` calls, check `RawKeyEvent` official API documentation to know how to get particular key from the event – pskink Jul 04 '21 at 07:47
  • @pskink Great thanks. I've changed to `if (event.runtimeType == RawKeyUpEvent && event.logicalKey == LogicalKeyboardKey.arrowUp) {}` and seems to work fine now. Not sure its the right way as I think I've read of potential issues with using `event.RuntimeType` before. – West Jul 04 '21 at 07:58
  • 1
    more natural is `if (event is RawKeyUpEvent && ...` – pskink Jul 04 '21 at 07:59
  • @pskink Works perfect. Thanks! – West Jul 04 '21 at 08:04
  • sure, your welcome – pskink Jul 04 '21 at 08:13

0 Answers0