flutter - correct way to create a box that starts at minHeight, grows to maxHeight

后端 未结 5 2040
时光说笑
时光说笑 2020-12-15 02:39

I have a container that I want to start off at a minimum size and grow (if its contents grow while user is adding content) to a maximum size, then stop.

The correct

5条回答
  •  忘掉有多难
    2020-12-15 03:06

    ConstrainedBox(
       constraints: BoxConstraints(
          minWidth: 70, 
          minHeight: 70,
          maxWidth: 150, 
          maxHeight: 150,
       ),
       child: Container(color: Colors.red, width: 10, height: 10),
    )
    

    You might guess that the Container has to be between 70 and 150 pixels, but you would be wrong. The ConstrainedBox only imposes additional constraints from those it receives from its parent.

    Here, the screen forces the ConstrainedBox to be exactly the same size as the screen, so it tells its child Container to also assume the size of the screen, thus ignoring its constraints parameter.

提交回复
热议问题