how to animate collapse elements in flutter

前端 未结 4 1872
轻奢々
轻奢々 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:43

    Alternatively you can just use an AnimatedContainer to mimic this behavior.

    class AnimateContentExample extends StatefulWidget {
      @override
      _AnimateContentExampleState createState() => new _AnimateContentExampleState();
    }
    
    class _AnimateContentExampleState extends State {
      double _animatedHeight = 100.0;
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(title: new Text("Animate Content"),),
          body: new Column(
            children: [
              new Card(
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    new GestureDetector(
                      onTap: ()=>setState((){
                        _animatedHeight!=0.0?_animatedHeight=0.0:_animatedHeight=100.0;}),
                      child:  new Container(
                      child: new Text("CLICK ME"),
                      color: Colors.blueAccent,
                      height: 25.0,
                        width: 100.0,
                    ),),
                    new AnimatedContainer(duration: const Duration(milliseconds: 120),
                      child: new Text("Toggle Me"),
                      height: _animatedHeight,
                      color: Colors.tealAccent,
                      width: 100.0,
                    )
                  ],
                ) ,
              )
            ],
          ),
        );
      }
    }
    

提交回复
热议问题