flutter - ListView 滚动经过顶部小部件

标签 flutter dart

我有以下 ListView 在文本小部件下创建得很好。
但是如果我向上滚动,它会滚动经过顶部的文本小部件。
如果我使用 ListView.Builder,同样的问题。起初以为这仅在IOS平台上存在。
但现在在 Android 中也注意到了同样的问题。我已经浓缩了实现
您可以复制整个代码块并将其粘贴到 https://flutter.dev/docs/get-started/codelab-web
并重新创建问题以查看它。 (虽然它需要一段时间才能呈现)。
我能否就我做错了什么得到一些建议?
在模拟器和实际移动设备中体验相同。谢谢。

import 'package:flutter/material.dart';

void main() {
  runApp(MyAppOne());
}

class MyAppOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: SafeArea(
        child: Scaffold(
          body: Column(
            children: [
              Text('The List scrolls behind this text instead of stopping under it'),
              Expanded(
                child: ListView(
                  // hard coded values to simplify. accountTitles is just used to iterate
                  // based on its size which is length 30. Issue exists in this example.
                  children: accountTitles.map((e) => ListTile(
                    tileColor: Color(0xff1B223E),
                    leading: Icon(
                      Icons.home,
                      color: Colors.white,
                    ),
                    title: Text(
                      'Some title',
                      style: TextStyle(color: Colors.white),
                    ),
                    trailing: Icon(Icons.home,
                        color: Colors.white),
                  ),).toList(),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

List<String> accountTitles = [
    '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',  '', '', '', '', '', '', '', '', '', '',
];

最佳答案

您可以在下面复制粘贴运行完整代码
解决方案:使用 Material包装ListView或使用 Material包装ListTileListTile的源代码描述 https://github.com/flutter/flutter/blob/1fd2f7c400a719931e9fa181a2dd19f1756a0c95/packages/flutter/lib/src/material/list_tile.dart#L731

class ListTile extends StatelessWidget {
  ...
  /// Requires one of its ancestors to be a [Material] widget.
  const ListTile({
因为ListTile使用 InkWell , InkWell小部件必须有 Material小部件作为祖先。 https://api.flutter.dev/flutter/material/InkWell-class.html
enter image description here
代码片段
Expanded(
    child: Material(
      child: ListView(
或者
Material(
      child: ListTile(
        tileColor: Color(0xff1B223E),
工作演示 Material包装ListView enter image description here
带有 Material 的完整代码包装ListView
import 'package:flutter/material.dart';

void main() {
  runApp(MyAppOne());
}

class MyAppOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: SafeArea(
        child: Scaffold(
          body: Column(
            children: [
              Text(
                  'The List scrolls behind this text instead of stopping under it'),
              Expanded(
                child: Material(
                  child: ListView(
                    // hard coded values to simplify. accountTitles is just used to iterate
                    // based on its size which is length 30. Issue exists in this example.
                    children: accountTitles
                        .map(
                          (e) => ListTile(
                            tileColor: Color(0xff1B223E),
                            leading: Icon(
                              Icons.home,
                              color: Colors.white,
                            ),
                            title: Text(
                              'Some title',
                              style: TextStyle(color: Colors.white),
                            ),
                            trailing: Icon(Icons.home, color: Colors.white),
                          ),
                        )
                        .toList(),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

List<String> accountTitles = [
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
];

关于flutter - ListView 滚动经过顶部小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68156054/

相关文章:

list - Dart 中的 DoubleLinkedQueue 和 ListQueue 有什么区别?

flutter - 应用插件: 'com.huawei.agconnect' in Flutter时出现错误

flutter - Flutter中的滑动表单步骤?

android - 如何修复 flutter 小部件闪烁?

flutter - 如何使用 Flutter 在恢复状态下重建 StreamBuilder

flutter - 如何在单元测试期间访问 StatefulWidget 的状态内容?

flutter - 如何使用 dart null-safety 对可能包含空值的列表进行排序

android - 从动态数据表 flutter 中获取选定的行索引

flutter - 类型转换中的“_InternalLinkedHashMap <dynamic,dynamic >' is not a subtype of type ' Map <String,dynamic>”

flutter - 取消键盘焦点在按下的多个按钮上