initState method

  1. @override
void initState()
override

Called when this object is inserted into the tree.

The framework will call this method exactly once for each State object it creates.

Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).

If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:

  • In initState, subscribe to the object.
  • In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
  • In dispose, unsubscribe from the object.

You should not use BuildContext.dependOnInheritedWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.dependOnInheritedWidgetOfExactType can be used there.

Implementations of this method should start with a call to the inherited method, as in super.initState().

Implementation

@override
void initState() {
  super.initState();
  childSize = widget.childs!.length;

  if (childSize <= 3) {
    _animationControl = AnimationController(vsync: this, duration: Duration(milliseconds: widget.duration));
  } else {
    _animationControl = AnimationController(vsync: this, duration: Duration(milliseconds: widget.duration * (childSize / 2).floor()));
  }

  Animation<double>? heightAnimation; //高度animation
  Animation<double> unfoldAnimation; //展开animation
  //折叠状态 0-1展开 0为折叠着  1为展开
  // _animationControl = 0;/// default
  if (!widget.foldState) {
    //展开状态 1为展开 下一个动画是 (1-0) 0为折叠,
    _animationControl.value = 1;
  }
  unfoldAnimation = _animationControl.drive(CurveTween(curve: Curves.easeInOut));
  heightAnimation = CurvedAnimation(
    parent: _animationControl,
    curve: Cubic(0.75, 0.82, 0.08, 1.25),
  );
  if (childSize > 1) {
    double interval = 1.0 / (childSize);
    _unfoldAnimations = List.generate(childSize, (index) {
      /// (Tween(begin: 0, end: .5)); //首个
      /// Tween(begin: -.50,end: .5));//中间
      /// (Tween(begin: -.5, end: 0)); //最后
      double begin, end = 0;
      if (index == 0) {
        begin = 0;
        end = 0.5;
      } else if (index == childSize - 1) {
        begin = -.5;
        end = 0;
      } else {
        begin = -.5;
        end = .5;
      }
      Tween<double> foldTween = Tween(begin: begin, end: end);
      return foldTween.chain(CurveTween(curve: Interval(index * interval, (index + 1) * interval))).animate(unfoldAnimation);
    });

    if (heightAnimation != null) {
      /// 第一个item高度不用变
      interval = 1.0 / (childSize - 1);
      _heightAnimations = List.generate(childSize - 1, (index) {
        var animatable;
        if (index == childSize - 2) {
          animatable = CurveTween(curve: IntervalOver1(index * interval, (index + 1) * interval));
          animatable.chain(CurveTween(curve: Curves.easeOutBack));
        } else {
          animatable = CurveTween(curve: IntervalSafe(index * interval, (index + 1) * interval));
        }
        return animatable.animate(heightAnimation!);
      });
    }
  }

  _animationControl.addListener(() {
    setState(() {});
  });

  // _animationControl.repeat(reverse: false);
}