Flutter Windows TextField set focus with shortcut keybinding

Viewed 309

I am trying to bind a shortcut (ctrl + f) to set a textfield in focus. So far it only works once, so after I press ctrl + f the textfield is focussed, but when the textfield loses focus (because I click somewhere else on the screen (intentionally)) and try the shortcut again, it doesn't work. With a simple counter it works, but not with the textfield, so I suppose the problem occurs due to the focusNode of the TextField, but I don't know how to make it work properly...

Here is a minimal reproducible code sample:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  FocusNode focusNode = FocusNode();

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  void setFocus() {
    focusNode.requestFocus();
  }


  @override
  Widget build(BuildContext context) {
    return CounterShortcuts(
        onIncrementDetected: _incrementCounter,
        onFocusDetected: setFocus,
        child: Scaffold(
        appBar: AppBar(
        title: Text(widget.title),
    ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have clicked the arrow up key this many times:',
            ),
            // a simple counter test which works always
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            // a textfield test which does not work after unfocusing
            TextField(
              focusNode: focusNode,
            )
          ],
        ),
      ),
    ));
  }
}
// for the counter test
final incrementKeySet = LogicalKeySet(
  LogicalKeyboardKey.control,
  LogicalKeyboardKey.arrowUp,
);
// for the textfield
final focusKeySet = LogicalKeySet(
  LogicalKeyboardKey.control,
  LogicalKeyboardKey.keyF,
);

class IncrementIntent extends Intent {}
class FocusIntent extends Intent {}

class CounterShortcuts extends StatelessWidget {

  final Widget child;
  final VoidCallback onIncrementDetected;
  final VoidCallback onFocusDetected;

  const CounterShortcuts({
    Key? key,
    required this.child,
    required this.onIncrementDetected,
    required this.onFocusDetected,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FocusableActionDetector(
      autofocus: true,
      shortcuts: {
        incrementKeySet: IncrementIntent(),
        focusKeySet: FocusIntent(),
      },
      actions: {
        IncrementIntent:
        CallbackAction(onInvoke: (e) => onIncrementDetected.call()),
        FocusIntent:
        CallbackAction(onInvoke: (e) => onFocusDetected.call()),
      },
      child: child,
    );
  }
}
0 Answers
Related