颤振位置固定等效

Axe*_*uez 6 dart flutter

无论滚动如何,是否可以在屏幕中修复保持固定的对象?

类似于CSS位置固定的东西.

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身体.如果您添加更多项目,您会发现列表滚动而不移动红色框.

一个绝对定位的例子

  • 我正在使用“堆栈”和“定位”,但是当我在“定位”的子隐藏中添加“左”或“右”或“底部”或“顶部”属性时。 (2认同)

小智 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)

希望它能帮助你......

  • 我正在使用“堆栈”和“定位”,但是当我在“定位”的子隐藏中添加“左”或“右”或“底部”或“顶部”属性时。 (2认同)