gpt4 book ai didi

flutter - 带有 SliverAppBar 的 NestedScrollView 导致意外的 body 填充

转载 作者:行者123 更新时间:2023-12-05 09:04:22 25 4
gpt4 key购买 nike

当我运行这段代码时,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'),
),
),
),
);
}
}

enter image description here

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)

最佳答案

似乎没有办法避免手动删除填充。所以我的解决方案现在看起来像这样:

return Scaffold(
body: DefaultTabController(
length: 2,
child: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) => [
SliverAppBar(
pinned: true,
title: Text('Title'),
bottom: TabBar(tabs: [Tab(text: 'T 1'), Tab(text: 'T 2')]),
),
],
body: MediaQuery.removePadding(
removeTop: true,
context: context,
child: ListView.builder(
itemCount: 24,
itemBuilder: (context, index) => Container(
height: 40,
padding: EdgeInsets.zero,
alignment: Alignment.center,
color: Colors.yellow,
margin: EdgeInsets.only(top: index != 0 ? 8 : 0),
child: Text('Body $index'),
),
),
),
),
),
);

关于flutter - 带有 SliverAppBar 的 NestedScrollView 导致意外的 body 填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68806090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com