当我运行此代码时,ListView 将有一个意外的顶部填充。(见截图)为什么会这样,有没有办法避免这种情况?
我已经尝试过 SliverOverlapAbsorber、SafeArea 和 MediaQuery 来修复填充,但到目前为止似乎没有任何效果。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) => [
SliverAppBar(
pinned: true,
title: Text('Title'),
),
],
body: ListView.builder(
itemCount: 24,
itemBuilder: (context, index) => Container(
height: 40,
alignment: Alignment.center,
color: Colors.yellow,
margin: EdgeInsets.only(top: index != 0 ? 8 : 0),
child: Text('Body $index'),
),
),
),
);
}
}
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.5.1 20G80 darwin-x64, locale de-DE)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
✗ Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.2)
[✓] IntelliJ IDEA Community Edition (version 2021.1.1)
[✓] VS Code (version 1.59.0)
[✓] Connected device (2 available)