-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathstacked_scroll.dart
98 lines (86 loc) · 2.79 KB
/
stacked_scroll.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import 'package:flutter/material.dart';
class StackingCardsList extends StatefulWidget {
const StackingCardsList({Key? key}) : super(key: key);
@override
State<StackingCardsList> createState() => _StackingCardsListState();
}
class _StackingCardsListState extends State<StackingCardsList> {
final ScrollController _scrollController = ScrollController();
final double _cardHeight = 200.0;
final int _totalCards = 10;
@override
void initState() {
super.initState();
_scrollController.addListener(_handleScroll);
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
void _handleScroll() {
setState(() {}); // Trigger rebuild to update card positions
}
double _getCardOffset(int index) {
final scrollOffset = _scrollController.offset;
final rawOffset = (index * _cardHeight) - scrollOffset;
// If card should be stacked at top
if (rawOffset < 0) {
return 0;
}
return rawOffset;
}
bool _shouldRenderCard(int index) {
final currentCard = (_scrollController.offset / _cardHeight).floor();
// Only render current card and next few cards
return index >= currentCard - 1 && index <= currentCard + 3;
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
controller: _scrollController,
child: SizedBox(
height: _cardHeight * _totalCards,
child: Stack(
children: List.generate(_totalCards, (index) {
if (!_shouldRenderCard(index)) {
return const SizedBox.shrink();
}
return AnimatedPositioned(
duration: const Duration(milliseconds: 100),
top: _getCardOffset(index),
left: 0,
right: 0,
height: _cardHeight,
child: Card(
elevation: 4,
margin: const EdgeInsets.all(8),
color: Colors.blue[
(index % 9 + 1) * 100], // Different colors for visibility
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Card ${index + 1}',
style: const TextStyle(
fontSize: 24,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
Text(
'Offset: ${_getCardOffset(index).toStringAsFixed(1)}',
style: const TextStyle(color: Colors.white),
),
],
),
),
),
);
}),
),
),
);
}
}