fix(post): instant scroll restoration when returning to cached post

Move provider initialization from postFrameCallback to didChangeDependencies
for synchronous access before first build. Create ScrollController with
initialScrollOffset set to cached position, eliminating the visible flash
from loading → content at top → jump to cached position.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Changed files
+51 -44
lib
+51 -44
lib/screens/home/post_detail_screen.dart
···
/// - Loading, empty, and error states
/// - Automatic comment loading on screen init
class PostDetailScreen extends StatefulWidget {
-
const PostDetailScreen({required this.post, this.isOptimistic = false, super.key});
/// Post to display (passed via route extras)
final FeedViewPost post;
···
}
class _PostDetailScreenState extends State<PostDetailScreen> {
-
final ScrollController _scrollController = ScrollController();
final GlobalKey _commentsHeaderKey = GlobalKey();
// Cached provider from CommentsProviderCache
···
@override
void initState() {
super.initState();
-
_scrollController.addListener(_onScroll);
-
// Initialize provider after frame is built
-
WidgetsBinding.instance.addPostFrameCallback((_) {
-
if (mounted) {
-
_initializeProvider();
-
_setupAuthListener();
-
}
-
});
}
/// Listen for auth state changes to handle sign-out
···
// If user signed out while viewing this screen, navigate back
// The CommentsProviderCache has already disposed our provider
-
if (!authProvider.isAuthenticated && _isInitialized && !_providerInvalidated) {
_providerInvalidated = true;
if (kDebugMode) {
···
}
}
-
/// Initialize provider from cache and restore state
-
void _initializeProvider() {
// Get or create provider from cache
final cache = context.read<CommentsProviderCache>();
_commentsCache = cache;
···
postCid: widget.post.post.cid,
);
// Listen for changes to trigger rebuilds
_commentsProvider.addListener(_onProviderChanged);
// Skip loading for optimistic posts (just created, not yet indexed)
if (widget.isOptimistic) {
if (kDebugMode) {
···
}
// Don't load comments - there won't be any yet
} else if (_commentsProvider.comments.isNotEmpty) {
-
// Already have data - restore scroll position immediately
if (kDebugMode) {
debugPrint(
'📦 Using cached comments (${_commentsProvider.comments.length})',
);
}
-
_restoreScrollPosition();
-
// Background refresh if data is stale
if (_commentsProvider.isStale) {
if (kDebugMode) {
debugPrint('🔄 Data stale, refreshing in background');
···
// No cached data - load fresh
_commentsProvider.loadComments(refresh: true);
}
-
-
setState(() {
-
_isInitialized = true;
-
});
}
@override
···
if (mounted) {
setState(() {});
}
-
}
-
-
/// Restore scroll position from provider
-
void _restoreScrollPosition() {
-
final savedPosition = _commentsProvider.scrollPosition;
-
if (savedPosition <= 0) {
-
return;
-
}
-
-
WidgetsBinding.instance.addPostFrameCallback((_) {
-
if (!mounted || !_scrollController.hasClients) {
-
return;
-
}
-
-
final maxExtent = _scrollController.position.maxScrollExtent;
-
final targetPosition = savedPosition.clamp(0.0, maxExtent);
-
-
if (targetPosition > 0) {
-
_scrollController.jumpTo(targetPosition);
-
if (kDebugMode) {
-
debugPrint('📍 Restored scroll to $targetPosition (max: $maxExtent)');
-
}
-
}
-
});
}
/// Handle sort changes from dropdown
···
/// - Loading, empty, and error states
/// - Automatic comment loading on screen init
class PostDetailScreen extends StatefulWidget {
+
const PostDetailScreen({
+
required this.post,
+
this.isOptimistic = false,
+
super.key,
+
});
/// Post to display (passed via route extras)
final FeedViewPost post;
···
}
class _PostDetailScreenState extends State<PostDetailScreen> {
+
// ScrollController created lazily with cached scroll position for instant restoration
+
late ScrollController _scrollController;
final GlobalKey _commentsHeaderKey = GlobalKey();
// Cached provider from CommentsProviderCache
···
@override
void initState() {
super.initState();
+
// ScrollController and provider initialization moved to didChangeDependencies
+
// where we have access to context for synchronous provider acquisition
+
}
+
@override
+
void didChangeDependencies() {
+
super.didChangeDependencies();
+
// Initialize provider synchronously on first call (has context access)
+
// This ensures cached data is available for the first build, avoiding
+
// the flash from loading state → content → scroll position jump
+
if (!_isInitialized) {
+
_initializeProviderSync();
+
}
}
/// Listen for auth state changes to handle sign-out
···
// If user signed out while viewing this screen, navigate back
// The CommentsProviderCache has already disposed our provider
+
if (!authProvider.isAuthenticated &&
+
_isInitialized &&
+
!_providerInvalidated) {
_providerInvalidated = true;
if (kDebugMode) {
···
}
}
+
/// Initialize provider synchronously from cache
+
///
+
/// Called from didChangeDependencies to ensure cached data is available
+
/// for the first build. Creates ScrollController with initialScrollOffset
+
/// set to cached position for instant scroll restoration without flicker.
+
void _initializeProviderSync() {
// Get or create provider from cache
final cache = context.read<CommentsProviderCache>();
_commentsCache = cache;
···
postCid: widget.post.post.cid,
);
+
// Create scroll controller with cached position for instant restoration
+
// This avoids the flash: loading → content at top → jump to cached position
+
final cachedScrollPosition = _commentsProvider.scrollPosition;
+
_scrollController = ScrollController(
+
initialScrollOffset: cachedScrollPosition,
+
);
+
_scrollController.addListener(_onScroll);
+
+
if (kDebugMode && cachedScrollPosition > 0) {
+
debugPrint(
+
'📍 Created ScrollController with initial offset: $cachedScrollPosition',
+
);
+
}
+
// Listen for changes to trigger rebuilds
_commentsProvider.addListener(_onProviderChanged);
+
// Setup auth listener
+
_setupAuthListener();
+
+
// Mark as initialized before triggering any loads
+
// This ensures the first build shows content (not loading) when cached
+
_isInitialized = true;
+
// Skip loading for optimistic posts (just created, not yet indexed)
if (widget.isOptimistic) {
if (kDebugMode) {
···
}
// Don't load comments - there won't be any yet
} else if (_commentsProvider.comments.isNotEmpty) {
+
// Already have cached data - it will render immediately
if (kDebugMode) {
debugPrint(
'📦 Using cached comments (${_commentsProvider.comments.length})',
);
}
+
// Background refresh if data is stale (won't cause flicker)
if (_commentsProvider.isStale) {
if (kDebugMode) {
debugPrint('🔄 Data stale, refreshing in background');
···
// No cached data - load fresh
_commentsProvider.loadComments(refresh: true);
}
}
@override
···
if (mounted) {
setState(() {});
}
}
/// Handle sort changes from dropdown