|
| 1 | +import 'package:flutter/foundation.dart'; |
1 | 2 | import 'package:flutter/material.dart'; |
2 | 3 | import 'package:flutter/services.dart'; |
3 | 4 | import 'package:flutter_animated_login/flutter_animated_login.dart'; |
4 | 5 |
|
5 | 6 | void main() { |
6 | | - runApp(const MyApp()); |
| 7 | + runApp( |
| 8 | + const MaterialApp( |
| 9 | + title: 'Flutter Animated Login', |
| 10 | + debugShowCheckedModeBanner: false, |
| 11 | + home: MyApp(), |
| 12 | + ), |
| 13 | + ); |
7 | 14 | } |
8 | 15 |
|
9 | | -ValueNotifier<bool> isDark = ValueNotifier<bool>(true); |
| 16 | +ValueNotifier<bool> loginTypeNotifier = ValueNotifier<bool>(true); |
10 | 17 |
|
11 | 18 | class MyApp extends StatelessWidget { |
12 | 19 | const MyApp({super.key}); |
13 | 20 |
|
| 21 | + @override |
| 22 | + Widget build(BuildContext context) { |
| 23 | + return Scaffold( |
| 24 | + body: Center( |
| 25 | + child: ElevatedButton( |
| 26 | + onPressed: () { |
| 27 | + Navigator.push( |
| 28 | + context, |
| 29 | + MaterialPageRoute( |
| 30 | + builder: (context) => const LoginDemoApp(), |
| 31 | + ), |
| 32 | + ); |
| 33 | + }, |
| 34 | + child: const Text("Login"), |
| 35 | + ), |
| 36 | + ), |
| 37 | + ); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +class LoginDemoApp extends StatefulWidget { |
| 42 | + const LoginDemoApp({super.key}); |
| 43 | + |
| 44 | + @override |
| 45 | + State<LoginDemoApp> createState() => _LoginDemoAppState(); |
| 46 | +} |
| 47 | + |
| 48 | +class _LoginDemoAppState extends State<LoginDemoApp> { |
| 49 | + late TextFieldController _controller; |
| 50 | + late TextFieldController _otpController; |
| 51 | + |
| 52 | + @override |
| 53 | + void initState() { |
| 54 | + _controller = TextFieldController(); |
| 55 | + _otpController = TextFieldController(); |
| 56 | + if (kDebugMode) { |
| 57 | + _controller.text = '7012345678'; |
| 58 | + _otpController.text = '123452'; |
| 59 | + } |
| 60 | + super.initState(); |
| 61 | + } |
| 62 | + |
| 63 | + @override |
| 64 | + void dispose() { |
| 65 | + _controller.isDisposed ? null : _controller.dispose(); |
| 66 | + super.dispose(); |
| 67 | + } |
| 68 | + |
14 | 69 | @override |
15 | 70 | Widget build(BuildContext context) { |
16 | 71 | return ValueListenableBuilder( |
17 | | - valueListenable: isDark, |
18 | | - builder: (context, value, child) => MaterialApp( |
19 | | - title: 'Flutter Animated Login', |
20 | | - themeMode: value ? ThemeMode.dark : ThemeMode.light, |
21 | | - theme: value ? ThemeData.dark() : ThemeData.dark(), |
22 | | - debugShowCheckedModeBanner: false, |
23 | | - home: Scaffold( |
24 | | - floatingActionButton: FilledButton( |
25 | | - onPressed: () => isDark.value = !isDark.value, |
26 | | - child: value |
27 | | - ? const Text("Login with OTP") |
28 | | - : const Text("Login with Password"), |
29 | | - ), |
30 | | - body: FlutterAnimatedLogin( |
31 | | - loginType: value ? LoginType.password : LoginType.otp, |
32 | | - onLogin: (loginData) async { |
| 72 | + valueListenable: loginTypeNotifier, |
| 73 | + builder: (context, value, child) => Scaffold( |
| 74 | + floatingActionButton: FilledButton( |
| 75 | + onPressed: () { |
| 76 | + loginTypeNotifier.value = !loginTypeNotifier.value; |
| 77 | + }, |
| 78 | + child: value |
| 79 | + ? const Text("Login with OTP") |
| 80 | + : const Text("Login with Password"), |
| 81 | + ), |
| 82 | + body: FlutterAnimatedLogin( |
| 83 | + loginType: value ? LoginType.password : LoginType.otp, |
| 84 | + onLogin: (loginData) async { |
| 85 | + if (loginData.name == '+917012345678') { |
| 86 | + final result = await Future.delayed( |
| 87 | + const Duration(seconds: 2), |
| 88 | + () => '', |
| 89 | + ); |
| 90 | + SystemChannels.textInput.invokeMethod('TextInput.hide'); |
| 91 | + TextInput.finishAutofillContext(); |
| 92 | + return result; |
| 93 | + } else { |
| 94 | + return 'Invalid phone number, please try again.'; |
| 95 | + } |
| 96 | + }, |
| 97 | + onVerify: (otp) async { |
| 98 | + if (otp.secret == '123456') { |
33 | 99 | final result = await Future.delayed( |
34 | 100 | const Duration(seconds: 2), |
35 | 101 | () => '', |
36 | 102 | ); |
37 | 103 | SystemChannels.textInput.invokeMethod('TextInput.hide'); |
38 | 104 | TextInput.finishAutofillContext(); |
39 | 105 | return result; |
40 | | - }, |
41 | | - loginConfig: const LoginConfig( |
42 | | - logo: FlutterLogo(size: 100), |
43 | | - title: 'Mohesu Enterprises', |
44 | | - subtitle: 'Let\'s Sign In', |
| 106 | + } else { |
| 107 | + return 'Invalid OTP, please try again.'; |
| 108 | + } |
| 109 | + }, |
| 110 | + loginConfig: LoginConfig( |
| 111 | + title: 'Mohesu Enterprises', |
| 112 | + subtitle: 'Let\'s Sign In', |
| 113 | + textFiledConfig: EmailPhoneTextFiledConfig( |
| 114 | + controller: _controller, |
45 | 115 | ), |
46 | | - providers: [ |
47 | | - LoginProvider( |
48 | | - icon: Icons.reddit, |
49 | | - label: 'Reddit', |
50 | | - callback: () async { |
51 | | - return ""; |
52 | | - }, |
53 | | - ), |
54 | | - LoginProvider( |
55 | | - icon: Icons.apple, |
56 | | - label: 'Apple', |
57 | | - callback: () async { |
58 | | - return ""; |
59 | | - }, |
60 | | - ), |
61 | | - LoginProvider( |
62 | | - icon: Icons.facebook, |
63 | | - label: 'Facebook', |
64 | | - callback: () async { |
65 | | - return ""; |
66 | | - }, |
67 | | - ), |
68 | | - ], |
69 | 116 | ), |
| 117 | + verifyConfig: VerifyConfig( |
| 118 | + textFiledConfig: OtpTextFiledConfig( |
| 119 | + controller: _otpController, |
| 120 | + ), |
| 121 | + ), |
| 122 | + providers: [ |
| 123 | + LoginProvider( |
| 124 | + icon: Icons.reddit, |
| 125 | + label: const Text('Reddit'), |
| 126 | + callback: () async { |
| 127 | + await Future.delayed(const Duration(seconds: 3), () { |
| 128 | + print('Reddit login'); |
| 129 | + }); |
| 130 | + return ""; |
| 131 | + }, |
| 132 | + ), |
| 133 | + LoginProvider( |
| 134 | + icon: Icons.apple, |
| 135 | + label: const Text('Apple'), |
| 136 | + callback: () async { |
| 137 | + return ""; |
| 138 | + }, |
| 139 | + ), |
| 140 | + LoginProvider( |
| 141 | + icon: Icons.facebook, |
| 142 | + label: const Text('Facebook'), |
| 143 | + callback: () async { |
| 144 | + return ""; |
| 145 | + }, |
| 146 | + ), |
| 147 | + ], |
70 | 148 | ), |
71 | 149 | ), |
72 | 150 | ); |
|
0 commit comments