-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.dart
149 lines (135 loc) · 4.47 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:smart_auth/smart_auth.dart';
void main() {
runApp(const MyApp());
}
/// This example demonstrates how to use the SmartAuth plugin.
class MyApp extends StatefulWidget {
/// Creates a new instance of the [MyApp] class.
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final smartAuth = SmartAuth.instance;
final pinputController = TextEditingController();
String? appSignature;
@override
void dispose() {
/// Removes the listeners if the SMS code is not received yet
smartAuth.removeSmsRetrieverApiListener();
/// Removes the listeners if the SMS code is not received yet
smartAuth.removeUserConsentApiListener();
pinputController.dispose();
super.dispose();
}
Future<void> getAppSignature() async {
final res = await smartAuth.getAppSignature();
setState(() => appSignature = res.data);
debugPrint('Signature: $res');
}
Future<void> userConsent() async {
final res = await smartAuth.getSmsWithUserConsentApi();
if (res.hasData) {
debugPrint('userConsent: $res');
final code = res.requireData.code;
/// The code can be null if the SMS was received but
/// the code was not extracted from it
if (code == null) return;
pinputController.text = code;
pinputController.selection = TextSelection.fromPosition(
TextPosition(offset: pinputController.text.length),
);
} else if (res.isCanceled) {
debugPrint('userConsent canceled');
} else {
debugPrint('userConsent failed: $res');
}
}
Future<void> smsRetriever() async {
final res = await smartAuth.getSmsWithRetrieverApi();
if (res.hasData) {
debugPrint('smsRetriever: $res');
final code = res.requireData.code;
/// The code can be null if the SMS was received but
/// the code was not extracted from it
if (code == null) return;
pinputController.text = code;
pinputController.selection = TextSelection.fromPosition(
TextPosition(offset: pinputController.text.length),
);
} else {
debugPrint('smsRetriever failed: $res');
}
}
Future<void> requestPhoneNumberHint() async {
final res = await smartAuth.requestPhoneNumberHint();
if (res.hasData) {
// Use the phone number
} else {
// Handle error
}
debugPrint('requestHint: $res');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: const Locale('es', ''),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', ''),
Locale('es', ''),
],
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.all(8),
child: TextField(
controller: pinputController,
decoration: const InputDecoration(
hintText: 'Code',
border: OutlineInputBorder(),
),
),
),
TextButton(
onPressed: userConsent,
child: const Text('Sms User Consent API'),
),
TextButton(
onPressed: smsRetriever,
child: const Text('Sms Retriever API'),
),
TextButton(
onPressed: getAppSignature,
child: const Text('Get App Signature'),
),
if (appSignature != null)
SelectableText(
'App Signature: $appSignature',
textAlign: TextAlign.center,
),
TextButton(
onPressed: requestPhoneNumberHint,
child: const Text('Request Phone Number Hint'),
),
],
),
),
),
),
);
}
}