-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_dropdown_button_form_field.dart
78 lines (74 loc) · 2.33 KB
/
custom_dropdown_button_form_field.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
import 'package:flutter/material.dart';
import 'package:flutter_app_sample/util/dropdown_item.dart';
class CustomDropdownButtonFormField<T> extends StatelessWidget {
const CustomDropdownButtonFormField({
super.key,
required this.itemList,
required this.onChanged,
this.decoration,
this.disabledHint,
this.minWidth = 200,
this.maxWidth = double.infinity,
});
final List<DropdownItem> itemList;
final ValueChanged<T?> onChanged;
final InputDecoration? decoration;
final String? disabledHint;
final double minWidth;
/// 横幅を固定したい際は、minWidthと同じ値を入れる。
final double maxWidth;
@override
Widget build(BuildContext context) {
return IntrinsicWidth(
child: ConstrainedBox(
constraints: BoxConstraints(
minWidth: minWidth,
maxWidth: maxWidth,
),
child: DropdownButtonFormField(
decoration: decoration ??
const InputDecoration(
// デフォルトだと縦の余白が大きく感じる
contentPadding: EdgeInsets.symmetric(horizontal: 10),
border: OutlineInputBorder(),
),
selectedItemBuilder: (context) {
// 調整がシビア
final selectedMaxWidth = MediaQuery.of(context).size.width - 70;
return itemList
.map(
(item) => ConstrainedBox(
constraints: BoxConstraints(
minWidth: minWidth,
maxWidth: selectedMaxWidth,
),
child: Text(
item.dropDownLabel,
overflow: TextOverflow.ellipsis,
),
),
)
.toList();
},
items: itemList
.map(
(item) => DropdownMenuItem(
value: item,
child: Text(
item.dropDownLabel,
),
),
)
.toList(),
disabledHint: disabledHint != null ? Text(disabledHint!) : null,
onChanged: (value) {
if (value != null) {
final item = value.item as T;
onChanged(item);
}
},
),
),
);
}
}