How to Trigger Button Click in Flutter

In this example, we are going to show you how to simulate or trigger gestures like click, button down, button up, or button press in Flutter. You will learn to make an auto-click button with this example. See the codes below:

final mybuttonkey = GlobalKey();
ElevatedButton(
  key:mybuttonkey,
  onPressed: (){
    print("Button is pressed");
  },
  child: Text("My Button"),
)

First, you need to set the global key to the button or any other widget where you want to simulate the button press gesture.

RenderBox renderbox = mybuttonkey.currentContext!.findRenderObject() as RenderBox;
Offset position = renderbox.localToGlobal(Offset.zero);
double x = position.dx;
double y = position.dy;

print(x);
print(y);

This is the way to get the x and y position of buttons or any widget in Flutter.

import 'package:flutter/gestures.dart';
GestureBinding.instance.handlePointerEvent(PointerDownEvent(
  position: Offset(x, y),
)); //trigger button up,

await Future.delayed(Duration(milliseconds: 500));
//add delay between up and down button

GestureBinding.instance.handlePointerEvent(PointerUpEvent(
  position: Offset(x, y),
)); //trigger button down

Using the GestureBinding(), you can trigger any mouse click event at a given x and y coordinate offset. 

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

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

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {

    final mybuttonkey = GlobalKey();

    return  Scaffold(
      appBar: AppBar( 
          title: Text("Trigger Button Click"),
          backgroundColor: Colors.deepPurpleAccent,
      ),
      body: Container(
        padding: EdgeInsets.all(20),
        alignment: Alignment.topCenter,
        child:Column(
          children: [
              //button A
              ElevatedButton(
                key:mybuttonkey,
                onPressed: (){
                  print("Button is pressed");
                },
                child: Text("My Button"),
              ),

               //Button B
               ElevatedButton(
                onPressed: () async {
                  
                    RenderBox renderbox = mybuttonkey.currentContext!.findRenderObject() as RenderBox;
                    Offset position = renderbox.localToGlobal(Offset.zero);
                    double x = position.dx;
                    double y = position.dy;

                    print(x);
                    print(y);


                    GestureBinding.instance.handlePointerEvent(PointerDownEvent(
                      position: Offset(x, y),
                    )); //trigger button up,

                    await Future.delayed(Duration(milliseconds: 500));
                    //add delay between up and down button

                    GestureBinding.instance.handlePointerEvent(PointerUpEvent(
                      position: Offset(x, y),
                    )); //trigger button down


                }, 
                child: Text("Trigger Cick Button A")
              )
          ]
        )
      ),
    );
  }
}

Here, when the user presses the down button, the upper button will be triggered button press, the GIF output is shown above in this article.

In this way, you can simulate or trigger button press or click in Flutter.

No any Comments on this Article


Please Wait...