Skip to content

Commit

Permalink
Removed SmartAuth package
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkko committed Apr 17, 2024
1 parent debebfc commit 1bedba6
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 80 deletions.
1 change: 0 additions & 1 deletion .flutter-plugins-dependencies

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#### 5.0.0 · 9/04/2024
- Implemented Pinput.builder to build custom Pinput fields
- Migrated deprecated imperative apply of Flutter's Gradle plugins example app


#### 4.0.0 · 10/02/2024
- Fixed RECEIVER_EXPORTED exception in android SDK 34 PR
- Fix "Namespace not specified" error when upgrading to AGP 8.0 PR
Expand Down
3 changes: 0 additions & 3 deletions example/lib/demo/builder_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ class _PinputBuilderExampleState extends State<PinputBuilderExample> {
},
controller: pinController,
focusNode: focusNode,
androidSmsAutofillMethod:
AndroidSmsAutofillMethod.smsUserConsentApi,
listenForMultipleSmsOnAndroid: true,
separatorBuilder: (index) => const SizedBox(width: 8),
hapticFeedbackType: HapticFeedbackType.lightImpact,
validator: (value) {
Expand Down
3 changes: 0 additions & 3 deletions example/lib/demo/pinput_templates/rounded_with_cursor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ class _RoundedWithCustomCursorState extends State<RoundedWithCustomCursor> {
child: Pinput(
controller: pinController,
focusNode: focusNode,
androidSmsAutofillMethod:
AndroidSmsAutofillMethod.smsUserConsentApi,
listenForMultipleSmsOnAndroid: true,
defaultPinTheme: defaultPinTheme,
validator: (value) {
return value == '2222' ? null : 'Pin is incorrect';
Expand Down
37 changes: 34 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:pinput/pinput.dart';
import 'package:smart_auth/smart_auth.dart';

void main() {
runApp(
Expand All @@ -18,6 +19,7 @@ void main() {
),
body: const FractionallySizedBox(
widthFactor: 1,

/// You can also checkout the [PinputBuilderExample]
child: PinputExample(),
),
Expand All @@ -26,6 +28,29 @@ void main() {
);
}

class SmsRetrieverImpl implements SmsRetriever {
const SmsRetrieverImpl(this.smartAuth);

final SmartAuth smartAuth;

@override
Future<void> removeSmsListener() {
return smartAuth.removeSmsListener();
}

@override
Future<String?> getSmsCode() async {
final res = await smartAuth.getSmsCode();
if (res.succeed && res.codeFound) {
return res.code!;
}
return null;
}

@override
bool get listenForMultipleSms => false;
}

/// This is the basic usage of Pinput
/// For more examples check out the demo directory
class PinputExample extends StatefulWidget {
Expand Down Expand Up @@ -66,6 +91,12 @@ class _PinputExampleState extends State<PinputExample> {
),
);

Pinput(
smsRetriever: SmsRetrieverImpl(
SmartAuth(),
),
);

/// Optionally you can use form to validate the Pinput
return Form(
key: formKey,
Expand All @@ -76,11 +107,11 @@ class _PinputExampleState extends State<PinputExample> {
// Specify direction if desired
textDirection: TextDirection.ltr,
child: Pinput(
smsRetriever: SmsRetrieverImpl(
SmartAuth(),
),
controller: pinController,
focusNode: focusNode,
androidSmsAutofillMethod:
AndroidSmsAutofillMethod.smsUserConsentApi,
listenForMultipleSmsOnAndroid: true,
defaultPinTheme: defaultPinTheme,
separatorBuilder: (index) => const SizedBox(width: 8),
validator: (value) {
Expand Down
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies:

pinput:
path: ../
smart_auth: 2.0.0
google_fonts: ^6.2.1
cupertino_icons: ^1.0.6

Expand Down
2 changes: 0 additions & 2 deletions lib/pinput.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
library pinput;

export 'src/pinput.dart';

export 'package:smart_auth/smart_auth.dart';
48 changes: 9 additions & 39 deletions lib/src/pinput.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:pinput/pinput.dart';
import 'package:smart_auth/smart_auth.dart';
import 'package:universal_platform/universal_platform.dart';

part 'pinput_state.dart';

Expand Down Expand Up @@ -52,6 +49,7 @@ class Pinput extends StatefulWidget {
/// Creates a PinPut widget
const Pinput({
this.length = PinputConstants._defaultLength,
this.smsRetriever,
this.defaultPinTheme,
this.focusedPinTheme,
this.submittedPinTheme,
Expand All @@ -68,10 +66,6 @@ class Pinput extends StatefulWidget {
this.focusNode,
this.preFilledWidget,
this.separatorBuilder,
this.smsCodeMatcher = PinputConstants.defaultSmsCodeMatcher,
this.senderPhoneNumber,
this.androidSmsAutofillMethod = AndroidSmsAutofillMethod.none,
this.listenForMultipleSmsOnAndroid = false,
this.mainAxisAlignment = MainAxisAlignment.center,
this.crossAxisAlignment = CrossAxisAlignment.start,
this.pinContentAlignment = Alignment.center,
Expand All @@ -97,7 +91,9 @@ class Pinput extends StatefulWidget {
this.keyboardAppearance,
this.inputFormatters = const [],
this.textInputAction,
this.autofillHints,
this.autofillHints = const [
AutofillHints.oneTimeCode,
],
this.obscuringCharacter = '•',
this.obscuringWidget,
this.selectionControls,
Expand Down Expand Up @@ -127,6 +123,7 @@ class Pinput extends StatefulWidget {
/// This gives you full control over the pin item widget
Pinput.builder({
required PinItemWidgetBuilder builder,
this.smsRetriever,
this.length = PinputConstants._defaultLength,
this.onChanged,
this.onCompleted,
Expand All @@ -137,10 +134,6 @@ class Pinput extends StatefulWidget {
this.controller,
this.focusNode,
this.separatorBuilder,
this.smsCodeMatcher = PinputConstants.defaultSmsCodeMatcher,
this.senderPhoneNumber,
this.androidSmsAutofillMethod = AndroidSmsAutofillMethod.none,
this.listenForMultipleSmsOnAndroid = false,
this.mainAxisAlignment = MainAxisAlignment.center,
this.crossAxisAlignment = CrossAxisAlignment.start,
this.enabled = true,
Expand Down Expand Up @@ -224,23 +217,10 @@ class Pinput extends StatefulWidget {
/// Displayed fields count. PIN code length.
final int length;

/// By default Android autofill is Disabled, you cane enable it by using any of options listed below
///
/// First option is [AndroidSmsAutofillMethod.smsRetrieverApi] it automatically reads sms without user interaction
/// More about Sms Retriever API https://developers.google.com/identity/sms-retriever/overview?hl=en
///
/// Second option requires user interaction to confirm reading a SMS, See readme for more details
/// [AndroidSmsAutofillMethod.smsUserConsentApi]
/// More about SMS User Consent API https://developers.google.com/identity/sms-retriever/user-consent/overview
final AndroidSmsAutofillMethod androidSmsAutofillMethod;

/// If true [androidSmsAutofillMethod] is not [AndroidSmsAutofillMethod.none]
/// Pinput will listen multiple sms codes, helpful if user request another sms code
final bool listenForMultipleSmsOnAndroid;

/// Used to extract code from SMS for Android Autofill if [androidSmsAutofillMethod] is enabled
/// By default it is [PinputConstants.defaultSmsCodeMatcher]
final String smsCodeMatcher;
/// By default Android autofill is Disabled, you can enable it by passing [smsRetriever]
/// SmsRetriever exposes methods to listen for incoming SMS and extract code from it
/// Recommended package to get sms code on Android is smart_auth https://pub.dev/packages/smart_auth
final SmsRetriever? smsRetriever;

/// Fires when user completes pin input
final ValueChanged<String>? onCompleted;
Expand Down Expand Up @@ -429,9 +409,6 @@ class Pinput extends StatefulWidget {
/// This value controls how far from the edges of a [Scrollable] the TextField will be positioned after the scroll.
final EdgeInsets scrollPadding;

/// Optional parameter for Android SMS User Consent API.
final String? senderPhoneNumber;

/// {@macro flutter.widgets.EditableText.contextMenuBuilder}
///
/// If not provided, will build a default menu based on the platform.
Expand Down Expand Up @@ -810,13 +787,6 @@ class Pinput extends StatefulWidget {
defaultValue: HapticFeedbackType.disabled,
),
);
properties.add(
DiagnosticsProperty<String?>(
'senderPhoneNumber',
senderPhoneNumber,
defaultValue: null,
),
);
properties.add(
DiagnosticsProperty<EditableTextContextMenuBuilder?>(
'contextMenuBuilder',
Expand Down
43 changes: 16 additions & 27 deletions lib/src/pinput_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ part of 'pinput.dart';
/// to support older versions of the API as well.
T? _ambiguate<T>(T? value) => value;

abstract class SmsRetriever {
bool get listenForMultipleSms;

Future<String?> getSmsCode();

Future<void> removeSmsListener();
}

class _PinputState extends State<Pinput>
with RestorationMixin, WidgetsBindingObserver, _PinputUtilsMixin
implements TextSelectionGestureDetectorBuilderDelegate, AutofillClient {
Expand All @@ -33,7 +41,7 @@ class _PinputState extends State<Pinput>
FocusNode? _focusNode;
bool _isHovering = false;
String? _validatorErrorText;
SmartAuth? _smartAuth;
SmsRetriever? _smsRetriever;

String? get _errorText => widget.errorText ?? _validatorErrorText;

Expand Down Expand Up @@ -92,38 +100,19 @@ class _PinputState extends State<Pinput>

/// Android Autofill
void _maybeInitSmartAuth() async {
final isAndroid = UniversalPlatform.isAndroid;
final isAutofillEnabled =
widget.androidSmsAutofillMethod != AndroidSmsAutofillMethod.none;

if (isAndroid && isAutofillEnabled) {
_smartAuth = SmartAuth();
_maybePrintAppSignature();
if (_smsRetriever == null) {
_smsRetriever = widget.smsRetriever!;
_listenForSmsCode();
}
}

void _maybePrintAppSignature() async {
if (widget.androidSmsAutofillMethod ==
AndroidSmsAutofillMethod.smsRetrieverApi) {
final res = await _smartAuth!.getAppSignature();
debugPrint('Pinput: App Signature for SMS Retriever API Is: $res');
}
}

void _listenForSmsCode() async {
final useUserConsentApi = widget.androidSmsAutofillMethod ==
AndroidSmsAutofillMethod.smsUserConsentApi;
final res = await _smartAuth!.getSmsCode(
useUserConsentApi: useUserConsentApi,
matcher: widget.smsCodeMatcher,
senderPhoneNumber: widget.senderPhoneNumber,
);
if (res.succeed && res.codeFound && res.code!.length == widget.length) {
_effectiveController.setText(res.code!);
final res = await _smsRetriever!.getSmsCode();
if (res != null && res.length == widget.length) {
_effectiveController.setText(res);
}
// Listen for multiple sms codes
if (widget.listenForMultipleSmsOnAndroid) {
if (_smsRetriever!.listenForMultipleSms) {
_listenForSmsCode();
}
}
Expand Down Expand Up @@ -212,7 +201,7 @@ class _PinputState extends State<Pinput>
widget.controller?.removeListener(_handleTextEditingControllerChanges);
_focusNode?.dispose();
_controller?.dispose();
_smartAuth?.removeSmsListener();
_smsRetriever?.removeSmsListener();
// https://github.com/Tkko/Flutter_Pinput/issues/89
_ambiguate(WidgetsBinding.instance)!.removeObserver(this);
super.dispose();
Expand Down
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: pinput
version: 4.0.0
version: 5.0.0
description: Pin code input (OTP) text field, iOS SMS autofill, Android SMS autofill One Time Code, Password, Passcode, Captcha, Security, Coupon, Wowcher, 2FA, Two step verification
homepage: https://github.com/Tkko/Flutter_PinPut
repository: https://github.com/Tkko/Flutter_PinPut
Expand All @@ -26,7 +26,6 @@ environment:
dependencies:
flutter:
sdk: flutter
smart_auth: ^2.0.0
universal_platform: ^1.0.0+1

dev_dependencies:
Expand Down

0 comments on commit 1bedba6

Please sign in to comment.