【问题标题】:How can I have widget sizes relative to the screen size in flutter?如何在颤动中获得相对于屏幕尺寸的小部件尺寸?
【发布时间】:2019-06-01 05:04:51
【问题描述】:

我正在构建一个颤振应用程序。在那里我做了一个 SliverAppBar:

child: NestedScrollView (
  headerSliverBuilder: (BuildContext context, i) {
    return <Widget> [
      SliverAppBar (
        backgroundColor: Colors.black,
        expandedHeight: 150.0,
      )
    ];
  },

我将高度设置为 150.0 像素。但我意识到这个尺寸会在不同的设备中发生变化。如何让尺寸在每台设备中占据 40% 的屏幕?

【问题讨论】:

标签: user-interface dart flutter frontend


【解决方案1】:

您可以使用BuildContext contextMediaQuery 找出当前设备的屏幕尺寸。例如:

    var width = MediaQuery.of(context).size.width  * 0.4; // set width to 40% of the screen width
    var height = MediaQuery.of(context).size.height  * 0.4; // set height to 40% of the screen height

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2013-04-17
    • 1970-01-01
    • 2021-04-22
    • 2015-02-09
    • 2011-08-25
    相关资源
    最近更新 更多