繁体   English   中英

Flutter Web:创建具有移动应用大小的 UI

[英]Flutter Web : Create UI with mobile app size

我有一个 Flutter 应用程序,它在 android、iOS 和 Web 中运行良好。但在 web 中,它的全屏宽度非常难看。 如何为 web 创建与移动版本一样的 UI?

参考这篇文章,根据不同的屏幕尺寸有效缩放 UI。

要确定用户是否正在使用浏览器,请使用kIsWeb

前任:

Image.network('Exampleurl.com',height:kIsWeb?webHeight:mobileHeight);

但是我个人使用这个 class 来动态调整大小,而不是上面提到的两者都是相似的,但是下面给出的一个有两个函数来获取宽高比的宽度和高度。

屏幕尺寸.dart

class SizeConfig {
  static MediaQueryData _mediaQueryData;
  static double screenWidth;
  static double screenHeight;
  static double defaultSize;
  static 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 width 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;
}

}

例子:

Image.network('Exampleurl.com',height:getProportionateScreenHeight(150)); 

或者

Image.network('Exampleurl.com',height:(kIsWeb)?getProportionateScreenHeight(250):getProportionateScreenHeight(100));

感谢@nishuthan-s,我用 KisWeb 和 Container 解决了我的问题

Center(
  child: Container(
    width: kIsWeb ? 400.0 : double.infinity,
    child: ListView.builder(...)
  ),
);

我开发了一个 package 来为你处理这个问题: https://pub.dev/packages/center_the_widgets

你可以像这样使用它:

 return CenterTheWidgets(
      child: Scaffold(
        body: const Text('Hi!'),
      ),
    );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM