【问题标题】:Flutter | How to add a SizedBox() as the last item of a ListView.builder()?颤振 |如何将 SizedBox() 添加为 ListView.builder() 的最后一项?
【发布时间】:2021-09-08 13:19:19
【问题描述】:

我正在尝试将一些空间或 SizedBox() 作为 ListView 的最后一项, 因为 FloatingActionButton() 隐藏了最后一个 ListTile 的一些信息。

我想像 WhatsApp 一样滚动到空格/SizeBox()。

body: ListView.builder(
    itemCount: cardsList.length,
    itemBuilder: (context, i) {
      return Column(
        children: [
          ListTile( ... ),
          Divider( ... ),
        ],
      );
    },
  ),

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    ListView 具有填充属性

    body: ListView.builder(
        padding: const EdgeInsets.only(bottom: 100),
        itemCount: cardsList.length,
        itemBuilder: (context, i) {
          return Column(
            children: [
              ListTile( ... ),
              Divider( ... ),
            ],
          );
        },
      ),
    

    【讨论】:

    • 嗯,这很快。请再问一个问题,为什么在 EdgeInsets 之前使用 const ?
    • 我正在使用 flutter_lints: ^1.0.4 # https://pub.dev/packages/flutter_lints 包并覆盖规则 prefer_const_constructors: true 所以现在分析器要求我添加 cont 关键字(如果可能)
    • 感谢您的帮助!!
    • 您可以使用 const 关键字,因为 EdgeInsets 有一个 const 构造函数。它允许框架在每次需要重建小部件树时不重建它,而是从缓存中重新加载它。性能提升
    【解决方案2】:

    您可以使用Column 小部件包装您的ListView。在该小部件中添加SizedBox。像这样:

    body: Column(
      children: [
        ListView.builder(
          itemCount: cardsList.length,
          itemBuilder: (context, i) {
            return Column(
              children: [
                ListTile( ... ),
                Divider( ... ),
              ],
            );
          },
        ),
        SizedBox(height: 50.0)
      ]
    )
    

    【讨论】:

    • 不幸的是,这带来了很多问题。
    【解决方案3】:

    我想你想要这个

     ListView.builder(
            itemCount: cardsList.length + 1,
            itemBuilder: (context, index) =>
                index < items.length ? myListTile() : SizedBox(),
          ),
    

    【讨论】:

    • myListTile() 在你的情况下是 Column( children: [ ListTile( ... ), Divider( ... ), ], );
    • 我认为我的问题变得具有误导性,但这个答案纠正了这种情况。谢谢!
    • 很高兴为您提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2022-01-14
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    相关资源
    最近更新 更多