简体   繁体   English

Flutter Wheel ListView 小部件内的按钮

[英]Flutter Button inside a Wheel ListView Widget

I am using a ListWheelScrollView more specific a clickable one.我正在使用更具体的ListWheelScrollView一个clickable的。 ( clickable_list_wheel_view ) ( clickable_list_wheel_view )

I am trying to have a button inside my itemWidget but I can not click it.我试图在我的itemWidget中有一个button ,但我无法单击它。 This is my list :这是我的list

return ClickableListWheelScrollView(
  scrollController: _scrollController,
  itemHeight: _itemHeight,
  itemCount: months.length,
  scrollOnTap: true,
  onItemTapCallback: (index) {
    print(index)
  },
  child: ListWheelScrollView.useDelegate(
    controller: _scrollController,
    itemExtent: _itemHeight,
    physics: FixedExtentScrollPhysics(),
    diameterRatio: 3,
    squeeze: 0.95,
    onSelectedItemChanged: (index) {
      // print(index);
    },
    childDelegate: ListWheelChildBuilderDelegate(
      builder: (context, index) => MonthCardWidget(
        month: months[index],
      ),
      childCount: months.length,
    ),
  ),
);

Inside my MonthCardWidget I have this simple button :在我的MonthCardWidget ,我有这个简单的button

          Container(
            height: 45,
            width: 45,
            color: Colors.transparent,
            child: IconButton(
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent,
              onPressed: () {
                print('flip');
              },
              icon: SvgPicture.asset('assets/images/flip.svg',
                  height: 20, width: 20),
            ),
          ),

Everything is getting displayed just fine but onPressed is never called on my button .一切都显示得很好,但我的button上从未调用过onPressed I guess the GestureDetector is overriding the button?我猜GestureDetector覆盖了按钮? Is there any workaround for this?有什么解决方法吗? I couldn't find anything on this.我在这方面找不到任何东西。

The GestureDetector is simply on top of the listView : GestureDetector只是在listView的顶部:

@override
  Widget build(BuildContext context) {
    if (_listHeight == .0) {
      return MeasureSize(
        child: Container(
          child: widget.child,
        ),
        onChange: (value) {
          setState(() {
            _listHeight = value.height;
          });
        },
      );
    }

    return GestureDetector(
      onTap: _onTap,
      onTapUp: (tapUpDetails) {
        _tapUpDetails = tapUpDetails?.localPosition;
      },
      child: widget.child,
    );
  }

The click will never reach your GestureDetector, you need to specify the click in the ClickableListWheelScrollView here:点击永远不会到达您的 GestureDetector,您需要在此处指定 ClickableListWheelScrollView 中的点击:

  onItemTapCallback: (index) {
print(index)

}, },

If the print you have there isn't printing, then get with the ClickableListWheelScrollView plugin publisher.如果您的印刷品没有印刷,请使用 ClickableListWheelScrollView 插件发布者。 You'll need logic to reach the changes you want with the button based on the index number passed from the ClickableListWheelScrollView I think....so a map of setState functions might do it.您需要逻辑来根据从 ClickableListWheelScrollView 传递的索引号使用按钮来实现您想要的更改,我认为......所以 setState 函数的 map 可能会做到这一点。

Think of the button just being a view, and you're just running the function logic in parallel to each button based on its position in the list.认为按钮只是一个视图,您只是基于列表中的 position 与每个按钮并行运行 function 逻辑。 The button is buried under the list, you need a layer above the ListWheel, which is the Clickable plugin, but obviously it becomes disconnected from the button, so you need to create logic to link them based on the index value, which is the position of the object in the list.按钮埋在列表下面,需要在ListWheel上面加一层,就是Clickable插件,但是显然它和按钮断开了,所以需要根据索引值创建逻辑来链接它们,也就是position列表中的 object。

What I typically do:我通常会做什么:

onSelectedItemChanged: (index) {
  // print(index);
},
  1. This Will return the index position of the item in your list.这将返回列表中项目的索引 position。 Use this to change a variable.使用它来更改变量。

  2. Wrap your scroller in a GestureDetector.将滚动条包裹在 GestureDetector 中。

  3. In the Gesture Detector, write or call a function that takes your index position variable and performs your function as you desire.在 Gesture Detector 中,编写或调用 function 以获取索引 position 变量并根据需要执行 function。

  4. If you need to control where in your list position the scroll begins, or remember its position when you rebuild etc, use a FixedExtentScrollController with an initialItem.如果您需要控制列表 position 中滚动开始的位置,或者在重建时记住它的 position 等,请使用带有 initialItem 的 FixedExtentScrollController。

Everything slaves off the index of the onSelectedItemChanged in this way, and you use the FixedExtentScrollController to set a starting position at build.一切都以这种方式从 onSelectedItemChanged 的索引中脱离出来,并且您使用 FixedExtentScrollController 在构建时设置起始 position。 Remember that the list begins at 0.请记住,列表从 0 开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM