CupertinoSliverRefreshControl与SuperScaffold兼容问题求可行示例
可运行的CupertinoSliverRefreshControl + SuperScaffold示例
以下是完整的可运行代码,解决下拉刷新控件与SuperScaffold的配合问题:
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:super_scaffold/super_scaffold.dart'; // 模拟技能数据的Provider final skillsProvider = FutureProvider<List<String>>((ref) async { await Future.delayed(const Duration(seconds: 2)); return List.generate(20, (index) => '技能 ${index + 1}'); }); class RefreshablePage extends ConsumerWidget { const RefreshablePage({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final skillsAsyncValue = ref.watch(skillsProvider); return SuperScaffold( appBar: AppBar(title: const Text('下拉刷新示例')), body: CustomScrollView( slivers: [ // 下拉刷新控件,必须置于slivers列表首位 CupertinoSliverRefreshControl( onRefresh: () async { print("refreshing ..."); await ref.refresh(skillsProvider.future); }, ), // 根据异步状态渲染列表内容 skillsAsyncValue.when( loading: () => const SliverFillRemaining( child: Center(child: CupertinoActivityIndicator()), ), error: (err, stack) => SliverFillRemaining( child: Center(child: Text('加载失败: $err')), ), data: (skills) => SliverList( delegate: SliverChildBuilderDelegate( (context, index) => ListTile(title: Text(skills[index])), childCount: skills.length, ), ), ), ], ), ); } } void main() { runApp( const ProviderScope( child: MaterialApp(home: RefreshablePage()), ), ); }
关键注意事项
- 依赖配置:确保pubspec.yaml中添加对应依赖:
dependencies: flutter: sdk: flutter flutter_riverpod: ^2.3.0 super_scaffold: ^1.0.0 # 请根据实际最新版本调整 - 结构修正:原代码用
SliverFillRemaining包裹普通ListView会导致sliver结构冲突,改用SliverList直接作为CustomScrollView的sliver子项,符合组件要求 - 刷新控件位置:
CupertinoSliverRefreshControl必须是slivers列表的第一个元素,才能正确触发下拉刷新逻辑 - 状态管理:使用
AsyncValue.when统一处理加载、错误、成功状态,避免状态异常
内容的提问来源于stack exchange,提问作者Puvi Styne
相关产品推荐
相关产品推荐

