Sizing elements to percentage of screen width/height

前端 未结 10 1735
情深已故
情深已故 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:30

    you can use MediaQuery with the current context of your widget and get width or height like this double width = MediaQuery.of(context).size.width double height = MediaQuery.of(context).size.height after that, you can multiply it with the percentage you want

    0 讨论(0)
  • 2020-11-30 17:31

    You could build a Column/Row with Flexible or Expanded children that have flex values that add up to the percentages you want.

    You may also find the AspectRatio widget useful.

    0 讨论(0)
  • 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: <Widget>[
                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

    0 讨论(0)
  • 2020-11-30 17:35

    if you are using GridView you can use something like Ian Hickson's solution.

    crossAxisCount: MediaQuery.of(context).size.width <= 400.0 ? 3 : MediaQuery.of(context).size.width >= 1000.0 ? 5 : 4
    
    0 讨论(0)
  • 2020-11-30 17:36

    Use the LayoutBuilder Widget that will give you constraints that you can use to obtain the height that excludes the AppBar and the padding. Then use a SizedBox and provide the width and height using the constraints from the LayoutBuilder

    return LayoutBuilder(builder: (context2, constraints) {
      return Column(
        children: <Widget>[
          SizedBox(
            width: constraints.maxWidth,
            height: constraints.maxHeight,
            ...
    
    0 讨论(0)
  • 2020-11-30 17:37

    This is a supplemental answer showing the implementation of a couple of the solutions mentioned.

    FractionallySizedBox

    If you have a single widget you can use a FractionallySizedBox widget to specify a percentage of the available space to fill. Here the green Container is set to fill 70% of the available width and 30% of the available height.

    FractionallySizedBox
    Widget myWidget() {
      return FractionallySizedBox(
        widthFactor: 0.7,
        heightFactor: 0.3,
        child: Container(
          color: Colors.green,
        ),
      );
    }
    

    Expanded

    The Expanded widget allows a widget to fill the available space, horizontally if it is in a row, or vertically if it is in a column. You can use the flex property with multiple widgets to give them weights. Here the green Container takes 70% of the width and the yellow Container takes 30% of the width.

    Expanded

    If you want to do it vertically, then just replace Row with Column.

    Widget myWidget() {
      return Row(
        children: <Widget>[
          Expanded(
            flex: 7,
            child: Container(
              color: Colors.green,
            ),
          ),
          Expanded(
            flex: 3,
            child: Container(
              color: Colors.yellow,
            ),
          ),
        ],
      );
    }
    

    Supplemental code

    Here is the main.dart code for your reference.

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text("FractionallySizedBox"),
            ),
            body: myWidget(),
          ),
        );
      }
    }
    
    // replace with example code above
    Widget myWidget() {
      return ...
    }
    
    0 讨论(0)
提交回复
热议问题