Flutter - AnimatedSize在将起始高度设置为MediaQuery高度时不动画

问题描述 投票:1回答:1

我无法弄清楚什么是错的......

当我使用静态高度为0.0的AnimatedSize小部件时会进行动画处理。但是当我设置topLayoutHeight = MediaQuery.of(context).size.height没有任何反应。

这是我的代码:

double topLayoutHeight = 0.0;

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

我的小部件:

@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,
                              )

如果我从来没有做setHeight()一切正常。

为什么我不能用MediaQuery.of(context).size.height动态设置高度?

flutter flutter-layout flutter-animation
1个回答
1
投票

请改用didChangeDependencies

@override
void didChangeDependencies() {
  super.didChangeDependencies();
  setState(() => topLayoutHeight = MediaQuery.of(context).size.height - 200);
}
© www.soinside.com 2019 - 2024. All rights reserved.