You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中CustomScrollView下SliverList的A-Z侧边菜单实现问题

需求:在CustomScrollView中实现Sliver版AZ侧边栏索引功能

尝试使用alphabet_scroll_view和azlistview库,但二者均无法与CustomScrollView及SliverList兼容。

页面结构

初始页面结构:

CustomScrollView
  Appbar - pinned
  title
  SliverList (10 items)
  title
  SliverList (10 items)
  tabs (contacts, favorites) - pinned
  contactsWidget
    SliverList (+ A-Z 索引视图)

实际页面结构:

  • CustomScrollView
    • AppBar
    • 搜索组件
    • 自定义Widget
    • 自定义Widget
    • Tabs(联系人、收藏)
    • 自定义Widget
    • SliverList(缺失AZ索引菜单)

核心诉求

需要实现与azlistview完全一致的功能,但必须是Sliver版本,适配现有CustomScrollView结构。

已尝试方案及问题

  • 使用SliverStack等方案:出现侧边索引菜单定位错误、滚动跳转异常问题
  • 调用Scrollable.ensureVisible:会直接跳转至页面底部,不符合预期
  • 尝试alphabet_list_view库:因该库基于ListView实现,出现LateInitializationError: Field 'geometry' has not been initialized错误
  • 嵌套CustomScrollView:不可行,且无法使用shrinkWrap属性

相关代码示例

class TestModel {
  String tagIndex;
  String name;
  TestModel({required this.tagIndex, required this.name});
}

class IndexPartial extends ConsumerWidget {
  final bool definitionsOnly;
  const IndexPartial({super.key, this.favorites = false});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final state = ref.watch(indexProvider(favorites));

    final List<TestModel> contacts = [
      TestModel(name: 'eeee', tagIndex: 'E'),
      TestModel(name: 'eeeeee', tagIndex: 'E'),
      TestModel(name: 'pp', tagIndex: 'P'),
      TestModel(name: 'ppp', tagIndex: 'P'),
      TestModel(name: 'pppp', tagIndex: 'P'),
      TestModel(name: 'ppppp', tagIndex: 'P'),
      TestModel(name: 'w', tagIndex: 'W'),
    ];

    return state.when(
      data: (data) {
        return MultiSliver(
          children: [
            // AZ列表索引待实现
            SliverPadding(
              padding: const EdgeInsets.only(right: 45),
              sliver: SliverList(
                delegate: SliverChildBuilderDelegate(
                  (BuildContext context, int index) {
                    return Container(
                      height: 200,
                      color: Colors.yellow,
                      child: Text(contacts[index].name),
                    );
                  },
                  childCount: contacts.length,
                ),
              ),
            ),
          ],
        );
      },
      error: (error, stackTrace) {
        // 错误处理逻辑
      },
      loading: () {
        // 加载状态UI
      },
    );
  }
}

内容的提问来源于stack exchange,提问作者Ne Xen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:50:29