我怎样才能有相对于颤振屏幕尺寸的小部件大小



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

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

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

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

最新更新