how to animate collapse elements in flutter

前端 未结 4 1867
轻奢々
轻奢々 2020-12-07 17:06

How can i expand and collapse widget when user taps on different widget ( sibling or parent ) with animation ?

new Column(
    children: [
            


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 17:59

    If you want to collapse a widget to zero height or zero width that has a child that overflow when collapsed, I would recommend SizeTransition or ScaleTransition.

    Here is an example of the ScaleTransition widget being used to collapse the container for the four black buttons and status text. My ExpandedSection widget is used with a column to get the following structure.

    An example of a Widget that use animation with the SizeTransition widget:

    class ExpandedSection extends StatefulWidget {
    
      final Widget child;
      final bool expand;
      ExpandedSection({this.expand = false, this.child});
    
      @override
      _ExpandedSectionState createState() => _ExpandedSectionState();
    }
    
    class _ExpandedSectionState extends State with SingleTickerProviderStateMixin {
      AnimationController expandController;
      Animation animation; 
    
      @override
      void initState() {
        super.initState();
        prepareAnimations();
        _runExpandCheck();
      }
    
      ///Setting up the animation
      void prepareAnimations() {
        expandController = AnimationController(
          vsync: this,
          duration: Duration(milliseconds: 500)
        );
        animation = CurvedAnimation(
          parent: expandController,
          curve: Curves.fastOutSlowIn,
        );
      }
    
      void _runExpandCheck() {
        if(widget.expand) {
          expandController.forward();
        }
        else {
          expandController.reverse();
        }
      }
    
      @override
      void didUpdateWidget(ExpandedSection oldWidget) {
        super.didUpdateWidget(oldWidget);
        _runExpandCheck();
      }
    
      @override
      void dispose() {
        expandController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return SizeTransition(
          axisAlignment: 1.0,
          sizeFactor: animation,
          child: widget.child
        );
      }
    }
    

    AnimatedContainer also works but Flutter can complain about overflow if the child is not resizable to zero width or zero height.

提交回复
热议问题