Line data Source code
1 : part of 'foss_text_field.dart';
2 :
3 : // The text selection highlight is the ring color at a low alpha.
4 : const double _focusRingOpacity = 0.24;
5 :
6 : /// The editable core: the [EditableText], the placeholder shown while it is
7 : /// empty, and the field semantics. Selection, cursor, and hint colors resolve
8 : /// from [colors] and [visuals]; the owning state passes its [controller],
9 : /// [focusNode], and [editableKey] in so the gesture detector keeps driving it.
10 : class _FieldEditable extends StatelessWidget {
11 4 : const _FieldEditable({
12 : required this.editableKey,
13 : required this.controller,
14 : required this.focusNode,
15 : required this.colors,
16 : required this.visuals,
17 : required this.enabled,
18 : required this.obscureText,
19 : required this.keyboardType,
20 : required this.textInputAction,
21 : required this.minLines,
22 : required this.maxLines,
23 : required this.label,
24 : required this.hintText,
25 : required this.onChanged,
26 : required this.onSubmitted,
27 : });
28 :
29 : final GlobalKey<EditableTextState> editableKey;
30 : final TextEditingController controller;
31 : final FocusNode focusNode;
32 : final FossColors colors;
33 : final _FieldVisuals visuals;
34 : final bool enabled;
35 : final bool obscureText;
36 : final TextInputType? keyboardType;
37 : final TextInputAction? textInputAction;
38 : final int? minLines;
39 : final int? maxLines;
40 : final String? label;
41 : final String? hintText;
42 : final ValueChanged<String>? onChanged;
43 : final ValueChanged<String>? onSubmitted;
44 :
45 4 : @override
46 : Widget build(BuildContext context) {
47 8 : final multiline = maxLines != 1;
48 4 : final editable = EditableText(
49 4 : key: editableKey,
50 4 : controller: controller,
51 4 : focusNode: focusNode,
52 4 : readOnly: !enabled,
53 : rendererIgnoresPointer: true,
54 20 : style: visuals.textStyle.copyWith(color: visuals.textColor),
55 : // The base type's line-height adds leading; distributed proportionally
56 : // (the default) most of it lands above the glyph and drops it below
57 : // center, so it is split evenly to center the text in the box.
58 : textHeightBehavior: const TextHeightBehavior(
59 : leadingDistribution: TextLeadingDistribution.even,
60 : ),
61 8 : cursorColor: colors.foreground,
62 8 : backgroundCursorColor: colors.mutedForeground,
63 12 : selectionColor: colors.ring.withValues(alpha: _focusRingOpacity),
64 : cursorOpacityAnimates: true,
65 4 : keyboardType: keyboardType,
66 4 : textInputAction: textInputAction,
67 4 : obscureText: obscureText,
68 : // An obscured field must not feed the keyboard's autocorrect or
69 : // suggestion engines with password characters.
70 4 : autocorrect: !obscureText,
71 4 : enableSuggestions: !obscureText,
72 8 : keyboardAppearance: colors.isDark ? Brightness.dark : Brightness.light,
73 4 : minLines: minLines,
74 4 : maxLines: maxLines,
75 4 : onChanged: onChanged,
76 4 : onSubmitted: onSubmitted,
77 4 : enableInteractiveSelection: enabled,
78 : );
79 :
80 4 : final hint = hintText;
81 4 : return MergeSemantics(
82 : // The hint is exposed through [Semantics.hint], not as a child, so an
83 : // empty labelled field does not announce label and placeholder as one.
84 4 : child: Semantics(
85 4 : label: label,
86 : hint: hint,
87 : textField: true,
88 : multiline: multiline,
89 4 : enabled: enabled,
90 4 : child: Stack(
91 4 : children: [
92 : if (hint != null)
93 1 : ValueListenableBuilder<TextEditingValue>(
94 1 : valueListenable: controller,
95 3 : builder: (context, value, _) => value.text.isEmpty
96 1 : ? IgnorePointer(
97 1 : child: ExcludeSemantics(
98 1 : child: Text(
99 : hint,
100 1 : maxLines: maxLines,
101 : overflow: multiline
102 : ? TextOverflow.clip
103 : : TextOverflow.ellipsis,
104 : // Match the editable's even leading so the hint and
105 : // the typed value share a baseline, with no jump on
106 : // the first keystroke.
107 : textHeightBehavior: const TextHeightBehavior(
108 : leadingDistribution: TextLeadingDistribution.even,
109 : ),
110 3 : style: visuals.textStyle.copyWith(
111 2 : color: visuals.hintColor,
112 : ),
113 : ),
114 : ),
115 : )
116 : : const SizedBox.shrink(),
117 : ),
118 4 : editable,
119 : ],
120 : ),
121 : ),
122 : );
123 : }
124 : }
|