【发布时间】:2020-03-04 12:01:59
【问题描述】:
想知道为什么在flutter中,Listview和Text不同,不能直接返回到runApp()。
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ListView(
padding: const EdgeInsets.all(8),
children: <Widget>[
Container(
height: 50,
color: Colors.amber[600],
child: const Center(child: Text('Entry A')),
),
],
),
);
}
}
如果 ListView 包装在 MaterialApp 中,它可以工作。但是,如果它是直接返回或在容器中返回,如下所示:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: ListView(
padding: const EdgeInsets.all(8),
children: <Widget>[
Container(
height: 50,
color: Colors.amber[600],
child: const Center(child: Text('Entry A')),
),
],
),
);
}
}
我有错误。 我已经阅读了很多关于颤振的文章,包括官方文档的 Listview api,但没有人解释原因。虽然有人在 github(https://github.com/flutter/flutter/issues/50947) 中提交了问题,但解决者只告诉他如何解决问题而没有理由。
【问题讨论】: