我怎样才能让小部件尺寸相对于颤振中的屏幕尺寸?

Ext*_*tra 5 user-interface frontend dart flutter

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

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

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

Osw*_*ann 3

您可以使用BuildContext context和 aMediaQuery来查找当前设备的屏幕尺寸。例如:

    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
Run Code Online (Sandbox Code Playgroud)