log2

PHOTO EMBED

Mon Jul 15 2024 18:31:53 GMT+0000 (Coordinated Universal Time)

Saved by @mehran

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:ghalam_jadoee/component/api_service.dart';
import 'package:ghalam_jadoee/component/body2.dart';
import 'package:ghalam_jadoee/pages/bottomnav.dart';
import 'package:ghalam_jadoee/pages/sign.dart';
import 'package:ghalam_jadoee/pages/moarf.dart';
import 'package:shared_preferences/shared_preferences.dart';

String SendPhone2 = "";
String OTP2 = "";
String userStatus = "";
bool BtnEnable = true;

void main() {
  runApp(Login2());
}

class Login2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: ' ',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: OtpRequestScreen(
        name: '',
        phone: '',
      ),
    );
  }
}

class OtpRequestScreen extends StatefulWidget {
  final String name;
  final String phone;

  OtpRequestScreen({required this.name, required this.phone});

  @override
  _OtpRequestScreenState createState() => _OtpRequestScreenState();
}

class _OtpRequestScreenState extends State<OtpRequestScreen> {
  final List<FocusNode> _focusNodes = List.generate(4, (_) => FocusNode());
  final List<TextEditingController> _controllers =
      List.generate(4, (_) => TextEditingController());

  Timer? _timer;
  int _start = 300;
  bool _isResendDisabled = true;

  @override
  void initState() {
    super.initState();
    _startTimer();
    setState(() {
      SendPhone2 = SendPhone;
      OTP2 = OTP1;
      userStatus = userStatus2;
    });
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  Future<void> saveLogin(String nameUser) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString("userkey", nameUser);
    await prefs.setBool("loggedIn", true);
  }

  void _startTimer() {
    const oneSec = Duration(seconds: 1);
    _timer?.cancel();
    _timer = Timer.periodic(oneSec, (Timer timer) {
      if (_start == 0) {
        setState(() {
          timer.cancel();
          BtnEnable = false;
          _isResendDisabled = false;
        });
      } else {
        setState(() {
          _start--;
        });
      }
    });
  }

  Future<void> _resetTimer() async {
    String state = 's2';
    var jsonResponse = await ApiService.login(SendPhone2, state);

    if (jsonResponse != null) {
      var userStatus = jsonResponse['USER STATUS'];
      var otp = jsonResponse['otp'];
      var rs = jsonResponse['rs'];

      OTP2 = otp;

      print("otp");
      print(otp);
    }

    setState(() {
      _start = 300;
      BtnEnable = false;
      _isResendDisabled = true;
      _startTimer();
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        body: Stack(
          alignment: Alignment.center,
          children: [
            const Body2(),
            Center(
              child: Padding(
                padding: EdgeInsets.only(
                    top: MediaQuery.of(context).size.height * 0.2),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    const Text(
                      'پیامک تایید شماره تلفن',
                      style: TextStyle(
                        fontSize: 25,
                      ),
                    ),
                    SizedBox(height: 20),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text(
                          SendPhone2,
                          style: const TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        const Text(
                          '  :کد ارسال شده به ',
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 20),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: List.generate(4, (index) {
                        return OtpInput(
                          focusNode: _focusNodes[index],
                          nextFocusNode:
                              index < 3 ? _focusNodes[index + 1] : null,
                          previousFocusNode:
                              index > 0 ? _focusNodes[index - 1] : null,
                          controller: _controllers[index],
                        );
                      }),
                    ),
                    SizedBox(height: 20),
                    Align(
                      alignment: Alignment.centerRight,
                      child: TextButton(
                        onPressed: _isResendDisabled ? null : _resetTimer,
                        child: Text(
                          _isResendDisabled
                              ? 'ارسال مجدد کد در $_start ثانیه'
                              : 'ارسال مجدد کد',
                          style: TextStyle(
                            color: _isResendDisabled
                                ? const Color.fromRGBO(158, 158, 158, 1)
                                : Colors.black,
                          ),
                        ),
                      ),
                    ),
                    SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: BtnEnable
                          ? () async {
                              String enteredOtp = _controllers
                                  .map((controller) => controller.text)
                                  .join('');
                              print(OTP2);

                              print(enteredOtp);
                              print("SOTP");
                              //print(Sotp);
                              if (enteredOtp == OTP2) {
                                if (userStatus == '0') {
                                  Navigator.pushReplacement(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => Moarf()),
                                  );
                                } else if (userStatus == '1') {
                                  await saveLogin(SendPhone2);
                                  Navigator.pushReplacement(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => Bottomnav()),
                                  );
                                }
                              } else {
                                ScaffoldMessenger.of(context).showSnackBar(
                                  SnackBar(
                                    content: Directionality(
                                      textDirection: TextDirection.rtl,
                                      child: Text('کد وارد شده صحیح نیست'),
                                    ),
                                  ),
                                );
                              }
                            }
                          : null,
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Color.fromRGBO(112, 27, 248, 100),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(100),
                        ),
                        padding:
                            EdgeInsets.symmetric(horizontal: 30, vertical: 12),
                      ),
                      child: const Text(
                        'تایید',
                        style: TextStyle(
                          color: Colors.black,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
      onWillPop: () async {
        return false;
      },
    );
  }
}

class OtpInput extends StatelessWidget {
  final FocusNode focusNode;
  final FocusNode? nextFocusNode;
  final FocusNode? previousFocusNode;
  final TextEditingController controller;

  const OtpInput({
    required this.focusNode,
    this.nextFocusNode,
    this.previousFocusNode,
    required this.controller,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 50,
      height: 50,
      decoration: BoxDecoration(
        color: Color.fromARGB(159, 226, 221, 221),
        borderRadius: BorderRadius.circular(10),
      ),
      child: TextField(
        focusNode: focusNode,
        controller: controller,
        textAlign: TextAlign.center,
        style: const TextStyle(
          fontSize: 24,
        ),
        keyboardType: TextInputType.number,
        maxLength: 1,
        decoration: const InputDecoration(
          counterText: '',
          border: InputBorder.none,
        ),
        onChanged: (value) {
          if (value.isNotEmpty && nextFocusNode != null) {
            FocusScope.of(context).requestFocus(nextFocusNode);
          } else if (value.isEmpty && previousFocusNode != null) {
            FocusScope.of(context).requestFocus(previousFocusNode);
          } else {
            focusNode.unfocus();
          }
        },
      ),
    );
  }
}
content_copyCOPY