From 657a8b8994aa3bc1e8bab5240b2361324a1d60ec Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 26 Apr 2021 15:03:47 +0300 Subject: [PATCH 1/5] Add spacing property --- example/main.dart | 1 + lib/code_input.dart | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/example/main.dart b/example/main.dart index 8dfdd82..ef92b35 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(), + padding: EdgeInsets.all(5) , 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..b79454e 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.padding, this.onChanged, this.onFilled, this.onDone, @@ -46,6 +47,7 @@ class CodeInput extends StatefulWidget { TextInputType keyboardType = TextInputType.text, List inputFormatters, @required CodeInputBuilder builder, + EdgeInsetsGeometry padding = const EdgeInsets.only(left:2, right: 2), void Function(String value) onChanged, void Function(String value) onFilled, void Function(String value) onDone, @@ -65,6 +67,7 @@ class CodeInput extends StatefulWidget { inputFormatters: inputFormatters ?? _createInputFormatters(length, keyboardType), builder: builder, + padding: padding, onChanged: onChanged, onFilled: onFilled, onDone: onDone, @@ -134,6 +137,9 @@ class CodeInput extends StatefulWidget { /// See [CodeInputBuilders] for examples. final CodeInputBuilder builder; + /// A parameter for customize padding between chars + final EdgeInsetsGeometry padding; + /// A callback for changes to the input. final void Function(String value) onChanged; @@ -215,11 +221,14 @@ 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); + final characterEntity = Padding( + padding: widget.padding, + child: widget.builder(hasFocus, char)); assert( characterEntity != null, From 89726a2185e8a98da032ea78ed442848df8eb12e Mon Sep 17 00:00:00 2001 From: 0xdecade Date: Mon, 26 Apr 2021 21:44:13 +0300 Subject: [PATCH 2/5] Apply suggestions from code review [marcelgarus] Co-authored-by: Marcel Garus --- example/main.dart | 2 +- lib/code_input.dart | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/example/main.dart b/example/main.dart index ef92b35..4732a21 100644 --- a/example/main.dart +++ b/example/main.dart @@ -21,7 +21,7 @@ class MyApp extends StatelessWidget { length: 4, keyboardType: TextInputType.number, builder: CodeInputBuilders.lightCircle(), - padding: EdgeInsets.all(5) , + padding: EdgeInsets.all(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 b79454e..9c57f90 100644 --- a/lib/code_input.dart +++ b/lib/code_input.dart @@ -47,7 +47,7 @@ class CodeInput extends StatefulWidget { TextInputType keyboardType = TextInputType.text, List inputFormatters, @required CodeInputBuilder builder, - EdgeInsetsGeometry padding = const EdgeInsets.only(left:2, right: 2), + EdgeInsetsGeometry padding = const EdgeInsets.only(left: 2, right: 2), void Function(String value) onChanged, void Function(String value) onFilled, void Function(String value) onDone, @@ -137,7 +137,7 @@ class CodeInput extends StatefulWidget { /// See [CodeInputBuilders] for examples. final CodeInputBuilder builder; - /// A parameter for customize padding between chars + /// A parameter for customizing the padding between characters. final EdgeInsetsGeometry padding; /// A callback for changes to the input. @@ -227,8 +227,9 @@ class _CodeInputState extends State { final hasFocus = controller.selection.start == i; final char = i < text.length ? text[i] : ''; final characterEntity = Padding( - padding: widget.padding, - child: widget.builder(hasFocus, char)); + padding: widget.padding, + child: widget.builder(hasFocus, char), + ); assert( characterEntity != null, From f522d7b61c1ba86c36bc5631976ff6536bdb41f6 Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 26 Apr 2021 22:08:21 +0300 Subject: [PATCH 3/5] fix: Make spacing parameter more intuitive --- example/main.dart | 2 +- lib/code_input.dart | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/example/main.dart b/example/main.dart index 4732a21..66a36a7 100644 --- a/example/main.dart +++ b/example/main.dart @@ -21,7 +21,7 @@ class MyApp extends StatelessWidget { length: 4, keyboardType: TextInputType.number, builder: CodeInputBuilders.lightCircle(), - padding: EdgeInsets.all(8), + 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 9c57f90..92cc1ad 100644 --- a/lib/code_input.dart +++ b/lib/code_input.dart @@ -34,7 +34,7 @@ class CodeInput extends StatefulWidget { @required this.keyboardType, @required this.inputFormatters, @required this.builder, - this.padding, + this.spacing, this.onChanged, this.onFilled, this.onDone, @@ -47,7 +47,7 @@ class CodeInput extends StatefulWidget { TextInputType keyboardType = TextInputType.text, List inputFormatters, @required CodeInputBuilder builder, - EdgeInsetsGeometry padding = const EdgeInsets.only(left: 2, right: 2), + double spacing = 2, void Function(String value) onChanged, void Function(String value) onFilled, void Function(String value) onDone, @@ -67,7 +67,7 @@ class CodeInput extends StatefulWidget { inputFormatters: inputFormatters ?? _createInputFormatters(length, keyboardType), builder: builder, - padding: padding, + spacing: spacing, onChanged: onChanged, onFilled: onFilled, onDone: onDone, @@ -138,7 +138,7 @@ class CodeInput extends StatefulWidget { final CodeInputBuilder builder; /// A parameter for customizing the padding between characters. - final EdgeInsetsGeometry padding; + final double spacing; /// A callback for changes to the input. final void Function(String value) onChanged; @@ -227,7 +227,7 @@ class _CodeInputState extends State { final hasFocus = controller.selection.start == i; final char = i < text.length ? text[i] : ''; final characterEntity = Padding( - padding: widget.padding, + padding: EdgeInsets.only(left: widget.spacing), child: widget.builder(hasFocus, char), ); From 60103236c3ab2d2653d3b38ba53115dfe098a7ee Mon Sep 17 00:00:00 2001 From: 0xdecade Date: Tue, 27 Apr 2021 10:22:15 +0300 Subject: [PATCH 4/5] Increase default spacing size from 2 to 8px. Co-authored-by: Marcel Garus --- lib/code_input.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/code_input.dart b/lib/code_input.dart index 92cc1ad..0c4cc79 100644 --- a/lib/code_input.dart +++ b/lib/code_input.dart @@ -47,7 +47,7 @@ class CodeInput extends StatefulWidget { TextInputType keyboardType = TextInputType.text, List inputFormatters, @required CodeInputBuilder builder, - double spacing = 2, + double spacing = 8, void Function(String value) onChanged, void Function(String value) onFilled, void Function(String value) onDone, From d127cfccf45d98fae3f4e929d73ee42d14eac8ec Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 27 Apr 2021 10:47:48 +0300 Subject: [PATCH 5/5] Skipped applying spacing on the first character; Renamed characterEntity variable to characterWidget; Adjusted docstrings according to the change above; --- lib/code_input.dart | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/code_input.dart b/lib/code_input.dart index 0c4cc79..89b545a 100644 --- a/lib/code_input.dart +++ b/lib/code_input.dart @@ -74,7 +74,7 @@ class CodeInput extends StatefulWidget { ); } - /// The length of character entities to always display. + /// The length of character widgets to always display. /// /// ## Sample code /// @@ -132,7 +132,7 @@ 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; @@ -208,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); @@ -226,18 +226,21 @@ class _CodeInputState extends State { children: List.generate(widget.length, (i) { final hasFocus = controller.selection.start == i; final char = i < text.length ? text[i] : ''; - final characterEntity = Padding( - padding: EdgeInsets.only(left: widget.spacing), - child: 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; }), ), )), @@ -246,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.