Pick Value onTap in CupertinoPicker and CupertinoDatePicker

Viewed 739

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

3 Answers

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)

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()),
              );
            },
          ),
        )
    );
  }
GestureDetector(
    behavior: HitTestBehavior.translucent,
    onTap: () => // write what you want to do,
    child: CupertinoPicker(),
  ),
Related