Flutter/Dart How to adjust Modalbottomsheet animation speed?

2,748

If you are using a StatefulWidget add with TickerProviderStateMixin and create an AnimationController with BottomSheet.createAnimationController(this). You can then set the duration on the AnimationController. In this example I've set the duration to 3 seconds.

Make sure you dispose the AnimationController in 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"),
          );
        },
      ),
    );
  }
}
Share:
2,748
Hajime
Author by

Hajime

Updated on December 28, 2022

Comments

  • Hajime
    Hajime over 1 year

    I read this reference

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

    it says "transitionAnimationController parameters can be passed in to customize the appearance and behavior of modal bottom sheets. The transitionAnimationController controls the bottom sheet's entrance and exit animations if provided."

    but, I couldn't find any reference of transitionAnimationController,

    so my question is, How can I adjust ModalBottomSheet Animation(entrance and exit speed that I want to adjust) with transitionAnimationController?

    thank you.

  • Hajime
    Hajime about 3 years
    thank you so much, it solved my problem. you are the best!
  • Lidor Eliyahu Shelef
    Lidor Eliyahu Shelef almost 3 years
    it solves the problem but after you close it it wont let you reopen it for some assertion error
  • Angelica
    Angelica almost 3 years
    Exception caught by gesture ══════════════════════════════════════════════ The following assertion was thrown while handling a gesture: AnimationController.forward() called after AnimationController.dispose() AnimationController methods should not be used after calling dispose. 'package:flutter/src/animation/animation_controller.dart': Failed assertion: line 455 pos 7: '_ticker != null'
  • lokesh-coder
    lokesh-coder almost 3 years
    @Angelica I got the same issue. temporary solution is to add listener again in onComplete showModalBottomSheet().whenComplete(() {controller =BottomSheet.createAnimationController(this);});
  • makri aymen abderraouf
    makri aymen abderraouf over 2 years
    how can I change the closing animation of bottomSheet!
  • Moh. Absar Rahman
    Moh. Absar Rahman about 2 years
    For changing the closing animation, you have to use reverse duration controller = BottomSheet.createAnimationController(this); controller.duration = Duration(seconds: 3); controller.reverseDuration = Duration(seconds: 3);