简体   繁体   中英

Flutter - AnimatedSize not animating when setting start height to MediaQuery height

I can't figure out whats wrong...

When I used a static height of 0.0 AnimatedSize widget would animate. But when i set the topLayoutHeight = MediaQuery.of(context).size.height nothing happens.

Here is my code:

double topLayoutHeight = 0.0;

setHeight(){
    topLayoutHeight = MediaQuery.of(context).size.height - 200;
}

my widget:

@override
  Widget build(BuildContext context) {
    setHeight();
    ...
    ...
    new AnimatedSize(
                            curve: Curves.fastOutSlowIn,
                            vsync: this,
                            child:
                              new ClipPath(
                                clipper: CustomShapeClipper(),
                                child: Container(
                                  decoration: BoxDecoration(
                                    gradient: LinearGradient(colors: [color1, color2]),
                                  ),
                                  height: topLayoutHeight,
                                ),
                              ), duration: new Duration(seconds: 2),
                          )


...
...

    new RaisedButton(
                                onPressed: () {
                                  setState(() {
                                    shrinkOnClick = false;
                                    topLayoutHeight = 360.0;
                                  });
                                },
                                shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.vertical(
                                      top: Radius.circular(15.0),
                                      bottom: Radius.circular(15.0)
                                    )),
                                child: new Text('Brands',
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 16,
                                    fontWeight: FontWeight.bold
                                  ),
                                ),
                                color: Color(0x33FFFFFF),
                                elevation: 0,
                              )

If I never do setHeight() everything works fine.

Why can't I set the height dynamically with MediaQuery.of(context).size.height ?

Use didChangeDependencies instead

@override
void didChangeDependencies() {
  super.didChangeDependencies();
  setState(() => topLayoutHeight = MediaQuery.of(context).size.height - 200);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM