-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathstar_choice_question_body.dart
More file actions
97 lines (90 loc) · 3.13 KB
/
Copy pathstar_choice_question_body.dart
File metadata and controls
97 lines (90 loc) · 3.13 KB
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
part of '../../../ui.dart';
class RPUIStarChoiceQuestionBody extends StatefulWidget {
final RPStarChoiceAnswerFormat answerFormat;
final void Function(dynamic) onResultChance;
const RPUIStarChoiceQuestionBody(
this.answerFormat,
this.onResultChance, {
super.key,
});
@override
RPUIStarChoiceQuestionBodyState createState() =>
RPUIStarChoiceQuestionBodyState();
}
class RPUIStarChoiceQuestionBodyState
extends State<RPUIStarChoiceQuestionBody>
with AutomaticKeepAliveClientMixin<RPUIStarChoiceQuestionBody> {
RPStarChoice? _selectedItem;
bool isLeftOfSelected(RPStarChoice item,List<RPStarChoice> items){
return (_selectedItem==null)?false:(items.indexOf(_selectedItem!)>=items.indexOf(item));
//items.indexOf(_selectedItem);
}
@override
Widget build(BuildContext context) {
super.build(context);
RPLocalizations? locale = RPLocalizations.of(context);
String text = (_selectedItem == null)
? (locale?.translate('select_star') ?? 'Select how many')
: (locale?.translate(_selectedItem!.description) ??
_selectedItem!.description);
return SizedBox(
height: 160,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
_buildList(context, widget.answerFormat.choices),
Text(
text,
style: Theme.of(context).textTheme.headlineSmall,
)
],
));
}
Row _buildList(BuildContext context, List<RPStarChoice> items) {
List<Widget> list = [];
for (var item in items) {
list.add(
InkWell(
borderRadius: BorderRadius.circular(15),
onTap: () {
setState(() {
_selectedItem = item == _selectedItem ? null : item;
});
widget.onResultChance(_selectedItem);
},
child: Container(
// Highlighting of chosen answer
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5 * 25 / items.length)),
border: Border.all(
color: _selectedItem == item
? Theme.of(context).dividerColor
: Colors.transparent,
width: 3,
),
),
// Scaling item size with number of choices
// Max size is 125
padding: EdgeInsets.all(10 / items.length),
width:
(MediaQuery.of(context).size.width * 0.8) / items.length > 125
? 125
: MediaQuery.of(context).size.width * 0.8 / items.length,
height:
(MediaQuery.of(context).size.width * 0.8) / items.length > 125
? 125
: MediaQuery.of(context).size.width * 0.8 / items.length,
child: isLeftOfSelected(item,items)?Image.asset(item.starActiveUrl):Image.asset(item.starNotActiveUrl),
),
),
);
}
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: list,
);
}
@override
bool get wantKeepAlive => true;
}