SardorbekR
SardorbekR

Reputation: 1778

Pick Value onTap in CupertinoPicker and CupertinoDatePicker

onSelectedItemChanged is working perfectly in CupertinoPicker/CupertinoDatePicker

But I also want to pick value when user clicked the value.

Currently, user must scroll in order to pick the value, and as far as I know CupertinoPicker/CupertinoDatePicker doesn't have onTap, onPressed functions

How can I solve this issue

enter image description here

Upvotes: 2

Views: 1612

Answers (3)

Serkan Ağca
Serkan Ağca

Reputation: 271

GestureDetector(
    behavior: HitTestBehavior.translucent,
    onTap: () => // write what you want to do,
    child: CupertinoPicker(),
  ),

Upvotes: 1

Deepak Gupta
Deepak Gupta

Reputation: 658

I found a workaround by using flutter_portal.

  final ScrollController scrollController = FixedExtentScrollController();

  @override
  Widget build(BuildContext context) {
    return Container(
        height: 100,
        child: Portal(
          child: CupertinoPicker.builder(
            scrollController: scrollController,
            childCount: itemCount,
            itemBuilder: (context, index) {
              return PortalTarget(
                anchor: Aligned(
                  follower: Alignment.center,
                  target: Alignment.center,
                ),
                portalFollower: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    scrollController.animateToItem(index,
                        duration: Duration(milliseconds: 250),
                        curve: Curves.decelerate);
                  },
                ),
                child: Text(index.toString()),
              );
            },
          ),
        )
    );
  }

Upvotes: 0

yellowgray
yellowgray

Reputation: 4509

Unfortunately, the gesture detection inside CupertinoPicker/CupertinoDatePicker is not supported for now. When you trace the code inside CupertinoPicker, it leads to use ListWheelScrollView at the end and it does not respond to the tap event.

Discussion thread on GitHub:

There is a workaround solution by using package clickable_list_wheel_view (fixed height for the child widget, mentioned here)

Upvotes: 3

Related Questions