Flutter:ListView 开始时有间隙
Flutter: ListView has gap at beginning
几个月前我开始使用 Flutter 并尝试实现我的第一个应用程序。该应用程序应显示一个简单的可滚动配置文件视图。
因此,我在 Scaffold 中使用了 ListView 来使屏幕可滚动。作为 ListView 的子项,我显示我想显示的内容。
到目前为止一切正常。我面临的唯一问题是,listView 在脚手架的开头(屏幕截图中的红色)和 ListView 的开头之间有一个奇怪的差距。我觉得这看起来不太好,想知道我是否可以弥补这个差距?
我已经测试过这个问题是否与以前的填充有关,但是如果我向脚手架添加一个普通的文本小部件,它会显示在脚手架的开头。
我的代码如下所示:
Scaffold(
backgroundColor: Colors.red,
body: Padding(
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
child: ListView(
children: [
Text('Status'),
SizedBox(
height: 10.0,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: Container(
child: Text(
'Das hier ist ein Beispieltext disaidhasjdiojsaiodjisajdioajs',
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.headline3
.copyWith(fontStyle: FontStyle.italic),
),
),
),
... //More Widgets
],
),
),
)
默认情况下,ListView
应用了一些填充,在官方文档中提到 (https://api.flutter.dev/flutter/widgets/ListView-class.html):
By default, ListView will automatically pad the list's scrollable
extremities to avoid partial obstructions indicated by MediaQuery's
padding. To avoid this behavior, override with a zero padding
property.
如那里所述,只需覆盖 ListView
:
中的填充值
ListView(
padding: const EdgeInsets.all(0), // or const EdgeInsets.symmetric(vertical: 0) for vertical padding only
children: [...],
)
如果不是这种情况,我建议您检查是否真的需要 Scaffold
小部件来包装您的 ListView
。
几个月前我开始使用 Flutter 并尝试实现我的第一个应用程序。该应用程序应显示一个简单的可滚动配置文件视图。 因此,我在 Scaffold 中使用了 ListView 来使屏幕可滚动。作为 ListView 的子项,我显示我想显示的内容。
到目前为止一切正常。我面临的唯一问题是,listView 在脚手架的开头(屏幕截图中的红色)和 ListView 的开头之间有一个奇怪的差距。我觉得这看起来不太好,想知道我是否可以弥补这个差距?
我已经测试过这个问题是否与以前的填充有关,但是如果我向脚手架添加一个普通的文本小部件,它会显示在脚手架的开头。
我的代码如下所示:
Scaffold(
backgroundColor: Colors.red,
body: Padding(
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
child: ListView(
children: [
Text('Status'),
SizedBox(
height: 10.0,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: Container(
child: Text(
'Das hier ist ein Beispieltext disaidhasjdiojsaiodjisajdioajs',
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.headline3
.copyWith(fontStyle: FontStyle.italic),
),
),
),
... //More Widgets
],
),
),
)
默认情况下,ListView
应用了一些填充,在官方文档中提到 (https://api.flutter.dev/flutter/widgets/ListView-class.html):
By default, ListView will automatically pad the list's scrollable extremities to avoid partial obstructions indicated by MediaQuery's padding. To avoid this behavior, override with a zero padding property.
如那里所述,只需覆盖 ListView
:
ListView(
padding: const EdgeInsets.all(0), // or const EdgeInsets.symmetric(vertical: 0) for vertical padding only
children: [...],
)
如果不是这种情况,我建议您检查是否真的需要 Scaffold
小部件来包装您的 ListView
。