【问题标题】:How to add haptic feedback to a scrollable widget in Flutter?如何将触觉反馈添加到 Flutter 中的可滚动小部件?
【发布时间】:2022-11-06 20:53:53
【问题描述】:

如何将触觉反馈添加到可滚动小部件?具体来说,在这两种情况下:

  • 当前正在滚动可滚动小部件时。
  • 当可滚动小部件到达可滚动区域的开始/结束边缘时。

第一种情况应该在整个事件期间经常发出轻微的触觉反馈,第二种情况应该发出一次更重的触觉反馈。

这很糟糕,因为它没有触觉反馈,但它是所需的布局:

SingleChildScrollView(
      child: Column(
        children: [
          Container(height: 100, color: Colors.redAccent),
          Container(height: 100, color: Colors.blue),
          Container(height: 100, color: Colors.green),
          Container(height: 100, color: Colors.deepOrange),
          Container(height: 100, color: Colors.purple),
        ],
      ),
    );

期望的结果与几个基于 Apple 的滚动视图的工作方式非常相似。例如,Apple Watch 会在您滚动时发出相同的触觉模式。

拥有触觉反馈将使用户的滚动体验非常愉快,并且还有助于解决应用程序中的可访问性问题。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我遇到了这个问题,刚刚为它创建了一个包:https://pub.dev/packages/scrollable

    为了让您的可滚动小部件发出触觉反馈,您可以将其包装在 ScrollHaptics 小部件中。默认情况下,这将起作用,但是,您也可以根据自己的喜好更改小部件的属性。

    这是添加了ScrollHaptics 的所提供示例的外观:

    ScrollHaptics( // <----- Added here!
          child: SingleChildScrollView(
            child: Column(
              children: [
                Container(height: 100, color: Colors.redAccent),
                Container(height: 100, color: Colors.blue),
                Container(height: 100, color: Colors.green),
                Container(height: 100, color: Colors.deepOrange),
                Container(height: 100, color: Colors.purple),
              ],
            ),
          ),
        );
    

    这是另一个版本,为了示例而更改了一些属性:

    ScrollHaptics( // <----- Added here, with example properties changed below.
          bubbleUpScrollNotifications: false,
          heavyHapticsAtEdgeEnabled: true,
          hapticEffectAtEdge: HapticType.medium,
          hapticEffectDuringScroll: HapticType.light,
          distancebetweenHapticEffectsDuringScroll: 55,
          child: SingleChildScrollView(
            child: Column(
              children: [
                Container(height: 100, color: Colors.redAccent),
                Container(height: 100, color: Colors.blue),
                Container(height: 100, color: Colors.green),
                Container(height: 100, color: Colors.deepOrange),
                Container(height: 100, color: Colors.purple),
              ],
            ),
          ),
        );
    

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      相关资源
      最近更新 更多