18

There is a function or method in the Flutter framework, which can be used to ajust the animation/running speed of every widget.

This is possible using I think a service.

I just forgot how I can call it and could not find any resources that describe it + I do not know where I once discovered it.

There is not really more information to provide as this is just a simple one liner. I hope that someone knows what I am talking about.

3
  • do you mean the slow motion option in the flutter Gallery app ? Jun 30, 2018 at 16:57
  • 1
    @RaoufRahiche I could have looked at that source if I knew about it :) I wanted to say that I knew about timeDilation some time back, but forgot the package and property names :) Jun 30, 2018 at 19:40
  • Can we use the timeDilation property to control the feedback of Draggable when it is not accepted by the DropTarget?
    – Rishabh
    Jan 9, 2019 at 6:37

1 Answer 1

53

You need set the timeDilation static property:

import 'package:flutter/scheduler.dart' show timeDilation;
// you can also import the whole file:
// import 'package:flutter/scheduler.dart'; 

...

timeDilation = 2.0; // Will slow down animations by a factor of two

I am using show in my import because it limits the import to certain declarations from the library. In this context I only want to be able to use timeDilation from the scheduler.dart library, and nothing else. Since schedulers are pretty low-level things, this makes sense to not pollute the namespace. There's also hide that has the opposite effect (only hides certain declarations).

You can set this from anywhere in your app, even in the main function:

import 'package:flutter/scheduler.dart' show timeDilation;

void main() {
  timeDilation = 3.0;
  runApp(new MyApp());
}

or in your pressed handler:

onPressed: () => timeDilation = 2.0

This is a global static property so you don't need to call setState in order for the changes to take place.

3
  • 12
    > it should be very obvious that setState does not need to be called -- maybe for you, not for everyone; > Animations are not connected to state -- animations are connected to the state, scheduling isn't, isn't that right? > I think that onPressed is a strange addition. Maybe remove that -- I'm sorry that my answer contains more information than just timeDilation from 'package:flutter/scheduler.dart'. I think an example is useful for people looking for information about time dilation in general. Why is it too much and explaining show` isn't?
    – wasyl
    Jun 30, 2018 at 19:56
  • 1
    Sorry, I did not mean it in a negative way. I just think that some parts were strange, e.g. onPressed is completely out of context and I still do not know what show does in your code :) You asked about scheduling and yes that is of course what I tried to say because that is the part of the animation that makes it an animation. Gave you an upvote now, but I still want to know what show does :) I do not want you to explain the concept of show, it would just be nice to explain why you chose to include it (I mean it is obvious if you know what it does). Jun 30, 2018 at 21:27
  • 8
    Sorry if I came out overtly negative as well, I just wanted the answer to be more accessible somehow. As for show, it limits import to certain declarations from the library -- in this context I only want to be able to use timeDilation from the scheduler.dart library, and nothing else. Since schedulers are pretty low-level thing this makes sense to not pollute the namespace. There's also hide that has the opposite effect (only hides certain declarations)
    – wasyl
    Jun 30, 2018 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.