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

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

我不知道怎么了...

当我使用 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 动态设置高度?

改用didChangeDependencies

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