How to Trigger Button Click in Flutter with example ?

We are going to create a very simple screen with only one button in the middle. Below is a code example that does just that.

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: const Text('Navigate to a new screen on Button click'),
          backgroundColor: Colors.teal),
      body: Center(
        child: ElevatedButton(
  key:mybuttonkey,
  onPressed: (){
    print("Button is pressed");
  },
  child: Text("My Button"),
)
      ),
    );
  }
}

 Step 1: Set the Key for the Button:

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.

Full code:

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(" Yes 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")
              )
          ]
        )
      ),
    );
  }
}

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More