diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 0f6a5e5..3f46a61 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 28 + compileSdkVersion 31 sourceSets { main.java.srcDirs += 'src/main/kotlin' @@ -40,7 +40,7 @@ android { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.example" minSdkVersion 16 - targetSdkVersion 28 + targetSdkVersion 31 versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 8bc6007..f087356 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -6,12 +6,12 @@ additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> { onPressed: () { print("Floating button was pressed."); otpController.clear(); - // otpController.set(['2', '3', '5', '5', '7']); - // otpController.setValue('3', 0); - // otpController.setFocus(1); }, ), body: Center( diff --git a/example/pubspec.lock b/example/pubspec.lock index 5f86f37..e0ee5f7 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -42,7 +42,7 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" cupertino_icons: dependency: "direct main" description: @@ -56,7 +56,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" flutter: dependency: "direct main" description: flutter @@ -80,7 +80,7 @@ packages: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.1.3" + version: "0.1.4" meta: dependency: transitive description: @@ -94,14 +94,14 @@ packages: path: ".." relative: true source: path - version: "1.1.2" + version: "1.1.3" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" sky_engine: dependency: transitive description: flutter @@ -113,7 +113,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -148,20 +148,13 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.8" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.9" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" + version: "2.1.2" sdks: - dart: ">=2.14.0 <3.0.0" + dart: ">=2.17.0-0 <3.0.0" diff --git a/lib/otp_field.dart b/lib/otp_field.dart index 5234a6a..3d94350 100644 --- a/lib/otp_field.dart +++ b/lib/otp_field.dart @@ -178,15 +178,21 @@ class _OTPTextFieldState extends State { right: isLast ? 0 : widget.spaceBetween, ), child: TextField( + onTap: () { + _textControllers[index]!.selection = TextSelection.fromPosition( + TextPosition(offset: _textControllers[index]!.text.length)); + }, controller: _textControllers[index], keyboardType: widget.keyboardType, textCapitalization: widget.textCapitalization, textAlign: TextAlign.center, style: widget.style, inputFormatters: widget.inputFormatter, - maxLength: 1, + //maxLength: 1, + maxLength: 2, focusNode: _focusNodes[index], obscureText: widget.obscureText, + showCursor: false, decoration: InputDecoration( isDense: widget.isDense, filled: true, @@ -205,8 +211,25 @@ class _OTPTextFieldState extends State { ), onChanged: (String str) { if (str.length > 1) { - _handlePaste(str); - return; + if (str.length == widget.length && index == widget.length - 1) { + print('Handling Paste'); + _handlePaste(str); + return; + } else { + // User modified same position + if (_pin.length >= index + 1) { + // remove previously entered digit + str = str.replaceFirst(_pin[index], ''); + } else { + // Take the last entered digit as otp entry + int len = str.length; + str = str[len - 1]; + } + // Update the current pin + setState(() { + _textControllers[index]!.text = str; + }); + } } // Check if the current value at this position is empty @@ -235,7 +258,8 @@ class _OTPTextFieldState extends State { // Call the `onCompleted` callback function provided if (!_pin.contains(null) && !_pin.contains('') && - currentPin.length == widget.length) { + currentPin.length == widget.length && + index + 1 == widget.length) { // Only call this when user is on last digit widget.onCompleted?.call(currentPin); } diff --git a/pubspec.lock b/pubspec.lock index 3317f5f..7d27b51 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -42,14 +42,14 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" flutter: dependency: "direct main" description: flutter @@ -73,7 +73,7 @@ packages: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.1.3" + version: "0.1.4" meta: dependency: transitive description: @@ -87,7 +87,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" sky_engine: dependency: transitive description: flutter @@ -99,7 +99,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -134,20 +134,13 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.8" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.9" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" + version: "2.1.2" sdks: - dart: ">=2.14.0 <3.0.0" + dart: ">=2.17.0-0 <3.0.0"