-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcard_splitting.dart
211 lines (194 loc) · 6.95 KB
/
card_splitting.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import 'package:custom_widgets/data/card_splitting_data.dart';
import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
class CardSplitting extends StatefulWidget {
const CardSplitting({super.key});
@override
State<CardSplitting> createState() => _CardSplittingState();
}
class _CardSplittingState extends State<CardSplitting>
with TickerProviderStateMixin {
int itemSelected = -1;
int lastSlected = -1;
late AnimationController controller = AnimationController(vsync: this);
SpringSimulation springSimulation = SpringSimulation(
const SpringDescription(mass: 1, stiffness: 150, damping: 5.0),
0.0,
1.0,
0.0);
//space between item title and detail
Animation<double> spaceTween = const AlwaysStoppedAnimation(0);
//angle of the arrow icon
Animation<double> angleTween = const AlwaysStoppedAnimation(0);
void onItemClicked(bool isExpanded, int index) {
if (isExpanded) {
//collapse the selected item
setState(() {
spaceTween = Tween(begin: 10.0, end: 0.0).animate(controller);
angleTween = Tween(begin: 3.14, end: 0.0).animate(controller);
controller.animateWith(springSimulation);
lastSlected = itemSelected;
itemSelected = -1;
});
} else {
//expand the selected item
setState(() {
lastSlected = itemSelected;
itemSelected = index;
controller.animateWith(springSimulation);
});
}
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(
title: const Text("Card Splitting"),
),
body: AnimatedBuilder(
animation: controller,
builder: (context, child) {
return Center(
child: SizedBox(
width: width * 0.9,
child: Column(
mainAxisSize: MainAxisSize.min,
children: List.generate(items.length, (index) {
//expand the selected item
bool isExpanded = index == itemSelected;
//isFirst is used to set the top border color
bool isFirst = index == 0 || //first item
index ==
itemSelected + 1 || //item below the selected item
index == itemSelected; //selected item
//isLast is used to set the bottom border color
bool isLast = index == items.length - 1 ||
index == itemSelected - 1 ||
index == itemSelected;
//update space and angle tween for the selected item
if (itemSelected == index) {
spaceTween =
Tween(begin: 0.0, end: 10.0).animate(controller);
angleTween =
Tween(begin: 0.0, end: 3.14).animate(controller);
} else if (index == lastSlected) {
//update space and angle tween for the last selected item
spaceTween =
Tween(begin: 10.0, end: 0.0).animate(controller);
angleTween =
Tween(begin: 3.14, end: 0.0).animate(controller);
} else {
//set space and angle tween to default
spaceTween = const AlwaysStoppedAnimation(0);
angleTween = const AlwaysStoppedAnimation(0);
}
return GestureDetector(
onTap: () {
onItemClicked(isExpanded, index);
},
child: SplittingItem(
title: items[index].title,
detail: items[index].detail,
icon: items[index].icon,
isExpanded: isExpanded,
isFirst: isFirst,
isLast: isLast,
space: spaceTween.value,
angle: angleTween.value,
),
);
}),
),
),
);
}),
);
}
}
class SplittingItem extends StatelessWidget {
final String title, detail;
final IconData icon;
final bool isExpanded, isFirst, isLast;
final double space, angle;
const SplittingItem(
{super.key,
required this.title,
required this.isExpanded,
required this.detail,
required this.icon,
required this.isFirst,
required this.isLast,
required this.angle,
required this.space});
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(15),
margin: isExpanded ? const EdgeInsets.symmetric(vertical: 10) : null,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(isFirst ? 20 : 0),
topRight: Radius.circular(isFirst ? 20 : 0),
bottomLeft: Radius.circular(isLast ? 20 : 0),
bottomRight: Radius.circular(isLast ? 20 : 0)),
border: Border(
left: const BorderSide(color: Colors.grey),
right: const BorderSide(color: Colors.grey),
top: isFirst
? const BorderSide(color: Colors.grey)
: BorderSide.none,
bottom: isLast
? const BorderSide(color: Colors.grey)
: BorderSide.none),
color: const Color.fromARGB(255, 252, 252, 252)),
child: Column(
children: [
Row(
children: [
Icon(
icon,
size: 30,
color: const Color.fromARGB(255, 133, 132, 137),
),
const SizedBox(width: 5),
Text(
title,
style:
const TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
Expanded(
child: Align(
alignment: Alignment.centerRight,
child: Transform.rotate(
angle: angle,
child: const Icon(
Icons.keyboard_arrow_down,
size: 30,
color: Color.fromARGB(255, 133, 132, 137),
),
),
),
)
],
),
SizedBox(height: space),
isExpanded
? Text(
detail,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
color: Color.fromARGB(255, 94, 94, 96)),
)
: const SizedBox(),
],
),
);
}
}