본문 바로가기
It Study/앱 개발 실습 기록(Flutter)

플러터 - 라디오버튼, 스위치, 슬라이더, 팝업메뉴버튼 상업적 이용x

by prlkt5200 2024. 8. 3.
반응형

연습예제입니다.

한번 따라쳐보시거나, 전체적으로 보고 모르는 부분을 구글링해도 좋을 듯 싶습니다.


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
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
 
const assetImagePath = 'assets/images';
const bannerImage = '$assetImagePath/banner.png';
 
void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        body: Body(),
      ),
    ),
  );
}
 
class Body extends StatelessWidget {
  const Body({super.key});
 
  @override
  Widget build(BuildContext context) {
    return const Column(
      children: [
        TestCheckBox(),
        TestRadioButton(),
        TestSlider(),
        TestSwitch(),
        TestPobupMenu(),
      ],
    );
  }
}
 
class TestCheckBox extends StatefulWidget {
  const TestCheckBox({super.key});
 
  @override
  State<TestCheckBox> createState() => _TestCheckBoxState();
}
 
class _TestCheckBoxState extends State<TestCheckBox> {
  late List<bool> values;
 
  @override
  void initState() {
    super.initState();
    values = [falsefalsefalse];
  }
 
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Checkbox(
            value: values[0],
            onChanged: (value) => changeValue(0, value: value)),
        Checkbox(
            value: values[1],
            onChanged: (value) => changeValue(1, value: value)),
        Checkbox(
            value: values[2],
            onChanged: (value) => changeValue(2, value: value)),
      ],
    );
  }
 
  void changeValue(int index, {bool? value = false}) {
    setState(() {
      values[index] = value!;
    });
  }
}
 
enum TestValue {
  test1,
  test2,
  test3,
}
 
class TestRadioButton extends StatefulWidget {
  const TestRadioButton({super.key});
 
  @override
  State<TestRadioButton> createState() => _TestRadioButtonState();
}
 
class _TestRadioButtonState extends State<TestRadioButton> {
  TestValue? selectValue;
 
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ListTile(
          leading: Radio<TestValue>(
            value: TestValue.test1,
            groupValue: selectValue,
            onChanged: (value) => setState(
              () => selectValue = value!,
            ),
          ),
          title: Text(TestValue.test1.name),
          onTap: () => setState(() {
            if (selectValue != TestValue.test1) {
              selectValue = TestValue.test1;
            }
          }),
        ),
        Radio<TestValue>(
          value: TestValue.test2,
          groupValue: selectValue,
          onChanged: (value) => setState(
            () {
              selectValue = value!;
            },
          ),
        ),
        Radio<TestValue>(
          value: TestValue.test3,
          groupValue: selectValue,
          onChanged: (value) => setState(
            () {
              selectValue = value!;
            },
          ),
        ),
      ],
    );
  }
}
 
class TestSlider extends StatefulWidget {
  const TestSlider({super.key});
 
  @override
  State<TestSlider> createState() => _TestSliderState();
}
 
class _TestSliderState extends State<TestSlider> {
  double value = 0;
 
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text('${value.round()}'),
        Slider(
          value: value,
          onChanged: (newValue) => setState(() => value = newValue),
          divisions: 100,
          max: 100,
          min: 0,
          label: value.round().toString(),
        ),
      ],
    );
  }
}
 
class TestSwitch extends StatefulWidget {
  const TestSwitch({super.key});
 
  @override
  State<TestSwitch> createState() => _TestSwitchState();
}
 
class _TestSwitchState extends State<TestSwitch> {
  bool value = false;
 
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        CupertinoSwitch(
            value: value,
            onChanged: (newValue) => setState(() {
                  newValue = value;
                })),
        Switch(
            value: value,
            onChanged: (newValue) => setState(() => value = newValue)),
      ],
    );
  }
}
 
class TestPobupMenu extends StatefulWidget {
  const TestPobupMenu({super.key});
 
  @override
  State<TestPobupMenu> createState() => _TestPobupMenuState();
}
 
class _TestPobupMenuState extends State<TestPobupMenu> {
  TestValue selectValue = TestValue.test1;
 
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(selectValue.name),
        PopupMenuButton(
          itemBuilder: (context) {
            return TestValue.values
                .map((value) =>
                    PopupMenuItem(value: value, child: Text(value.name)))
                .toList();
          },
          onSelected: (newValue) => setState(() => selectValue = newValue),
        ),
      ],
    );
  }
}
 
cs

 

반응형