-
Notifications
You must be signed in to change notification settings - Fork 10
/
radio_button_group.dart
94 lines (84 loc) · 2.61 KB
/
radio_button_group.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
import 'package:flutter/material.dart';
class RadioButtonGroup extends StatefulWidget {
RadioButtonGroup({
@required this.buttonValues,
this.radioButtonValue,
@required this.buttonColor,
@required this.buttonSelectedColor,
this.textColor = Colors.black,
this.textSelectedColor = Colors.white,
this.horizontalSpace = 10,
this.verticalSpace = 10,
this.fontSize = 15,
this.horizontalButtonCount = 3,
this.buttonAspectRatio = 3 / 1,
this.buttonBorderRadius = 5.0,
this.defaultIndex = 0,
}) : assert(buttonColor != null),
assert(buttonSelectedColor != null);
final double fontSize;
final List buttonValues;
final Function(dynamic) radioButtonValue;
final int defaultIndex;
final double horizontalSpace;
final double verticalSpace;
final int horizontalButtonCount;
final double buttonAspectRatio;
final Color buttonColor;
final Color buttonSelectedColor;
final Color textColor;
final Color textSelectedColor;
final double buttonBorderRadius;
_RadioButtonGroupState createState() => _RadioButtonGroupState();
}
class _RadioButtonGroupState extends State<RadioButtonGroup> {
int currentSelected = 0;
@override
void initState() {
super.initState();
currentSelected = widget.defaultIndex;
}
_buildButton(int index) {
return GestureDetector(
onTap: () {
widget.radioButtonValue(index);
currentSelected = index;
if (mounted) {
setState(() {});
}
},
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(widget.buttonBorderRadius),
color: currentSelected == index ? widget.buttonSelectedColor : widget.buttonColor,
),
child: Text(
widget.buttonValues[index],
style: TextStyle(
color: currentSelected == index ? widget.textSelectedColor : widget.textColor,
fontSize: widget.fontSize,
),
),
),
);
}
@override
Widget build(BuildContext context) {
return GridView.builder(
padding: EdgeInsets.all(0.0),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: widget.buttonValues.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: widget.horizontalButtonCount,
crossAxisSpacing: widget.verticalSpace,
mainAxisSpacing: widget.horizontalSpace,
childAspectRatio: widget.buttonAspectRatio,
),
itemBuilder: (BuildContext context, int index) {
return _buildButton(index);
},
);
}
}