Search code examples
flutterdartbuttonsimulationkeypress

Flutter: Programatically perform a button click


How can you programatically perform a button click in Flutter? I basically want to bind a number of buttons to keys on the keyboard, the most important part is that after a key is pressed the corresponding button the visual state of the button is triggered to inform the user which button was pressed.

I am aware you can invoke the onTap function, but that by itself doesn't update the visual state of button. Is there a way to simulate this kind of behavior, I tried to look into MaterialStateController but doesn't feel like it's the right approach.


Solution

  • I discovered that _InkWellResponseState (internal-) classes have a function called simulateTap. The function is bound against ActivateIntent and by default this intend is fired whenever the user presses Enter or Space

    
    class SimActivateIntent extends ActivateIntent {
      const SimActivateIntent (this.model);
      final FocusNode model;
    }
    
    class SimActivateAction extends Action<SimActivateIntent> {
      SimActivateAction ();
      @override
      void invoke(covariant SimActivateIntentintent) {
        Actions.maybeInvoke(intent.model.context!, const ActivateIntent());
      }
    }
    
    

    With the intends listed above one can invoke the ActivateIntent on a specific object and simulate a button press. They can bound into the application using Shortcuts & Actions classes.

    Map<ShortcutActivator, Intent> get defaultShortcuts {
        return <ShortcutActivator, Intent>{
           const SingleActivator(LogicalKeyboardKey.backspace):
              SimActivateIntent(digitBackspace),
        }
    }
    
    @override
    Widget build(BuildContext context)
    {
        return Shortcuts(
          shortcuts: defaultShortcuts,
          child: Actions(
          actions: <Type, Action<Intent>>{
            SimActivateIntent: SimActivateAction(),
          },
          child: ...
        ));
    }