@@ -7,7 +7,7 @@ class YYRadioListTile extends StatefulWidget {
7
7
YYRadioListTile ({
8
8
Key key,
9
9
this .items,
10
- this .intialValue= 0 ,
10
+ this .intialValue,
11
11
this .activeColor,
12
12
this .onChanged,
13
13
}) : assert (items != null ),
@@ -25,32 +25,38 @@ class YYRadioListTile extends StatefulWidget {
25
25
}
26
26
27
27
class YYRadioListTileState extends State <YYRadioListTile > {
28
- var groupId = 0 ;
29
- var selectedItem = - 1 ;
28
+ var groupId = - 1 ;
29
+
30
+ void intialSelectedItem () {
31
+ //intialValue:
32
+ //The button initializes the position.
33
+ //If it is not filled, it is not selected.
34
+ if (groupId == - 1 ) {
35
+ groupId = widget.intialValue ?? - 1 ;
36
+ }
37
+ }
30
38
31
39
@override
32
40
Widget build (BuildContext context) {
33
- if (selectedItem == - 1 ) {
34
- selectedItem = widget.intialValue != null ? widget.intialValue : 0 ;
35
- }
41
+ intialSelectedItem ();
42
+
36
43
return ListView .builder (
37
44
padding: EdgeInsets .all (0.0 ),
38
45
shrinkWrap: true ,
39
46
itemCount: widget.items.length,
40
47
itemBuilder: (BuildContext context, int index) {
41
- var radioItem = widget.items[index];
42
48
return Material (
43
49
color: Colors .white,
44
50
child: RadioListTile (
45
- title: Text (radioItem .text),
46
- value: radioItem.value == null ? index: radioItem.value ,
47
- groupValue: selectedItem ,
51
+ title: Text (widget.items[index] .text),
52
+ value: index,
53
+ groupValue: groupId ,
48
54
activeColor: widget.activeColor,
49
55
onChanged: (int value) {
50
56
setState (() {
51
- selectedItem = value;
57
+ widget.onChanged (value);
58
+ groupId = value;
52
59
});
53
- widget.onChanged (value);
54
60
},
55
61
),
56
62
);
0 commit comments