dart 如何修正这一误差,如何把握Flutter布局的基本原则?

wgeznvg7  于 2023-06-19  发布在  Flutter
关注(0)|答案(1)|浏览(83)
return Scaffold(
  appBar: AppBar(
    // Here we take the value from the MyHomePage object that was created by
    // the App.build method, and use it to set our appbar title.
    title: Text(widget.title),
  ),
  body: const Positioned(
    left: 100,
    height: 50,
    child: SizedBox(
      width: 100,
      height: 100,
      child: Text("111"),
    ),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: const Icon(Icons.add),
  ), // This trailing comma makes auto-formatting nicer for build methods.
);

我在flutter中遇到了一个“Incorrect use of ParentDataWidget.”错误。是什么原因导致了这种错误,如何把握Flutter中布局的基本原理?
Flutter布局是否有任何规则以及如何找到它们?

zzlelutf

zzlelutf1#

错误消息应该告诉您更多信息并告诉您原因。它应该这样写:

The following ParentDataWidgets are providing parent data to the same RenderObject:
- Positioned(left: 100.0, height: 50.0) (typically placed directly inside a Stack widget)

换句话说:Positioned只能在Stack小工具中使用。因此,要修复错误,您可以将其 Package 在Stack中,例如

return Scaffold(
    appBar: AppBar(
      // Here we take the value from the MyHomePage object that was created by
      // the App.build method, and use it to set our appbar title.
      title: Text('5'),
    ),
    body: const Stack(
      children: [
        Positioned(
          left: 100,
          height: 50,
          child: SizedBox(
            width: 100,
            height: 100,
            child: Text("111"),
          ),
        ),
      ],
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: (){},
      tooltip: 'Increment',
      child: const Icon(Icons.add),
    ), // This trailing comma makes auto-formatting nicer for build methods.
);

相关问题