Files
SpotiFLAC-Mobile/lib/controllers/collapsing_header_scroll_mixin.dart
T
zarzet 04277f40bd perf(pages): rebuild only the toolbar title when it flips
The collapsing title flip used setState, rebuilding the whole album,
playlist, artist or track metadata page mid-scroll. The mixin now keeps the
flag in a ValueNotifier that these headers listen to; screens that still read
the bool keep the page rebuild.
2026-10-23 22:49:09 +07:00

49 lines
1.7 KiB
Dart

import 'package:flutter/material.dart';
/// Expanded height of the album-style collapsing header, shared with the
/// loading screens that show the same header before the tracks arrive.
double collectionHeaderExpandedHeight(BuildContext context) {
final mediaSize = MediaQuery.sizeOf(context);
return (mediaSize.height * 0.6).clamp(400.0, 580.0);
}
/// Shared collapsing-header scroll behavior for album-style screens: owns the
/// [ScrollController] and flips [showTitleNotifier] once the header has
/// scrolled out of view.
mixin CollapsingHeaderScrollMixin<T extends StatefulWidget> on State<T> {
final ScrollController scrollController = ScrollController();
final ValueNotifier<bool> showTitleNotifier = ValueNotifier(false);
bool get showTitleInAppBar => showTitleNotifier.value;
/// Whether a title flip also rebuilds the screen. Screens that hand
/// [showTitleNotifier] to their header return false.
bool get rebuildsOnTitleFlip => true;
@override
void initState() {
super.initState();
scrollController.addListener(_onHeaderScroll);
}
@override
void dispose() {
scrollController.removeListener(_onHeaderScroll);
scrollController.dispose();
showTitleNotifier.dispose();
super.dispose();
}
double calculateExpandedHeight(BuildContext context) =>
collectionHeaderExpandedHeight(context);
void _onHeaderScroll() {
final expandedHeight = calculateExpandedHeight(context);
final shouldShow =
scrollController.offset > (expandedHeight - kToolbarHeight - 20);
if (shouldShow == showTitleInAppBar) return;
showTitleNotifier.value = shouldShow;
if (rebuildsOnTitleFlip) setState(() {});
}
}