How to lock page scrolling when moving a slider

我的未来我决定 提交于 2019-12-24 20:18:45

问题


In Flutter, I have an app with a custom Slider inside a custom ListView widget. The only problem is that when you move the slider handle (left to right), the page will still scroll (up and down) due to the ListView, whereas I would need the page to lock in place until the user stops moving the slider.

I started off by having a look at the material app slider code but couldn't find anything that would help. I've also played around with the slider and it has the effect that I am trying to achieve, which tells me the problem lies in the my custom slider. *

I am using a Listener in the slider widget to detect gestures so I would assume that I am missing a few lines in there somewhere? Any help would be appreciated, cheers.

Update: * I have also tried changing the behaviour of the listener but that hasn't seemed to solve the problem.

Here is the slider within the ListView - Note: the slider can have multiple handles (hence why it's custom) but in this case just showing a single to avoid confusion.

@override
  Widget build(BuildContext context) {
    return ListView(children: <Widget>[
    Center(
     child: StagedSlider.single(
      value: point,
      //A function which changes the handle value
      onChanged: setHandleInfo,
     ),
    ),
...

Here is the listener inside the slider:

return Listener(
      behavior: HitTestBehavior.translucent,
      //The following return functions to calculate handle
      //position/progress...
      onPointerDown: someFunction(),
      onPointerMove: someFunction(),
      onPointerUp: someFunction(),
      //Child is a stack of handles/thumbs calculated higher up
      child: handles,
      ),
    );

Just as an example, if you were to move the slider handle in a vertical-upwards motion, the handle would move along the horizontal axis and the whole page would move down vertically. On the other handle, the Material App Slider does not do this and in this case would only move the handle.

Update2: So a quick fix for this would be to just use a Gesture Detector instead of a Listener. I tried it out and all the issues seemed to have been fixed. However, due to another unrelated issue I was having earlier if someone had a solution using the Listener that would be great.


回答1:


If I understood correctly, you could use the method onChangeEnd of the Slider to update the position of the ListView and the method onChange of the Slider to update the position of the Slider

The same approach could be done using a Listener, using the method onPointerUp to update the position of the ListView and the method onPointerMove to update the position of the Slider




回答2:


So after a days work I've cracked two birds with one stone and found a solution which solves both this and my previous post here

Just wanted to leave an answer incase anyone else comes across this and has similar problems.

I basically used the RawGestureDetector below:

class _MyGestureDetector extends OneSequenceGestureRecognizer {
  final Function onHorizontalDragDown;
  final Function onHorizontalDragUpdate;
  final Function onHorizontalDragUp;

  _SingleDeviceGestureDetector({
    @required this.onHorizontalDragDown,
    @required this.onHorizontalDragUpdate,
    @required this.onHorizontalDragUp,
  });

  @override
  void addPointer(PointerEvent event) {
    //Returns true or false depending on whether there
    //is a pointer on screen already
    if (onHorizontalDragDown(event)) {
      startTrackingPointer(event.pointer);
      resolve(GestureDisposition.accepted);
    } else {
      stopTrackingPointer(event.pointer);
    }
  }

  @override
  void handleEvent(PointerEvent event) {
    //If the pointer is being dragged
    if (event is PointerMoveEvent) {
      //Sends the position of the drag
      onHorizontalDragUpdate(event.position);
    }
    //If the pointer is being lifted
    else if (event is PointerUpEvent) {
      onHorizontalDragUp(event);
      stopTrackingPointer(event.pointer);
    }
  }

  @override
  String get debugDescription => 'singlePointerDrag';

  @override
  void didStopTrackingLastPointer(int pointer) {}
}


来源:https://stackoverflow.com/questions/58299959/how-to-lock-page-scrolling-when-moving-a-slider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!