Flutter/Dart 如何调整 Modalbottomsheet 动画速度?

Haj*_*ime 4 performance animation dart flutter flutter-showmodalbottomsheet

我读过这篇参考资料

https://api.flutter.dev/flutter/material/showModalBottomSheet.html

它说“可以传入transitionAnimationController参数来自定义模式底部表单的外观和行为。transitionAnimationController控制底部表单的进入和退出动画(如果提供)。”

但是,我找不到任何关于transitionAnimationController的参考,

所以我的问题是,如何使用transitionAnimationController调整ModalBottomSheet动画(我想调整的入口和出口速度)?

谢谢。

koh*_*kob 12

如果您使用的是 StatefulWidget,则添加with TickerProviderStateMixin并创建一个AnimationControllerwith BottomSheet.createAnimationController(this)duration然后您可以在AnimationController 上设置。在此示例中,我将持续时间设置为 3 秒。

确保将 AnimationController 放置在void dispose ()

class MyModalBottomButton extends StatefulWidget {
  @override
  _MyModalBottomButtonState createState() => _MyModalBottomButtonState();
}

class _MyModalBottomButtonState extends State<MyModalBottomButton>
    with TickerProviderStateMixin {
  AnimationController controller;

  @override
  initState() {
    super.initState();
    controller =
        BottomSheet.createAnimationController(this);
    controller.duration = Duration(seconds: 3);
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextButton(
      child: Text("Show bottom sheet"),
      onPressed: () => showModalBottomSheet(
        context: context,
        transitionAnimationController: controller,
        builder: (context) {
          return Container(
            child: Text("Your bottom sheet"),
          );
        },
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • @Angelica 我也遇到了同样的问题。临时解决方案是在 onComplete 中再次添加监听 ```showModalBottomSheet().whenComplete(() {controller =BottomSheet.createAnimationController(this);});``` (3认同)
  • 我怎样才能改变bottomSheet的关闭动画! (2认同)
  • 要更改关闭动画,您必须使用反向持续时间 `controller = BottomSheet.createAnimationController(this); 控制器.duration = 持续时间(秒:3);controller.reverseDuration = 持续时间(秒:3);` (2认同)