perf: eliminate 60fps setState rebuilds causing scroll lag
Three root causes of the perceived scroll/animation lag: 1. profile_screen.dart — AnimationController listener called setState() on every animation frame (60fps × 2s = 120 full-tree rebuilds). The entire ProfileScreen with its nested lists and images was rebuilding 60 times per second just to update two small widgets (EXP bar + stat counters). Fix: remove setState() from listeners entirely; wrap only the EXP bar (LayoutBuilder) and stat row (IntrinsicHeight) in AnimatedBuilder so only those two leaf widgets re-render per frame. 2. learn_more_screen.dart — PageView.onPageChanged called setState() on every swipe, rebuilding the full event detail screen (blurred bg image, map, about section, etc.) just to update the 6px dot indicators. Fix: int _currentPage → ValueNotifier<int> _pageNotifier; wrap only the dot row and the blurred background image in ValueListenableBuilder. 3. search_screen.dart — BackdropFilter(ImageFilter.blur) without a RepaintBoundary forces Flutter to read every pixel behind the blur widget and composite it every frame. When the user scrolls the underlying list, the blur repaints continuously causing frame drops. Fix: wrap BackdropFilter in RepaintBoundary to isolate its repaint layer.
This commit is contained in:
@@ -28,7 +28,7 @@ class _LearnMoreScreenState extends State<LearnMoreScreen> {
|
||||
|
||||
// Carousel
|
||||
final PageController _pageController = PageController();
|
||||
int _currentPage = 0;
|
||||
late final ValueNotifier<int> _pageNotifier;
|
||||
Timer? _autoScrollTimer;
|
||||
|
||||
// About section
|
||||
@@ -45,6 +45,7 @@ class _LearnMoreScreenState extends State<LearnMoreScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_pageNotifier = ValueNotifier(0);
|
||||
_loadEvent();
|
||||
}
|
||||
|
||||
@@ -52,6 +53,7 @@ class _LearnMoreScreenState extends State<LearnMoreScreen> {
|
||||
void dispose() {
|
||||
_autoScrollTimer?.cancel();
|
||||
_pageController.dispose();
|
||||
_pageNotifier.dispose();
|
||||
_mapController?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
@@ -99,7 +101,7 @@ class _LearnMoreScreenState extends State<LearnMoreScreen> {
|
||||
if (count <= 1) return;
|
||||
_autoScrollTimer = Timer.periodic(const Duration(seconds: 3), (_) {
|
||||
if (!_pageController.hasClients) return;
|
||||
final next = (_currentPage + 1) % count;
|
||||
final next = (_pageNotifier.value + 1) % count;
|
||||
_pageController.animateToPage(next,
|
||||
duration: const Duration(milliseconds: 500), curve: Curves.easeInOut);
|
||||
});
|
||||
@@ -312,10 +314,12 @@ class _LearnMoreScreenState extends State<LearnMoreScreen> {
|
||||
// Pill-shaped page indicators (centered)
|
||||
Expanded(
|
||||
child: _imageUrls.length > 1
|
||||
? Row(
|
||||
? ValueListenableBuilder<int>(
|
||||
valueListenable: _pageNotifier,
|
||||
builder: (context, currentPage, _) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: List.generate(_imageUrls.length, (i) {
|
||||
final active = i == _currentPage;
|
||||
final active = i == currentPage;
|
||||
return AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 300),
|
||||
margin: const EdgeInsets.symmetric(horizontal: 3),
|
||||
@@ -329,6 +333,7 @@ class _LearnMoreScreenState extends State<LearnMoreScreen> {
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
)
|
||||
: const SizedBox.shrink(),
|
||||
),
|
||||
@@ -433,8 +438,10 @@ class _LearnMoreScreenState extends State<LearnMoreScreen> {
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
CachedNetworkImage(
|
||||
imageUrl: images[_currentPage],
|
||||
ValueListenableBuilder<int>(
|
||||
valueListenable: _pageNotifier,
|
||||
builder: (context, currentPage, _) => CachedNetworkImage(
|
||||
imageUrl: images[currentPage],
|
||||
fit: BoxFit.cover,
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
@@ -457,6 +464,7 @@ class _LearnMoreScreenState extends State<LearnMoreScreen> {
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
BackdropFilter(
|
||||
filter: ImageFilter.blur(sigmaX: 25, sigmaY: 25),
|
||||
child: Container(
|
||||
@@ -488,7 +496,7 @@ class _LearnMoreScreenState extends State<LearnMoreScreen> {
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
child: PageView.builder(
|
||||
controller: _pageController,
|
||||
onPageChanged: (i) => setState(() => _currentPage = i),
|
||||
onPageChanged: (i) => _pageNotifier.value = i,
|
||||
itemCount: images.length,
|
||||
itemBuilder: (_, i) => CachedNetworkImage(
|
||||
imageUrl: images[i],
|
||||
|
||||
@@ -90,24 +90,17 @@ class _ProfileScreenState extends State<ProfileScreen>
|
||||
parent: _animController,
|
||||
curve: const Interval(0.0, 0.65, curve: Curves.easeOut),
|
||||
);
|
||||
// Update fields without setState — AnimatedBuilder handles the rebuilds
|
||||
expAnim.addListener(() {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_expProgress = expTween.evaluate(expAnim);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Animate stat counters: 0 → target over full 2s
|
||||
_animController.addListener(() {
|
||||
if (!mounted) return;
|
||||
final t = _animController.value;
|
||||
setState(() {
|
||||
_animatedLikes = (t * _targetLikes).round();
|
||||
_animatedPosts = (t * _targetPosts).round();
|
||||
_animatedViews = (t * _targetViews).round();
|
||||
});
|
||||
});
|
||||
|
||||
_animController.forward();
|
||||
});
|
||||
@@ -751,7 +744,9 @@ class _ProfileScreenState extends State<ProfileScreen>
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: LayoutBuilder(
|
||||
child: AnimatedBuilder(
|
||||
animation: _animController,
|
||||
builder: (context, _) => LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final fullWidth = constraints.maxWidth;
|
||||
final filledWidth = fullWidth * _expProgress;
|
||||
@@ -759,7 +754,7 @@ class _ProfileScreenState extends State<ProfileScreen>
|
||||
height: 8,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
color: Colors.grey.shade200, // gray track
|
||||
color: Colors.grey.shade200,
|
||||
),
|
||||
child: Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
@@ -776,6 +771,7 @@ class _ProfileScreenState extends State<ProfileScreen>
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -820,7 +816,9 @@ class _ProfileScreenState extends State<ProfileScreen>
|
||||
bottom: BorderSide(color: Colors.grey.shade200, width: 1),
|
||||
),
|
||||
),
|
||||
child: IntrinsicHeight(
|
||||
child: AnimatedBuilder(
|
||||
animation: _animController,
|
||||
builder: (context, _) => IntrinsicHeight(
|
||||
child: Row(
|
||||
children: [
|
||||
statColumn(_formatNumber(_animatedLikes), 'Likes'),
|
||||
@@ -831,6 +829,7 @@ class _ProfileScreenState extends State<ProfileScreen>
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -169,10 +169,12 @@ class _SearchScreenState extends State<SearchScreen> {
|
||||
behavior: HitTestBehavior.opaque,
|
||||
child: Stack(
|
||||
children: [
|
||||
BackdropFilter(
|
||||
RepaintBoundary(
|
||||
child: BackdropFilter(
|
||||
filter: ImageFilter.blur(sigmaX: 8.0, sigmaY: 8.0),
|
||||
child: Container(color: Colors.black.withOpacity(0.16)),
|
||||
),
|
||||
),
|
||||
Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: GestureDetector(
|
||||
|
||||
Reference in New Issue
Block a user