Skip to content

Commit d1211e9

Browse files
authored
Merge pull request #181 from adrianFarkas/feat/placeholder
Add hint/placeholder feature
2 parents 4df5bc2 + 4bf552d commit d1211e9

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ A flutter package which will help you to generate pin code fields with beautiful
216216
/// Use external [AutoFillGroup]
217217
final bool useExternalAutoFillGroup;
218218
219+
/// Displays a hint or placeholder in the field if it's value is empty.
220+
/// It only appears if it's not null. Single character is recommended.
221+
final String? hintCharacter;
222+
223+
/// the style of the [hintCharacter], default is [fontSize: 20, fontWeight: FontWeight.bold]
224+
/// and it also uses the [textStyle]'s properties
225+
/// [TextStyle.color] is [Colors.grey]
226+
final TextStyle? hintStyle;
227+
219228
```
220229

221230
**PinTheme**

lib/src/pin_code_fields.dart

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ class PinCodeTextField extends StatefulWidget {
171171
/// Use external [AutoFillGroup]
172172
final bool useExternalAutoFillGroup;
173173

174+
/// Displays a hint or placeholder in the field if it's value is empty.
175+
/// It only appears if it's not null. Single character is recommended.
176+
final String? hintCharacter;
177+
178+
/// the style of the [hintCharacter], default is [fontSize: 20, fontWeight: FontWeight.bold]
179+
/// and it also uses the [textStyle]'s properties
180+
/// [TextStyle.color] is [Colors.grey]
181+
final TextStyle? hintStyle;
182+
174183
PinCodeTextField({
175184
Key? key,
176185
required this.appContext,
@@ -220,6 +229,8 @@ class PinCodeTextField extends StatefulWidget {
220229
this.cursorColor,
221230
this.cursorWidth = 2,
222231
this.cursorHeight,
232+
this.hintCharacter,
233+
this.hintStyle,
223234

224235
/// Default for [AutofillGroup]
225236
this.onAutoFillDisposeAction = AutofillContextAction.commit,
@@ -271,6 +282,14 @@ class _PinCodeTextFieldState extends State<PinCodeTextField>
271282
fontWeight: FontWeight.bold,
272283
).merge(widget.textStyle);
273284

285+
TextStyle get _hintStyle => _textStyle
286+
.copyWith(
287+
color: _pinTheme.disabledColor,
288+
)
289+
.merge(widget.hintStyle);
290+
291+
bool get _hintAvailable => widget.hintCharacter != null;
292+
274293
@override
275294
void initState() {
276295
// if (!kReleaseMode) {
@@ -490,10 +509,18 @@ class _PinCodeTextFieldState extends State<PinCodeTextField>
490509
}
491510
}
492511

512+
if (_inputList[index!].isEmpty && _hintAvailable) {
513+
return Text(
514+
widget.hintCharacter!,
515+
key: ValueKey(_inputList[index]),
516+
style: _hintStyle,
517+
);
518+
}
519+
493520
return Text(
494-
widget.obscureText && _inputList[index!].isNotEmpty && showObscured
521+
widget.obscureText && _inputList[index].isNotEmpty && showObscured
495522
? widget.obscuringCharacter
496-
: _inputList[index!],
523+
: _inputList[index],
497524
key: ValueKey(_inputList[index]),
498525
style: _textStyle,
499526
);

0 commit comments

Comments
 (0)