Flutter Button onLongPress() Tutorial

Flutter Button on Long Press

In this tutorial, we will learn to execute a function when user long presses a button.

To execute a set of instructions for long-press action, use onLongPress property.

We shall use RaisedButton for a button. The working should be same even for other types of Buttons.

Sample Code Snippet

Following is a sample code snippet to use onLongPress property with RaisedButton.

  onLongPress: () => {
	//do something
  child: new Text('Click me'),


This is an example Flutter Application to demonstrate on how to respond for long press action on a button.

To recreate this example, create an empty Flutter Application and replace main.dart with the following code.


import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new MyApp(),

class MyApp extends StatefulWidget {
  _State createState() => new _State();

class _State extends State<MyApp> {
  int count = 0;

  void incrementCounter() {
    setState(() {

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Flutter Tutorial - googleflutter.com'),
      body: new Center(
        child: new RaisedButton(
          onLongPress: () => {
          child: new Text('Button Clicks - ${count}'),

When you run this Flutter application, the following UI should be displayed in the mobile. When you long press on the button, we shall increment the count in the button.


In this Flutter Tutorial, we learned how to respond for a long press on a button.

Desclaimer: We are not affiliated, associated, authorized, endorsed by, or in any way officially connected with the Google, Apple or Flutter, or any of its subsidiaries or its affiliates. The names Google, Apple and Flutter as well as related names, marks, emblems and images are registered trademarks of their respective owners. This site googleflutter.com covers tutorials related to Flutter developed by Google.