Search code examples
flutterdartslider

flutter silder reach to end after 1 swipe in viewPager


I am using Slider on my project, I have a PageView, Its contains 3 pages and when I change the page I want to slider 1 step moves forward so I catch viewPager page from its controller:

pageViewController.addListener(() {
  controller.setPagIndex((pageViewController.page! + 1));
});

after swiping I rebuild Slider:

child: Slider(
  min: 1,
  max: state.length.toDouble(), // = 3
  value: controller.currentPageValue.value,
  divisions: 1,
  label: "${controller.currentPageValue.value.toInt()}",
  activeColor: mainRed,
  thumbColor: mainYellow,
  inactiveColor: backgroundGray,
  onChanged: (newValue) {},
),

But after 1 swipe and slider value being 2 the slider reaches to end?

If I remove divisions it's ok and the codes work. the problem is because divisions.

enter image description here


Solution

  • divisions in a slider is the total number of equally divided sections in the slider. In your case you have 3 steps so the minimum division should be 2. Which will create 3 points and 2 sections in the slider.

    child: Slider(
      min: 1,
      max: state.length.toDouble(), // = 3
      value: controller.currentPageValue.value,
      divisions: 2,//<-here
      label: "${controller.currentPageValue.value.toInt()}",
      activeColor: mainRed,
      thumbColor: mainYellow,
      inactiveColor: backgroundGray,
      onChanged: (newValue) {},
    ),