为什么 space 出现在列表视图的顶部?

Why does space appear at the top of listview?

最近我在制作应用程序并在列表视图上发现问题,

我想要位于底部的定位容器,其中有列表视图。

并且 space 将出现在列表视图的顶部

我不想 space 在列表视图的顶部

如何删除 space??

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:run_flutter/controller.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MainPage(),
    );
  }
}

class MainPage extends StatelessWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    TestController testController = TestController();
    return Scaffold(
      body: Container(
        width: Get.width,
        height: Get.height,
        child: Stack(
          children: [
            Positioned(
              bottom: 0,
              child: AnimatedContainer(
                height: 50,
                width: Get.width,
                color: Colors.amber,
                duration: Duration(milliseconds: 100),
                child: ListView(
                  children: [
                    Text("data"),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

原因写在官方ListView class规范中。

https://api.flutter.dev/flutter/widgets/ListView-class.html#external-links:%7E:text=By%20default%2C%20ListView%20will%20automatically%20pad,link

默认情况下,ListView 将自动填充列表的可滚动末端,以避免 MediaQuery 填充指示的部分障碍。为避免此行为,请使用零填充覆盖 属性.

试着喜欢这个。

ListView(
  padding: EdgeInsets.zero,
  ...
);

根据文档,

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.

您必须使用 removePadding 构造函数(将 removeTop 设置为 true)添加 MediaQuery 父级以删除有意的额外上边距。

这是代码片段:

 MediaQuery.removePadding(
    context: context,
    removeTop: true,
    child: ListView.builder(
     .......
   )
 );

更多信息,请关注this issue

只需删除您的 底部:0 或添加 顶部:0

class MainPage extends StatelessWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    TestController testController = TestController();
    return Scaffold(
      body: Container(
        width: Get.width,
        height: Get.height,
        child: Stack(
          children: [
            Positioned(
              child: AnimatedContainer(
                height: 50,
                width: Get.width,
                color: Colors.amber,
                duration: Duration(milliseconds: 100),
                child: ListView(
                  children: [
                    Text("data"),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}