-
Notifications
You must be signed in to change notification settings - Fork 104
/
toggle_button.dart
157 lines (147 loc) · 5 KB
/
toggle_button.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
import 'package:flutter/material.dart';
class MyToggleButton extends StatefulWidget {
const MyToggleButton({Key? key}) : super(key: key);
@override
State<MyToggleButton> createState() => _MyToggleButtonState();
}
class _MyToggleButtonState extends State<MyToggleButton> {
final List<bool> isSelected1 = [true, false, false];
final List<bool> isSelected2 = [false, false, false];
final List<bool> isSelected3 = [true, false, false];
final List<bool> isSelected4 = [false, false];
TextStyle textStyle = const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('ToggleButton')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Single + Required\n', style: textStyle),
singleAndRequired(),
Text('\nSingle + Not Required\n', style: textStyle),
singleAndNotRequired(),
Text('\nMultiple + Required\n', style: textStyle),
multipleAndRequired(),
Text('\nMultiple + Not Required\n', style: textStyle),
multipleAndNotRequired(),
],
),
),
);
}
Widget multipleAndNotRequired() => ToggleButtons(
isSelected: isSelected4,
selectedColor: Colors.white,
color: Colors.black,
fillColor: Colors.purple,
children: const [
Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Text('Cat', style: TextStyle(fontSize: 18)),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Text('Dog', style: TextStyle(fontSize: 18)),
),
],
onPressed: (int index) {
setState(() {
isSelected4[index] = !isSelected4[index];
});
},
);
Widget multipleAndRequired() => ToggleButtons(
isSelected: isSelected3,
selectedColor: Colors.white,
color: Colors.black,
fillColor: Colors.purple,
borderWidth: 3,
borderColor: Colors.purple.shade100,
selectedBorderColor: Colors.purple.shade300,
borderRadius: BorderRadius.circular(50),
children: const [
Icon(Icons.ac_unit),
Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Text('Any', style: TextStyle(fontSize: 18)),
),
Icon(Icons.cake),
],
onPressed: (newIndex) {
final isOneSelected =
isSelected3.where((element) => element).length == 1;
if (isOneSelected && isSelected3[newIndex]) return;
setState(() {
for (int index = 0; index < isSelected3.length; index++) {
if (index == newIndex) {
isSelected3[index] = !isSelected3[index];
}
}
});
},
);
Widget singleAndNotRequired() => ToggleButtons(
isSelected: isSelected2,
selectedColor: Colors.white, // selected text color
color: Colors.black, // unselected text color
fillColor: Colors.purple,
children: const [
Icon(Icons.ac_unit),
Icon(Icons.call),
Icon(Icons.cake),
],
onPressed: (int newIndex) {
setState(() {
for (int index = 0; index < isSelected2.length; index++) {
if (index == newIndex) {
isSelected2[index] = !isSelected2[index];
} else {
isSelected2[index] = false;
}
}
});
},
);
Widget singleAndRequired() => Container(
// unselected background color
color: Colors.green.withOpacity(0.5),
child: ToggleButtons(
isSelected: isSelected1,
selectedColor: Colors.white, // selected text color
color: Colors.black, // unselected text color
fillColor: Colors.purple,
renderBorder: false, // disable border
splashColor: Colors.purple.withOpacity(0.5),
children: const [
Padding(
padding: EdgeInsets.symmetric(horizontal: 12.0),
child: Text('For Rent', style: TextStyle(fontSize: 18)),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 12.0),
child: Text('For Sale', style: TextStyle(fontSize: 18)),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 12.0),
child: Text('Sold', style: TextStyle(fontSize: 18)),
),
],
onPressed: (int newIndex) {
setState(() {
for (int index = 0; index < isSelected1.length; index++) {
if (index == newIndex) {
isSelected1[index] = true;
} else {
isSelected1[index] = false;
}
}
});
},
),
);
}