Uday
Uday

Reputation: 1699

How to disable the Cupertinopicker in flutter?

I have a CupertinoPicker, on some condition I have to disable the CupertinoPicker.

Checked the CupertinoPicker class and didn't find any disable attribute in that.

If disabling is not possible, Can I Stop scroll on it?

CupertinoPicker(
          backgroundColor: null,
          itemExtent: PICKER_EXTENT,
          useMagnifier: true,
          looping: true,
          onSelectedItemChanged: (int index) {
            print('selected index $index');
          },
          children: List<Widget>.generate(dataList.length, (int index) {
            return Center(
              child: Text(dataList[index]),
            );
          }),
          scrollController:
              FixedExtentScrollController(initialItem: selectedIndex)),

Upvotes: 3

Views: 1926

Answers (1)

Darish
Darish

Reputation: 11481

You can use the AbsorbPointer to enable/disable touch events for any widget. As per the documentation

When absorbing is true, this widget prevents its subtree from receiving pointer events by terminating hit testing at itself. It still consumes space during layout and paints its child as usual. It just prevents its children from being the target of located events, because it returns true from RenderBox.hitTest.

In your case, wrap the CupertinoPicker with an AbsorbPointer and use the absorbing property to enable/disable touch events for your CupertinoPicker

 AbsorbPointer(
  absorbing: true,
  child: CupertinoPicker(
      backgroundColor: null,
      itemExtent: 100.0,
      useMagnifier: true,
      looping: true,
      onSelectedItemChanged: (int index) {
        print('selected index $index');
      },
      children: List<Widget>.generate(dataList.length, (int index) {
        return Center(
          child: Text(dataList[index]),
        );
      }),
      scrollController:
          FixedExtentScrollController(initialItem: selectedIndex)),
);

You can

Upvotes: 4

Related Questions