Aoi-hosizora / flutter_ahlib

A personal flutter library, contains some useful widgets and utils.
MIT License
3 stars 0 forks source link

AppendIndicator done_before record #11

Closed Aoi-hosizora closed 3 years ago

Aoi-hosizora commented 3 years ago
enum _AppendIndicatorMode {
  drag, // start to show
  armed, // drag far enough
  append, // run the append callback
  done_before, // before append callback is done
  done, // append callback is done
  canceled, // canceled by no arming
}
  static const _kScaleSlowDuration = Duration(milliseconds: 400);
  static const _kScaleFastDuration = Duration(milliseconds: 100);
  static const _kProgressFadeDuration = Duration(milliseconds: 100);
  static const _kProgressIncrDuration = Duration(milliseconds: 150);
  static const _kProgressFinalDuration = Duration(milliseconds: 150);
  AnimationController _progressController;
  Animation<double> _progressAnimation;

  void initState() {
    super.initState();
    _sizeController = AnimationController(vsync: this);
    _sizeFactor = _sizeController.drive(Tween(begin: 0.0, end: 1.0));
    _progressController = AnimationController(vsync: this);
    _progressAnimation = _progressController.drive(Tween(begin: 0.0, end: 1.0));
  }

  void _start() {
    _sizeController.value = 0.0;
    _progressController.value = 0.0;
    _dragOffset = 0;
  }
  /// Show animation with "-> fade -> increasing -> shrink" or "-> shrink".
  void _dismiss(_AppendIndicatorMode newMode) async {
    if (newMode == _AppendIndicatorMode.canceled) {
      // -> cancel
      _mode = _AppendIndicatorMode.canceled;
      if (mounted) setState(() {});
    } else if (newMode == _AppendIndicatorMode.done) {
      // -> done_before
      _mode = _AppendIndicatorMode.done_before;
      if (mounted) setState(() {});
      await Future.delayed(_kProgressFadeDuration); // fade
      await _progressController.animateTo(1.0, duration: _kProgressIncrDuration); // increasing
      await Future.delayed(_kProgressFinalDuration);
      // -> done
      _mode = _AppendIndicatorMode.done;
      if (mounted) setState(() {});
    }
    await _sizeController.animateTo(0.0, duration: _kScaleSlowDuration, curve: Curves.easeOutCubic); // shrink

    // -> null
    _mode = null;
    if (mounted) setState(() {});
  }
SizeTransition(
  sizeFactor: _sizeFactor,
  axis: Axis.horizontal,
  child: AnimatedCrossFade(
    duration: _kProgressFadeDuration,
    crossFadeState: _mode == _AppendIndicatorMode.done_before || _mode == _AppendIndicatorMode.done ? CrossFadeState.showSecond : CrossFadeState.showFirst,
    firstChild: LinearProgressIndicator(
      value: _mode == _AppendIndicatorMode.append || _mode == _AppendIndicatorMode.done_before || _mode == _AppendIndicatorMode.done ? null : 0,
      minHeight: widget.minHeight,
      backgroundColor: widget.backgroundColor,
      valueColor: widget.valueColor,
    ),
    secondChild: AnimatedBuilder(
      animation: _progressAnimation,
      builder: (_, __) => LinearProgressIndicator(
        value: _progressAnimation.value,
        minHeight: widget.minHeight,
        backgroundColor: widget.backgroundColor,
        valueColor: widget.valueColor,
      ),
    ),
  ),
),