diff --git a/example/main.dart b/example/main.dart index 8dfdd82..66a36a7 100644 --- a/example/main.dart +++ b/example/main.dart @@ -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.'), ); diff --git a/lib/code_input.dart b/lib/code_input.dart index a1db4db..89b545a 100644 --- a/lib/code_input.dart +++ b/lib/code_input.dart @@ -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, @@ -46,6 +47,7 @@ class CodeInput extends StatefulWidget { TextInputType keyboardType = TextInputType.text, List inputFormatters, @required CodeInputBuilder builder, + double spacing = 8, void Function(String value) onChanged, void Function(String value) onFilled, void Function(String value) onDone, @@ -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 /// @@ -129,11 +132,14 @@ class CodeInput extends StatefulWidget { /// ``` final List 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; @@ -202,7 +208,7 @@ class _CodeInputState extends State { )), // 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); @@ -215,19 +221,26 @@ class _CodeInputState extends State { 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; }), ), )), @@ -236,7 +249,7 @@ class _CodeInputState extends State { } /// 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.