IgnorePointer widget and Attributes

IgnorePointer widget and Attributes

ยท

2 min read

The IgnorePointer widget in Flutter is used to make its subtree completely insensitive to user gestures, making it "ignore" any taps, drags, or other gestures.

Attributes:

  1. ignoring (bool):

    • Determines whether the subtree should be ignored or not.

    • If set to true, the widget and its subtree will be insensitive to user gestures.

    • If set to false, the widget and its subtree will respond to user gestures.

  2. ignoringSemantics (bool):

    • If true, the subtree is excluded from the semantics tree.

    • This can be useful when you want to hide certain interactive elements from accessibility services.

Example:

import 'package:flutter/material.dart';

class IgnorePointerExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('IgnorePointer Example'),
      ),
      body: Center(
        child: IgnorePointer(
          ignoring: true, // Set to true to make the child insensitive to gestures
          child: ElevatedButton(
            onPressed: () {
              // This won't be triggered when IgnorePointer is set to true
              print('Button Pressed!');
            },
            child: Text('Click Me'),
          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(
    MaterialApp(
      home: IgnorePointerExample(),
    ),
  );
}

In this example, an IgnorePointer widget is used to wrap an ElevatedButton. When ignoring is set to true, the button becomes insensitive to user gestures, and the onPressed callback won't be triggered. When ignoring is set to false, the button becomes interactive again. This can be useful in scenarios where you want to temporarily disable user interactions for a specific part of your UI.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย