【问题标题】:Flutter Listview inside of a Card showing extra space at the bottom卡片内的颤振列表视图在底部显示额外的空间
【发布时间】:2022-07-19 00:01:45
【问题描述】:

为什么Card 在底部显示额外的空间?

如果我设置 itemCount: 0 我会得到这个:

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    List<String> words = ['hello', 'world', 'flutter'];

    return Scaffold(
      backgroundColor: Colors.green,
      appBar: AppBar(
        title: const Text('Test'),
        centerTitle: true,
      ),
      body: Card(
        child: ListView.separated(
          separatorBuilder: (context, index) {
            return const Divider(
              color: Colors.black,
            );
          },
          itemCount: words.length,
          shrinkWrap: true,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(words[index]),
            );
          },
        ),
      ),
    );
  }
}

【问题讨论】:

  • 您可以尝试flutter clean 并重建应用程序。我未能从当前 chrome 上的 sn-p 产生问题

标签: flutter


【解决方案1】:

根据 ListView 文档,默认情况下它有填充。您可以使用

禁用此行为
ListView(
   padding: const EdgeInsets.all(8),
   ...

文档在这里:

/// 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.
///
/// {@tool snippet}
/// This example uses the default constructor for [ListView] which takes an
/// explicit [List<Widget>] of children. This [ListView]'s children are made up
/// of [Container]s with [Text].
///
/// ![A ListView of 3 amber colored containers with sample text.](https://flutter.github.io/assets-for-api-docs/assets/widgets/list_view.png)
///
/// ```dart
/// ListView(
///   padding: const EdgeInsets.all(8),
///   children: <Widget>[
///     Container(
///       height: 50,
///       color: Colors.amber[600],
///       child: const Center(child: Text('Entry A')),
///     ),
///     Container(
///       height: 50,
///       color: Colors.amber[500],
///       child: const Center(child: Text('Entry B')),
///     ),
///     Container(
///       height: 50,
///       color: Colors.amber[100],
///       child: const Center(child: Text('Entry C')),
///     ),
///   ],
/// )
/// ```

【讨论】: