Jon*_*ams 14
您可以Stack
使用Positioned
窗口小部件绝对定位窗口小部件的子项.
下面的最小示例将红色框放在列表视图上方,方法是将子项放在Stack的子项中ListView 之后的Positioned小部件中.
List<String> todos = [...];
return new Stack(
children: <Widget>[
new ListView(
children: todos
.map((todo) => new ListTile(title: new Text(todo)))
.toList(),
),
new Positioned(
left: 30.0,
top: 30.0,
child: new Container(
width: 100.0,
height: 80.0,
decoration: new BoxDecoration(color: Colors.red),
child: new Text('hello'),
)
),
],
);
Run Code Online (Sandbox Code Playgroud)
而这里是一个Scaffold
身体.如果您添加更多项目,您会发现列表滚动而不移动红色框.
小智 6
您可以在带有AspectRatio小部件的Stack小部件中使用Positioned小部件,并使用 % 距离,如下面的代码所示。
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size; //get the screen size
List<String> todos = [...];
//the below if to get the aspect ratio of the screen i am using the app only in landscape
//if you need to use it in portrait you should add the sizes below
if((size.width / size.height) > 1.76){
aspect = 16 / 9;
}else if((size.width / size.height) < 1.77 && (size.width / size.height) >= 1.6){
aspect = 16 / 10;
}else{
aspect = 4 /3;
}
return new Scaffold(
body: new Center(
//layoutBuilder i can use the constraints to get the width and height of the screen
child: new LayoutBuilder(builder: (context, constraints) {
return new AspectRatio(
aspectRatio: aspect,
child: new Column(
children: <Widget>[
new ListView(
children: todos
.map((todo) => new ListTile(title: new Text(todo)))
.toList(),
),
new Positioned(
//constraints.biggest.height to get the height
// * .05 to put the position top: 5%
top: constraints.biggest.height * .05,
left: constraints.biggest.width * .30,
child: new Container(
width: 100.0,
height: 80.0,
decoration: new BoxDecoration(color: Colors.red),
child: new Text('hello'),
),
),
],
),
),
}),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
希望它能帮助你......
归档时间: |
|
查看次数: |
12849 次 |
最近记录: |