forked from VB10/flutter-ready-to-use-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_card_widget.dart
244 lines (222 loc) · 6.69 KB
/
item_card_widget.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// ignore_for_file: must_be_immutable, library_private_types_in_public_api
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:ready_to_use_widgets/atomic/large_button.dart/custom_large_button.dart';
import 'package:ready_to_use_widgets/core/init/constants/color_constant.dart';
import 'package:ready_to_use_widgets/core/init/extensions/context_extension.dart';
class ItemCardWidget extends StatefulWidget {
final int minShowItemCount;
final List<CardLine> cardLines;
final Duration animationDuration;
final Curve animationCurve = Curves.easeInOutCubic;
final List<ItemCardButton> buttons;
bool isExpanded;
ItemCardWidget(
{Key? key,
this.isExpanded = false,
this.minShowItemCount = 3,
required this.cardLines,
this.animationDuration = const Duration(milliseconds: 180),
required this.buttons})
: super(key: key);
@override
_ItemCardWidgetState createState() => _ItemCardWidgetState();
}
class _ItemCardWidgetState extends State<ItemCardWidget>
with SingleTickerProviderStateMixin {
late AnimationController _animationController;
late Animation _animation;
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this,
duration: widget.animationDuration,
lowerBound: 0,
upperBound: 180);
_animation = CurvedAnimation(
parent: _animationController,
curve: widget.animationCurve,
reverseCurve: widget.animationCurve);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
child: Material(
elevation: 4,
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: AnimatedBuilder(
animation: _animation,
builder: (context, child) => Container(
padding: const EdgeInsets.only(top: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
buildCardLines(),
Column(
children: [
buttonGroup(widget.buttons),
buildExpandButton(),
],
)
],
)),
)),
);
}
AnimatedCrossFade buildCardLines() {
return AnimatedCrossFade(
firstCurve: widget.animationCurve,
secondCurve: widget.animationCurve,
sizeCurve: widget.animationCurve,
firstChild: buildCardLinesColumn(),
secondChild: buildCardLinesColumn(),
crossFadeState: widget.isExpanded
? CrossFadeState.showSecond
: CrossFadeState.showFirst,
duration: widget.animationDuration);
}
AnimatedBuilder buildExpandButton() {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) => ClipOval(
child: Material(
color: Colors.white,
child: InkWell(
onTap: () {
if (widget.isExpanded) {
_animationController.reverse();
widget.isExpanded = false;
} else {
_animationController.forward();
widget.isExpanded = true;
}
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Transform.rotate(
origin: const Offset(0.5, 0.5),
angle: _animationController.value * pi / 180,
child: const Icon(Icons.keyboard_arrow_down_outlined,
color: Colors.black),
),
),
),
),
),
);
}
Column buildCardLinesColumn() {
return Column(
mainAxisSize: MainAxisSize.min,
children: widget.isExpanded
? widget.cardLines
: widget.cardLines.sublist(
0,
(widget.minShowItemCount < widget.cardLines.length
? widget.minShowItemCount
: widget.cardLines.length)),
);
}
Widget buttonGroup(List<ItemCardButton> buttons) {
List<Widget> widgetList = [];
buttons.forEach((element) {
widgetList.add(
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: element,
)),
);
});
return Container(
padding: const EdgeInsets.only(
top: 4,
left: 16,
right: 16,
),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: widgetList),
);
}
}
class ItemCardButton extends StatelessWidget {
final Color? backgroundColor;
final bool isPrimary;
final String? buttonText;
final VoidCallback? onPressed;
const ItemCardButton({
Key? key,
this.isPrimary = true,
this.buttonText,
this.onPressed,
this.backgroundColor,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return LargeButton(
text: buttonText.toString(),
style: context.appTheme().itemCardWidgetTextStyle,
backgroundColor: isPrimary ? ColorConstants.blueGray : backgroundColor,
isOutlined: true,
onPressed: onPressed,
);
}
}
class CardLine extends StatelessWidget {
const CardLine({
Key? key,
required this.title,
required this.value,
required this.index,
}) : super(key: key);
final String title;
final String value;
final int index;
final _listBottomLeftTextStyle = const TextStyle(
fontWeight: FontWeight.bold, fontSize: 14, color: Colors.black);
final _listBottomRightTextStyle =
const TextStyle(fontSize: 14, color: Colors.black);
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.only(
top: 4,
left: 16,
right: 16,
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
title,
style: _listBottomLeftTextStyle,
),
SizedBox(
// width: MediaQuery.of(context).size.width*0.4,
child: Text(
value,
style: _listBottomRightTextStyle,
textAlign: TextAlign.right,
),
)
],
),
);
}
}