aboutsummaryrefslogtreecommitdiff
path: root/lib/widgets/common/basic/text_dropdown_button.dart
blob: e282446c2f5d4c137dbd02de272673e6e8ecc930 (plain) (blame)
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
import 'package:flutter/material.dart';

class TextDropdownButton<T> extends StatefulWidget {
  final List<T> values;
  final String Function(T value) valueText;
  final IconData Function(T value)? valueIcon;
  final T? value;
  final Widget? underline;
  final bool isExpanded;
  final double? itemHeight;
  final Color? dropdownColor;
  final ValueChanged<T?>? onChanged;

  const TextDropdownButton({
    super.key,
    required this.values,
    required this.valueText,
    this.valueIcon,
    this.value,
    this.underline,
    this.isExpanded = false,
    this.itemHeight = kMinInteractiveDimension,
    this.dropdownColor,
    required this.onChanged,
  });

  @override
  State<TextDropdownButton<T>> createState() => _TextDropdownButtonState<T>();
}

class _TextDropdownButtonState<T> extends State<TextDropdownButton<T>> {
  @override
  Widget build(BuildContext context) {
    return DropdownButton(
      items: widget.values
          .map((v) => DropdownMenuItem<T>(
                value: v,
                child: _buildItem(widget.valueText(v), widget.valueIcon?.call(v), selected: false),
              ))
          .toList(),
      selectedItemBuilder: (context) => widget.values
          .map((v) => DropdownMenuItem<T>(
                value: v,
                child: _buildItem(widget.valueText(v), widget.valueIcon?.call(v), selected: true),
              ))
          .toList(),
      value: widget.value,
      style: Theme.of(context).textTheme.titleMedium!.copyWith(fontWeight: FontWeight.normal),
      underline: widget.underline,
      isExpanded: widget.isExpanded,
      itemHeight: widget.itemHeight,
      dropdownColor: widget.dropdownColor,
      onChanged: widget.onChanged,
    );
  }

  static Widget _buildItem<T>(String text, IconData? icon, {required bool selected}) {
    final softWrap = selected ? false : null;
    final overflow = selected ? TextOverflow.fade : null;

    Widget child = icon != null
        ? Text.rich(
            TextSpan(
              children: [
                WidgetSpan(
                  alignment: PlaceholderAlignment.middle,
                  child: Padding(
                    padding: const EdgeInsetsDirectional.only(end: 8, bottom: 2),
                    child: Icon(icon),
                  ),
                ),
                TextSpan(text: text),
              ],
            ),
            softWrap: softWrap,
            overflow: overflow,
          )
        : Text(
            text,
            softWrap: softWrap,
            overflow: overflow,
          );

    if (selected) {
      child = Align(
        alignment: AlignmentDirectional.centerStart,
        child: child,
      );
    }

    return child;
  }
}