MarcelGarus / marquee

A Flutter widget that scrolls text infinitely. Provides many customizations including custom scroll directions and velocities, pausing after every round and specifying custom durations and curves for accelerating and decelerating.
MIT License
284 stars 135 forks source link

What the use of `_makeRoundTrip` function #15

Closed izayl closed 5 years ago

izayl commented 5 years ago
  /// Causes the controller to scroll one round.
  Future<void> _makeRoundTrip() async {
    // Reset the controller, then accelerate, move linearly and decelerate.
    _controller.jumpTo(_startPosition);
    if (!_running) return;

    await _accelerate();
    if (!_running) return;

    await _moveLinearly();
    if (!_running) return;

    await _decelerate();
    if (!_running) return;

    await Future.delayed(widget.pauseAfterRound);
  }

I'm confused to this _makeRoundTrip function, why there are three phase: accelerate/ linear/ decelerate?

my version for implement auto scroll is just use a infinite loop:

  @override
  void initState() {
    super.initState();

    _scrollController = ScrollController(initialScrollOffset: 0);
    _initState();

    Future.delayed(Duration(microseconds: 5000), () {
      Future.doWhile(() async {
        await _scroll();
        return true;
      });
    });
  }

  Future<void> _scroll() async {
    await _scrollController.animateTo(_scrollController.offset + (delta * 4),
        curve: Curves.linear, duration: Duration(seconds: 1));
  }

But this loop will cost more CPU load.

MarcelGarus commented 5 years ago

This function scroll only one round trip. If you provide an acceleration or deceleration curve and duration, you'll see that the marquee slowly starts to scroll, then scrolls linearly and then, finally stops. Example:

Marquee(
  text: 'Some sample text that takes some space.',
  pauseAfterRound: Duration(seconds: 1),
  accelerationDuration: Duration(seconds: 1),
  accelerationCurve: Curves.linear,
  decelerationDuration: Duration(milliseconds: 500),
  decelerationCurve: Curves.easeOut,
)