Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MyApp extends StatelessWidget {
length: 4,
keyboardType: TextInputType.number,
builder: CodeInputBuilders.lightCircle(),
spacing: 8,
onFilled: (value) => print('Your input is $value.'),
onDone: (value) => print('Your input is $value.'),
);
Expand Down
29 changes: 21 additions & 8 deletions lib/code_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CodeInput extends StatefulWidget {
@required this.keyboardType,
@required this.inputFormatters,
@required this.builder,
this.spacing,
this.onChanged,
this.onFilled,
this.onDone,
Expand All @@ -46,6 +47,7 @@ class CodeInput extends StatefulWidget {
TextInputType keyboardType = TextInputType.text,
List<TextInputFormatter> inputFormatters,
@required CodeInputBuilder builder,
double spacing = 8,
void Function(String value) onChanged,
void Function(String value) onFilled,
void Function(String value) onDone,
Expand All @@ -65,13 +67,14 @@ class CodeInput extends StatefulWidget {
inputFormatters:
inputFormatters ?? _createInputFormatters(length, keyboardType),
builder: builder,
spacing: spacing,
onChanged: onChanged,
onFilled: onFilled,
onDone: onDone,
);
}

/// The length of character entities to always display.
/// The length of character widgets to always display.
///
/// ## Sample code
///
Expand Down Expand Up @@ -129,11 +132,14 @@ class CodeInput extends StatefulWidget {
/// ```
final List<TextInputFormatter> inputFormatters;

/// A builder for the character entities.
/// A builder for the character widgets.
///
/// See [CodeInputBuilders] for examples.
final CodeInputBuilder builder;

/// A parameter for customizing the padding between characters.
final double spacing;

/// A callback for changes to the input.
final void Function(String value) onChanged;

Expand Down Expand Up @@ -202,7 +208,7 @@ class _CodeInputState extends State<CodeInput> {
)),
// These are the actual character widgets. A transparent container lies
// right below the gesture detector, so all taps get collected, even
// the ones between the character entities.
// the ones between the character widgets.
GestureDetector(
onTap: () {
final focusScope = FocusScope.of(context);
Expand All @@ -215,19 +221,26 @@ class _CodeInputState extends State<CodeInput> {
child: Container(
color: Colors.transparent,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: List.generate(widget.length, (i) {
final hasFocus = controller.selection.start == i;
final char = i < text.length ? text[i] : '';
final characterEntity = widget.builder(hasFocus, char);
var characterWidget = widget.builder(hasFocus, char);
if (i > 0) {
characterWidget = Padding(
padding: EdgeInsets.only(left: widget.spacing),
child: widget.builder(hasFocus, char),
);
}

assert(
characterEntity != null,
'The builder for the character entity at position $i '
characterWidget != null,
'The builder for the character widget at position $i '
'returned null. It did${hasFocus ? ' not' : ''} have the '
'focus and the character passed to it was \'$char\'.');

return characterEntity;
return characterWidget;
}),
),
)),
Expand All @@ -236,7 +249,7 @@ class _CodeInputState extends State<CodeInput> {
}

/// An abstract class that provides some commonly-used builders for the
/// character entities.
/// character widgets.
///
/// * [containerized]: A builder putting chars in an animated container.
/// * [circle]: A builder putting chars in circles.
Expand Down