Flutter tdlib integration with widgets

72 views Asked by At

I need to integrate tdlib authentication with Flutter widgets such as TextFormField to get phone_number or verification code from user. I used to this code to authenticate with input data using stdin.readLineSync()!;

Can you help me with this problem

import 'package:tdlib/td_api.dart' as tda;
import 'package:tdlib/td_client.dart' as tdc;

  Future<tdc.Client> init([BuildContext? context]) async {
  
 

    final client = tdc.Client.create();
    await client.initialize();

    await client.send(const tda.GetCountries());

    client.updates.listen((event) {
      if (event.toJson()['@type'] == "updateAuthorizationState") {
        tda.UpdateAuthorizationState update =
            event as tda.UpdateAuthorizationState;
        tda.AuthorizationState authorizationState = update.authorizationState;
        onAuthStateUpdated(authorizationState, context!);
      }
    });
    return client;
    // Create a new TDLib _client
  }

  void onAuthStateUpdated(
      tda.AuthorizationState authorizationState, BuildContext context) async {
    switch (authorizationState.getConstructor()) {
      case tda.AuthorizationStateWaitTdlibParameters.constructor:
        await setTdlibParametrs();

      case tda.AuthorizationStateWaitPhoneNumber.constructor:
        await Future.delayed(Duration.zero).then((value) =>
            Navigator.pushNamedAndRemoveUntil(
                context, 'auth', (route) => false));

      case tda.SetDatabaseEncryptionKey.constructor:
        showToastMessage("DB ENCRYPT KEY");
      // await _client.send(
      //     tda.SetDatabaseEncryptionKey(newEncryptionKey: dbEncryptionKey));
      case tda.AuthorizationStateWaitOtherDeviceConfirmation.constructor:
        String link = const tda.AuthorizationStateWaitOtherDeviceConfirmation(
                link: "https://")
            .link;
      //print("Please confirm this login link on another device: $link");

      case tda.AuthorizationStateWaitCode.constructor:
        Future.delayed(Duration.zero).then((value) =>
            Navigator.pushNamedAndRemoveUntil(
                context, 'otp', (route) => false));

      case tda.AuthorizationStateWaitRegistration.constructor:
        print("Please enter your first name: ");
        String fName = stdin.readLineSync()!;

        print("Please enter your last name: ");
        String lName = stdin.readLineSync()!;

       // await _client.send(tda.RegisterUser(firstName: fName, lastName: lName));

      case tda.AuthorizationStateWaitPassword.constructor:
        print("Please enter password: ");
        String passworde = stdin.readLineSync()!;
      // await _client
      //     .send(tda.CheckAuthenticationPassword(password: passworde));

      case tda.AuthorizationStateReady.constructor:
        haveAuthorization = true;
        showToastMessage("You are in Telegram Home page");
      // try {
      //   gotAuthorization.signal();
      // } finally {
      //   authorizationLock.unlock();
      // }

      default:
    }
  }

My Splash Screen.

class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    Future.delayed(const Duration(seconds: 2)).then((value) => checkEvent());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: CircleAvatar(
          backgroundImage: AssetImage("assets/telegram_logo.png"),
          radius: 80,
        ),
      ),
    );
  }

  Future<void> checkEvent() async {
     await TDClientService().init(context);
  }
}

I expected, Splash screen firstly opened, and this page find user status. Such as AuthorizationStateReady, AuthorizationStateWaitPassword, AuthorizationStateWaitRegistration.

And navigate user to certain page.

0

There are 0 answers