相对于屏幕大小,如何显示窗口小部件尺寸?

时间:2019-01-06 05:56:35

标签: user-interface dart flutter frontend

我正在构建一个Flutter应用程序。在这里,我做了一个SliverAppBar:

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

我将高度设置为150.0像素。但是我意识到这个大小将在不同的设备中改变。如何使尺寸在每台设备中占据40%的屏幕?

1 个答案:

答案 0 :(得分:2)

您可以将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