|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:otp_text_field/otp_field.dart'; |
| 5 | +import 'package:otp_text_field/otp_field_style.dart'; |
| 6 | +import 'package:otp_text_field/style.dart'; |
| 7 | + |
| 8 | +class PhoneAuth extends StatefulWidget { |
| 9 | + PhoneAuth({Key key}) : super(key: key); |
| 10 | + |
| 11 | + @override |
| 12 | + _PhoneAuthState createState() => _PhoneAuthState(); |
| 13 | +} |
| 14 | + |
| 15 | +class _PhoneAuthState extends State<PhoneAuth> { |
| 16 | + TextEditingController _phneController = TextEditingController(); |
| 17 | + // Timer _timer; |
| 18 | + int _start = 30; |
| 19 | + String buttonName = "Send"; |
| 20 | + bool wait = false; |
| 21 | + |
| 22 | + @override |
| 23 | + Widget build(BuildContext context) { |
| 24 | + return Scaffold( |
| 25 | + backgroundColor: Colors.black87, |
| 26 | + appBar: AppBar( |
| 27 | + backgroundColor: Colors.black87, |
| 28 | + centerTitle: true, |
| 29 | + title: Text( |
| 30 | + "SignUp", |
| 31 | + style: TextStyle(color: Colors.white, fontSize: 24), |
| 32 | + ), |
| 33 | + ), |
| 34 | + body: SingleChildScrollView( |
| 35 | + child: Container( |
| 36 | + height: MediaQuery.of(context).size.height, |
| 37 | + width: MediaQuery.of(context).size.width, |
| 38 | + child: Column( |
| 39 | + children: [ |
| 40 | + SizedBox( |
| 41 | + height: 150, |
| 42 | + ), |
| 43 | + textItem("Enter your phone Number", _phneController, false), |
| 44 | + SizedBox( |
| 45 | + height: 25, |
| 46 | + ), |
| 47 | + subdivider( |
| 48 | + "Enter your 6 digit otp ", |
| 49 | + ), |
| 50 | + SizedBox( |
| 51 | + height: 30, |
| 52 | + ), |
| 53 | + otp(), |
| 54 | + SizedBox( |
| 55 | + height: 40, |
| 56 | + ), |
| 57 | + RichText( |
| 58 | + text: TextSpan(children: [ |
| 59 | + TextSpan( |
| 60 | + text: "Send OTP again in ", |
| 61 | + style: TextStyle( |
| 62 | + fontSize: 16, |
| 63 | + color: Colors.yellowAccent, |
| 64 | + ), |
| 65 | + ), |
| 66 | + TextSpan( |
| 67 | + text: " 00:$_start ", |
| 68 | + style: TextStyle( |
| 69 | + fontSize: 16, |
| 70 | + color: Colors.pinkAccent, |
| 71 | + ), |
| 72 | + ), |
| 73 | + TextSpan( |
| 74 | + text: "sec ", |
| 75 | + style: TextStyle( |
| 76 | + fontSize: 16, |
| 77 | + color: Colors.yellowAccent, |
| 78 | + ), |
| 79 | + ), |
| 80 | + ]), |
| 81 | + ), |
| 82 | + Expanded(child: Container()), |
| 83 | + Container( |
| 84 | + height: 60, |
| 85 | + width: MediaQuery.of(context).size.width - 60, |
| 86 | + decoration: BoxDecoration( |
| 87 | + color: Color(0xffff9601), |
| 88 | + borderRadius: BorderRadius.circular(15), |
| 89 | + ), |
| 90 | + child: Center( |
| 91 | + child: Text( |
| 92 | + "Let's Go", |
| 93 | + style: TextStyle( |
| 94 | + color: Color(0xfffbe2ae), |
| 95 | + fontSize: 19, |
| 96 | + fontWeight: FontWeight.w700, |
| 97 | + ), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ), |
| 101 | + SizedBox( |
| 102 | + height: 150, |
| 103 | + ), |
| 104 | + ], |
| 105 | + ), |
| 106 | + ), |
| 107 | + ), |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + Widget subdivider(String text) { |
| 112 | + return Container( |
| 113 | + width: MediaQuery.of(context).size.width - 30, |
| 114 | + child: Row( |
| 115 | + children: [ |
| 116 | + Expanded( |
| 117 | + child: Container( |
| 118 | + height: 1, |
| 119 | + margin: EdgeInsets.symmetric(horizontal: 12), |
| 120 | + color: Colors.grey, |
| 121 | + ), |
| 122 | + ), |
| 123 | + Text( |
| 124 | + text, |
| 125 | + style: TextStyle(color: Colors.white, fontSize: 15), |
| 126 | + ), |
| 127 | + Expanded( |
| 128 | + child: Container( |
| 129 | + height: 1, |
| 130 | + margin: EdgeInsets.symmetric(horizontal: 12), |
| 131 | + color: Colors.grey, |
| 132 | + ), |
| 133 | + ), |
| 134 | + ], |
| 135 | + ), |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + Widget otp() { |
| 140 | + return OTPTextField( |
| 141 | + length: 6, |
| 142 | + width: MediaQuery.of(context).size.width - 34, |
| 143 | + fieldWidth: 58, |
| 144 | + otpFieldStyle: OtpFieldStyle( |
| 145 | + backgroundColor: Color(0xff1d1d1d), |
| 146 | + borderColor: Colors.white, |
| 147 | + ), |
| 148 | + style: TextStyle(fontSize: 17, color: Colors.white), |
| 149 | + textFieldAlignment: MainAxisAlignment.spaceAround, |
| 150 | + fieldStyle: FieldStyle.underline, |
| 151 | + onCompleted: (pin) { |
| 152 | + print("Completed: " + pin); |
| 153 | + }, |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + Widget textItem( |
| 158 | + String labeltext, TextEditingController controller, bool obscureText) { |
| 159 | + return Container( |
| 160 | + width: MediaQuery.of(context).size.width - 40, |
| 161 | + decoration: BoxDecoration( |
| 162 | + color: Color(0xff1d1d1d), |
| 163 | + borderRadius: BorderRadius.circular(15), |
| 164 | + ), |
| 165 | + height: 60, |
| 166 | + child: TextFormField( |
| 167 | + controller: controller, |
| 168 | + obscureText: obscureText, |
| 169 | + keyboardType: TextInputType.number, |
| 170 | + style: TextStyle( |
| 171 | + fontSize: 17, |
| 172 | + color: Colors.white, |
| 173 | + ), |
| 174 | + decoration: InputDecoration( |
| 175 | + border: InputBorder.none, |
| 176 | + hintText: labeltext, |
| 177 | + hintStyle: TextStyle( |
| 178 | + fontSize: 17, |
| 179 | + color: Colors.white54, |
| 180 | + ), |
| 181 | + contentPadding: EdgeInsets.symmetric(vertical: 19, horizontal: 8), |
| 182 | + prefixIcon: Padding( |
| 183 | + padding: const EdgeInsets.symmetric(vertical: 14, horizontal: 15), |
| 184 | + child: Text( |
| 185 | + "(+91)", |
| 186 | + style: TextStyle( |
| 187 | + fontSize: 17, |
| 188 | + color: Colors.white, |
| 189 | + ), |
| 190 | + ), |
| 191 | + ), |
| 192 | + suffixIcon: InkWell( |
| 193 | + onTap: wait |
| 194 | + ? null |
| 195 | + : () { |
| 196 | + setState(() { |
| 197 | + _start = 30; |
| 198 | + buttonName = "Resnd"; |
| 199 | + wait = true; |
| 200 | + }); |
| 201 | + startTimer(); |
| 202 | + }, |
| 203 | + child: Padding( |
| 204 | + padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 20), |
| 205 | + child: Text( |
| 206 | + buttonName, |
| 207 | + style: TextStyle( |
| 208 | + fontSize: 17, |
| 209 | + fontWeight: FontWeight.bold, |
| 210 | + color: wait ? Colors.grey : Colors.white, |
| 211 | + ), |
| 212 | + ), |
| 213 | + ), |
| 214 | + ), |
| 215 | + ), |
| 216 | + ), |
| 217 | + ); |
| 218 | + } |
| 219 | + |
| 220 | + void startTimer() { |
| 221 | + const oneSec = const Duration(seconds: 1); |
| 222 | + Timer _timer = new Timer.periodic( |
| 223 | + oneSec, |
| 224 | + (Timer timer) { |
| 225 | + if (_start == 0) { |
| 226 | + setState(() { |
| 227 | + timer.cancel(); |
| 228 | + wait = false; |
| 229 | + }); |
| 230 | + } else { |
| 231 | + setState(() { |
| 232 | + _start--; |
| 233 | + }); |
| 234 | + } |
| 235 | + }, |
| 236 | + ); |
| 237 | + } |
| 238 | +} |
0 commit comments