Home > Back-end >  Use Child Drag Behavior in PageView
Use Child Drag Behavior in PageView

Time:09-21

I would like to use a Slider in a PageView without the page transitioning when dragged.

I have tried wrapping the Slider in a GestureDetector, and using HorizontalDragStart and HorizontalDragEnd to change the PageViews physics with no luck. Code below:

  bool pageViewDisabled = false;
  late PageView pageView;

  sliderChanged(double value) {
    setState(() {
      sliderValue = value;
    });
  }

  disablePageView() {
    setState(() {
      pageViewDisabled = true;
    });
  }

  enablePageView() {
    setState(() {
      pageViewDisabled = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    pageView = PageView(
      physics: pageViewDisabled ? NeverScrollableScrollPhysics() : AlwaysScrollableScrollPhysics(),
      controller: PageController(),
      children: <Widget>[
        Center(child: Text('Page One')),
        GestureDetector(
          onHorizontalDragStart: disablePageView(),
          onHorizontalDragEnd: enablePageView(),
          child: Slider(
            value: sliderValue,
            onChanged: sliderChanged,
          ),
        ),
        Center(child: Text('Page Three')),
      ],
    );
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: pageView
    );
  }

enter image description here

  • Related