Flutter PageView how to make faster animations on swipe

前端 未结 1 995
被撕碎了的回忆
被撕碎了的回忆 2021-01-22 18:39

I have a simple PageView:

PageView(
  controller: _pageController,
  physics: PlatformScrollPhysics.getPlatformScrollPhysics(),
  children: [
    Text("I am          


        
1条回答
  •  借酒劲吻你
    2021-01-22 19:31

    cs guy's answer is right on track. Also credit to pskink.The PageView widget's transition speed isn't based on an animation curve even though the programmatic approach is.

    PageView actually uses a Spring Simulation to handle the page transitions when used with swiping, so the "physics" property has to be overridden to change the "animation speed".

    Here is a simple way to increase the "animation speed" by using custom Scroll Physics class.

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/physics.dart';
    
    class CustomPageViewScrollPhysics extends ScrollPhysics {
      const CustomPageViewScrollPhysics({ScrollPhysics parent})
          : super(parent: parent);
    
      @override
      CustomPageViewScrollPhysics applyTo(ScrollPhysics ancestor) {
        return CustomPageViewScrollPhysics(parent: buildParent(ancestor));
      }
    
      @override
      SpringDescription get spring => const SpringDescription(
            mass: 80,
            stiffness: 100,
            damping: 1,
          );
    }
    

    It can be used in the PageView constructor like this:

    PageView(... physics: const CustomPageViewScrollPhysics(),)
    

    And feel free to adjust any of the spring parameters!

    0 讨论(0)
提交回复
热议问题