【问题标题】:Is there a way of setting dimensions of sizedbox according to dimensions of my device有没有办法根据我的设备尺寸设置 sizebox 的尺寸
【发布时间】:2021-08-30 04:46:51
【问题描述】:

我想根据设备的尺寸设置sizedbox的尺寸,简单来说,我想用MediaQuery.of(context).size.width*constant作为我的SizedBox()

如果还有其他选择,请告知我它的信息。

【问题讨论】:

    标签: android flutter dart user-interface


    【解决方案1】:

    您可以从MediaQuery.of(context).size 获取设备尺寸,并根据该尺寸创建一个大小的盒子。 SizedBox.fromSize({Key? key, Widget? child, Size? size}) 创建一个具有指定大小的框。

    替代方案:您也可以使用flutter_screenutil 包。 https://pub.dev/packages/flutter_screenutil

    【讨论】:

      【解决方案2】:

      不是我的,来自 youtube 视频

                  import 'package:flutter/material.dart';
              
              class SizeConfig {
                static  late MediaQueryData _mediaQueryData;
                static  late double screenWidth;
                static late double screenHeight;
                static late double defaultSize;
                static late Orientation orientation;
              
                void init(BuildContext context) {
                  _mediaQueryData = MediaQuery.of(context);
                  screenWidth = _mediaQueryData.size.width;
                  screenHeight = _mediaQueryData.size.height;
                  orientation = _mediaQueryData.orientation;
                }
              }
              
              // Get the proportionate height as per screen size
              double getProportionateScreenHeight(double inputHeight) {
                double screenHeight = SizeConfig.screenHeight;
                // 812 is the layout height that designer use
                return (inputHeight / 812.0) * screenHeight;
              }
              
              // Get the proportionate height as per screen size
              double getProportionateScreenWidth(double inputWidth) {
                double screenWidth = SizeConfig.screenWidth;
                // 375 is the layout width that designer use
                return (inputWidth / 375.0) * screenWidth;
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-27
        • 2022-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多