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
相关产品推荐
相关产品推荐

