【发布时间】:2021-02-07 04:15:29
【问题描述】:
我希望将一组小部件按比例放置在背景小部件(图像小部件)上方。
我尝试了以下方法。
创建了一个 Widget 数组并将其添加到 Stack 使用 LayoutBuilder 构建子小部件并使用 Positioned 小部件定位它们。
但由于Incorrect use of ParentDataWidget. 错误而无法正常工作。
Widget build(BuildContext context) {
final _widgetStack = <Widget>[];
_widgetStack.add(Container(
child: Image.network(
'https://nikhileshwar96.github.io/Showcase/blue.jpg'),
color: Color.fromARGB(255, 255, 255, 255),
));
for (int _i = 1; _i < 5; _i++) {
_widgetStack.add(LayoutBuilder(
builder: (context, constrain) => Positioned(
top: constrain.maxWidth * 0.5,
left: constrain.maxHeight * 0.5,
width: constrain.maxWidth * 0.25,
height: constrain.maxHeight * 0.25,
child: Container(
child: Text('HiTHERE'),
color: Color.fromARGB(255, 255, 0, 0),
),
)));
}
return Container(
child: Scaffold(
appBar: AppBar(
title: Text("Hi there"),
),
body:
Container(
height: 500,
child: Stack(
children: _widgetStack,
),
)
),
);
}
【问题讨论】:
标签: flutter dart widget flutter-layout