Skip to main content

Flutter Set onLongPress on ElevatedButton

In flutter ElevatedButton support onLongPress functionality by implementing onLongPress event. onLongPress actives the press or click event on continually pressing button for 1 second without releasing. We can fire any event or function on this event.

Flutter Set onLongPress on ElevatedButton:

1. Defining onLongPress on button and passing our function which we want to be execute on event.
ElevatedButton(
        style: ElevatedButton.styleFrom(
          backgroundColor: Colors.purple,
          textStyle: const TextStyle(fontSize: 24),
        ),
        onLongPress: tempFunction,
        onPressed: () {},
        child: const Text('On Long Press Button'),
      ),
2. Making a temporary function with print message log on screen.
  void tempFunction() {
    print('onLongPress Trigger...');
  }
3. Complete source code for main.dart file:
import 'package:flutter/material.dart';

void main() => runApp(const App());

class App extends StatelessWidget {
  const App({super.key});

  void tempFunction() {
    print('onLongPress Trigger...');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Center(
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          backgroundColor: Colors.purple,
          textStyle: const TextStyle(fontSize: 24),
        ),
        onLongPress: tempFunction,
        onPressed: () {},
        child: const Text('On Long Press Button'),
      ),
    )));
  }
}
Screenshot:
Flutter Set onLongPress on ElevatedButton

Comments