微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Wheel ListView 小部件内的颤动按钮

如何解决Wheel ListView 小部件内的颤动按钮

我使用的是更具体的 ListWheelScrollViewclickable。 (clickable_list_wheel_view)

我试图在我的 button添加一个 itemWidget,但我无法点击它。这是我的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,);

在我的 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),

一切都显示得很好,但从未在我的 onpressed调用 button。我猜 GestureDetector 正在覆盖按钮?有什么解决方法吗?我在这方面找不到任何内容

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,);
  }

解决方法

点击永远不会到达您的 GestureDetector,您需要在此处的 ClickableListWheelScrollView 中指定点击:

  onItemTapCallback: (index) {
print(index)

},

如果您拥有的打印件无法打印,请与 ClickableListWheelScrollView 插件发布者联系。您将需要逻辑来根据从 ClickableListWheelScrollView 传递的索引号通过按钮实现所需的更改,我认为......所以 setState 函数的映射可能会做到这一点。

把按钮想象成一个视图,你只是根据每个按钮在列表中的位置并行运行函数逻辑。按钮被埋在列表下面,你需要在ListWheel上面一层,也就是Clickable插件,但是很明显它变得和按钮断开了,所以你需要根据index值来创建逻辑来链接它们,也就是位置列表中的对象。

我通常做什么:

onSelectedItemChanged: (index) {
  // print(index);
},
  1. 这将返回列表中项目的索引位置。使用它来更改变量。

  2. 将您的滚动条包裹在 GestureDetector 中。

  3. 在手势检测器中,编写或调用一个函数,该函数接受您的索引位置变量并根据您的需要执行您的功能。

  4. 如果您需要控制滚动开始在列表中的位置,或者在重建时记住它的位置等,请使用带有 initialItem 的 FixedExtentScrollController。

一切都以这种方式从 onSelectedItemChanged 的​​索引脱离,并且您使用 FixedExtentScrollController 在构建时设置起始位置。请记住,列表从 0 开始。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。