Make container widget fill parent vertically

前端 未结 6 993
天命终不由人
天命终不由人 2020-12-25 09:38

TL;DR Need the container to fill the vertical space so that it can act as a ontap listener. Have tried most solutions but nothing seems to work.

So what I am trying

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-25 10:31

    There are many answers which suggest using two things

    1. constraints: BoxConstraints.expand(),
    2. height: double.infinity,

    But both these answer will give you an error like

    BoxConstraints forces an infinite height.

    We can avoid these by calculating the height of the screen like

    1. App Bar
    2. Top Bar Space(Exist on the above App Bar)
    3. Remaining screen

    1. Get the MediaQuer

     final mediaQuery = MediaQuery.of(context);
    

    2. Declare the AppBar Widget and same App Bar instance should be used in Scaffold App Bar

    final PreferredSizeWidget appBar = AppBar(
          title: Text('Home'),
        );
    

    3. Use calculated height

          Container(
                  width: mediaQuery.size.width,
                  height: (mediaQuery.size.height -
                      appBar.preferredSize.height -
                      mediaQuery.padding.top),
                  color: Colors.red,
                ),
    

    Output:

提交回复
热议问题