2

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)),
Uday
  • 1,619
  • 3
  • 23
  • 48

1 Answers1

3

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

Darish
  • 11,032
  • 5
  • 50
  • 70