Sizing elements to percentage of screen width/height

前端 未结 10 1749
情深已故
情深已故 2020-11-30 17:11

Is there a simple (non-LayoutBuilder) way to size an element relative to screen size (width/height)? For example: how do I set the width of a CardView to be 65% of the scree

10条回答
  •  执念已碎
    2020-11-30 17:34

    There is many way to do this.

    1. Using MediaQuery : Its return fullscreen of your device including appbar,toolbar

     Container(
            width: MediaQuery.of(context).size.width * 0.50,
            height: MediaQuery.of(context).size.height*0.50, 
            color: Colors.blueAccent[400],
     )
    




    2. Using Expanded : You can set width/height in ratio

     Container(
            height: MediaQuery.of(context).size.height * 0.50,
            child: Row(
              children: [
                Expanded(
                  flex: 70,
                  child: Container(
                    color: Colors.lightBlue[400],
                  ),
                ),
                Expanded(
                  flex: 30,
                  child: Container(
                    color: Colors.deepPurple[800],
                  ),
                )
              ],
            ),
        )
    



    3. Others Like Flexible and AspectRatio and FractionallySizedBox

提交回复
热议问题