traderpal_connect 1.0.0-beta.4 copy "traderpal_connect: ^1.0.0-beta.4" to clipboard
traderpal_connect: ^1.0.0-beta.4 copied to clipboard

[BETA] TraderPal Connect SDK for Flutter - A comprehensive SDK for integrating TraderPal services into Flutter applications with JWT authentication and subscription management. This is a beta version [...]

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:traderpal_connect/traderpal_connect.dart';
import 'package:dio/dio.dart';
import 'package:footprint_flutter/footprint_flutter.dart';
import 'connect_api/connect.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Load environment variables
  await dotenv.load(fileName: ".env");

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'TraderPal Connect Example',
      debugShowCheckedModeBanner: false,
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: const Color.fromARGB(255, 18, 32, 47),
      ),
      home: const ExampleHomePage(),
    );
  }
}

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

  @override
  State<ExampleHomePage> createState() => _ExampleHomePageState();
}

class _ExampleHomePageState extends State<ExampleHomePage> {
  bool _isLoadingOnboarding = false;
  final TextEditingController _userIdController = TextEditingController();
  bool _showClearButton = false;

  @override
  void initState() {
    super.initState();
    // Cargar el userId del .env como valor por defecto si existe
    final defaultUserId = dotenv.env['TRADERPAL_USER_ID'] ?? '';
    _userIdController.text = defaultUserId;
    _showClearButton = defaultUserId.isNotEmpty;

    // Listener para mostrar/ocultar el botón de limpiar
    _userIdController.addListener(() {
      setState(() {
        _showClearButton = _userIdController.text.isNotEmpty;
      });
    });
  }

  @override
  void dispose() {
    _userIdController.dispose();
    super.dispose();
  }

  /// Obtiene el session token del endpoint de onboarding
  Future<String?> _fetchOnboardingSessionToken(String userId) async {
    try {
      final token = dotenv.env['TRADERPAL_FOOTPRINT_TOKEN'];
      final mode = dotenv.env['MODE'];
      final urlConnect =
          mode == 'dev' ? dotenv.env['API_URL_DEBUG'] : dotenv.env['API_URL'];
      // 'https://connect-api-sandbox.greenwater-b4a2eb1a.eastus.azurecontainerapps.io/v1/onboarding/$userId/session-token';
      if (token == null || token.isEmpty) {
        return null;
      }

      final dio = Dio();
      final url = '$urlConnect/v1/onboarding/$userId/session-token';

      final response = await dio.post(
        url,
        options: Options(
          headers: {
            'Authorization': 'Bearer $token',
            'Content-Type': 'application/json',
          },
        ),
        data: <String, dynamic>{},
      );

      if (response.statusCode == 200 && response.data != null) {
        if (response.data is String) {
          return response.data as String;
        } else if (response.data is Map<String, dynamic>) {
          final data = response.data as Map<String, dynamic>;
          final token = data['sessionToken'] ??
              data['token'] ??
              data['onboardingSessionToken'] ??
              data['session_token'];

          if (token != null && token is String) {
            return token;
          }
        }
      }
      return null;
    } catch (e) {
      return null;
    }
  }

  /// Inicia el proceso de obtener el session token
  Future<void> _startOnboarding() async {
    if (_isLoadingOnboarding) return;

    setState(() {
      _isLoadingOnboarding = true;
    });

    try {
      final userId = _userIdController.text.trim();
      if (userId.isEmpty) {
        _showError('Por favor ingresa un User ID');
        return;
      }

      final sessionToken = await _fetchOnboardingSessionToken(userId);
      if (sessionToken == null || sessionToken.isEmpty) {
        _showError('No se pudo obtener el session token.');
        return;
      }

      // Inicializar Footprint SDK con el token obtenido
      await footprint.init(
        FootprintConfiguration(
          authToken: sessionToken,
          redirectUrl: "traderpal.co",
          onComplete: (token) {
            if (mounted) {
              //_showSuccess('Onboarding completado exitosamente');
            }
          },
          onCancel: () {
            if (mounted) {
              // Usuario canceló el flujo
            }
          },
        ),
        context,
      );
    } catch (e) {
      _showError('Error: $e');
    } finally {
      if (mounted) {
        setState(() {
          _isLoadingOnboarding = false;
        });
      }
    }
  }

  void _showError(String message) {
    if (!mounted) return;
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text(message), backgroundColor: Colors.red),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('TraderPal SDK Connect'),
        backgroundColor: Colors.black,
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              const Text(
                'Demo de TraderPal Connect',
                style: TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 20),
              const Text(
                'Este es un ejemplo de cómo usar el package traderpal_connect.',
                style: TextStyle(color: Colors.white70),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 30),
              // Campo de texto para el User ID
              TextField(
                controller: _userIdController,
                decoration: InputDecoration(
                  labelText: 'User ID',
                  hintText: 'Ingresa tu User ID',
                  labelStyle: const TextStyle(color: Colors.white70),
                  hintStyle: const TextStyle(color: Colors.white38),
                  filled: true,
                  fillColor: Colors.white.withOpacity(0.1),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                    borderSide: const BorderSide(color: Colors.white30),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                    borderSide: const BorderSide(color: Colors.white30),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                    borderSide: const BorderSide(
                      color: Colors.white70,
                      width: 2,
                    ),
                  ),
                  suffixIcon: _showClearButton
                      ? IconButton(
                          icon: const Icon(Icons.clear, color: Colors.white70),
                          onPressed: () {
                            _userIdController.clear();
                          },
                        )
                      : null,
                ),
                style: const TextStyle(color: Colors.white),
              ),
              const SizedBox(height: 30),
              StreamBuilder<bool>(
                stream: TraderPalSDK.loadingStream,
                initialData: TraderPalSDK.loading,
                builder: (context, snapshot) {
                  final isLoading = snapshot.data ?? false;

                  return SizedBox(
                    width: double.infinity,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        SizedBox(
                          width: double.infinity,
                          child: ElevatedButton(
                            onPressed: (isLoading || _isLoadingOnboarding)
                                ? null
                                : _startOnboarding,
                            style: ElevatedButton.styleFrom(
                              backgroundColor: AppColors.gray11,
                              padding: const EdgeInsets.symmetric(vertical: 16),
                            ),
                            child: (isLoading || _isLoadingOnboarding)
                                ? const SizedBox(
                                    width: 20,
                                    height: 20,
                                    child: CircularProgressIndicator(
                                      strokeWidth: 2,
                                      valueColor: AlwaysStoppedAnimation<Color>(
                                        Colors.white,
                                      ),
                                    ),
                                  )
                                : const Text(
                                    'Onboarding',
                                    style: TextStyle(
                                      fontSize: 16,
                                      color: Colors.white,
                                    ),
                                  ),
                          ),
                        ),
                        const SizedBox(height: 10),
                        SizedBox(
                          width: double.infinity,
                          child: ElevatedButton(
                            onPressed: isLoading
                                ? null
                                : () async {
                                    final userId =
                                        _userIdController.text.trim();
                                    if (userId.isEmpty) {
                                      _showError(
                                        'Por favor ingresa un User ID',
                                      );
                                      return;
                                    }

                                    /* final bool isAccountClosed =
                                        await TraderPalSDK.accountClosed();
                          
                                    if (isAccountClosed) {
                                      debugPrint(
                                        "Account is closed. Cannot initialize SDK.",
                                      );
                                      return;
                                    } */

                                    // Initialize Connect and get token
                                    final connect = Connect();
                                    await connect.startTokenManager();
                                    final token = connect.authToken;

                                    if (token == null || token.isEmpty) {
                                      _showError(
                                        'No se pudo obtener el token de autenticación',
                                      );
                                      return;
                                    }

                                    await TraderPalSDK.initialize(
                                      token: token,
                                      userId: userId,
                                      context: context,
                                      mode: "light", // "dark" o "light"
                                      useSandbox: true,
                                      onCancel: () {
                                        debugPrint(
                                          "onCancel: user canceled the flow",
                                        );
                                      },
                                      onError: (error) {
                                        debugPrint("onError: $error");
                                      },
                                    );
                                  },
                            style: ElevatedButton.styleFrom(
                              backgroundColor: AppColors.purple6,
                              padding: const EdgeInsets.symmetric(vertical: 16),
                            ),
                            child: isLoading
                                ? const SizedBox(
                                    width: 20,
                                    height: 20,
                                    child: CircularProgressIndicator(
                                      strokeWidth: 2,
                                      valueColor: AlwaysStoppedAnimation<Color>(
                                        Colors.white,
                                      ),
                                    ),
                                  )
                                : const Text(
                                    'Abrir SDK TraderPal',
                                    style: TextStyle(
                                      fontSize: 16,
                                      color: Colors.white,
                                    ),
                                  ),
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
130
points
252
downloads

Publisher

unverified uploader

Weekly Downloads

[BETA] TraderPal Connect SDK for Flutter - A comprehensive SDK for integrating TraderPal services into Flutter applications with JWT authentication and subscription management. This is a beta version for early testing and feedback.

Repository (GitHub)
View/report issues

Topics

#sdk #authentication #jwt #subscription #traderpal

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

dio, flutter, flutter_animate, flutter_dotenv, flutter_riverpod, flutter_svg, google_fonts, syncfusion_flutter_charts, table_calendar, url_launcher, webview_flutter

More

Packages that depend on traderpal_connect