generateUnifiedLogin static method

String generateUnifiedLogin(
  1. String projectName
)

Implementation

static String generateUnifiedLogin(String projectName) {
  return '''
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:$projectName/shared/widgets/widgets.dart';

enum AuthMethod { email, username, phone }

class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);

@override
State<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> with SingleTickerProviderStateMixin {
late TabController _tabController;
final _emailFormKey = GlobalKey<FormState>();
final _usernameFormKey = GlobalKey<FormState>();
final _phoneFormKey = GlobalKey<FormState>();

final _emailController = TextEditingController();
final _usernameController = TextEditingController();
final _phoneController = TextEditingController();
final _passwordController = TextEditingController();

bool _isLoading = false;

@override
void initState() {
  super.initState();
  _tabController = TabController(length: 3, vsync: this);
}

@override
void dispose() {
  _tabController.dispose();
  _emailController.dispose();
  _usernameController.dispose();
  _phoneController.dispose();
  _passwordController.dispose();
  super.dispose();
}

Future<void> _handleLogin() async {
  final currentIndex = _tabController.index;
  bool isValid = false;

  switch (currentIndex) {
    case 0:
      isValid = _emailFormKey.currentState!.validate();
      break;
    case 1:
      isValid = _usernameFormKey.currentState!.validate();
      break;
    case 2:
      isValid = _phoneFormKey.currentState!.validate();
      if (isValid) {
        context.push('/otp-verification', extra: _phoneController.text);
        return;
      }
      break;
  }

  if (!isValid) return;

  setState(() => _isLoading = true);
  await Future.delayed(const Duration(seconds: 2));

  if (mounted) {
    setState(() => _isLoading = false);
    context.go('/');
  }
}

Future<void> _handleSocialLogin(SocialAuthProvider provider) async {
  setState(() => _isLoading = true);
  await Future.delayed(const Duration(seconds: 2));

  if (mounted) {
    setState(() => _isLoading = false);
    context.go('/');
  }
}

@override
Widget build(BuildContext context) {
  final theme = Theme.of(context);
  final size = MediaQuery.of(context).size;
  final isTablet = size.width > 600;

  return Scaffold(
    body: SafeArea(
      child: Center(
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(24),
          child: ConstrainedBox(
            constraints: BoxConstraints(maxWidth: isTablet ? 500 : double.infinity),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Icon(Icons.lock_outline, size: 80, color: theme.colorScheme.primary),
                const SizedBox(height: 24),
                Text('Welcome Back', style: theme.textTheme.displaySmall, textAlign: TextAlign.center),
                const SizedBox(height: 8),
                Text('Choose your login method', style: theme.textTheme.bodyMedium, textAlign: TextAlign.center),
                const SizedBox(height: 32),
                TabBar(
                  controller: _tabController,
                  tabs: const [
                    Tab(icon: Icon(Icons.email), text: 'Email'),
                    Tab(icon: Icon(Icons.person), text: 'Username'),
                    Tab(icon: Icon(Icons.phone), text: 'Phone'),
                  ],
                ),
                const SizedBox(height: 24),
                SizedBox(
                  height: 250,
                  child: TabBarView(
                    controller: _tabController,
                    children: [
                      _buildEmailForm(),
                      _buildUsernameForm(),
                      _buildPhoneForm(),
                    ],
                  ),
                ),
                const SizedBox(height: 24),
                PrimaryButton(
                  text: _tabController.index == 2 ? 'Send OTP' : 'Login',
                  isFullWidth: true,
                  isLoading: _isLoading,
                  onPressed: _handleLogin,
                ),
                const SizedBox(height: 16),
                SecondaryButton(
                  text: 'Create Account',
                  isFullWidth: true,
                  onPressed: () => context.push('/signup'),
                ),
                const SizedBox(height: 24),
                Row(
                  children: [
                    const Expanded(child: Divider()),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 16),
                      child: Text('OR', style: theme.textTheme.bodySmall),
                    ),
                    const Expanded(child: Divider()),
                  ],
                ),
                const SizedBox(height: 24),
                SocialAuthButton(
                  provider: SocialAuthProvider.google,
                  onPressed: () => _handleSocialLogin(SocialAuthProvider.google),
                ),
                const SizedBox(height: 12),
                SocialAuthButton(
                  provider: SocialAuthProvider.apple,
                  onPressed: () => _handleSocialLogin(SocialAuthProvider.apple),
                ),
              ],
            ),
          ),
        ),
      ),
    ),
  );
}

Widget _buildEmailForm() {
  return Form(
    key: _emailFormKey,
    child: Column(
      children: [
        AppTextField(
          controller: _emailController,
          label: 'Email',
          prefixIcon: Icons.email_outlined,
          keyboardType: TextInputType.emailAddress,
          validator: (value) {
            if (value == null || value.isEmpty) return 'Please enter your email';
            if (!value.contains('@')) return 'Please enter a valid email';
            return null;
          },
        ),
        const SizedBox(height: 16),
        PasswordField(
          controller: _passwordController,
          label: 'Password',
          validator: (value) {
            if (value == null || value.isEmpty) return 'Please enter your password';
            return null;
          },
        ),
      ],
    ),
  );
}

Widget _buildUsernameForm() {
  return Form(
    key: _usernameFormKey,
    child: Column(
      children: [
        AppTextField(
          controller: _usernameController,
          label: 'Username',
          prefixIcon: Icons.person_outline,
          validator: (value) {
            if (value == null || value.isEmpty) return 'Please enter your username';
            return null;
          },
        ),
        const SizedBox(height: 16),
        PasswordField(
          controller: _passwordController,
          label: 'Password',
          validator: (value) {
            if (value == null || value.isEmpty) return 'Please enter your password';
            return null;
          },
        ),
      ],
    ),
  );
}

Widget _buildPhoneForm() {
  return Form(
    key: _phoneFormKey,
    child: Column(
      children: [
        AppTextField(
          controller: _phoneController,
          label: 'Phone Number',
          hint: '+1 (555) 123-4567',
          prefixIcon: Icons.phone,
          keyboardType: TextInputType.phone,
          validator: (value) {
            if (value == null || value.isEmpty) return 'Please enter your phone number';
            if (value.length < 10) return 'Please enter a valid phone number';
            return null;
          },
        ),
        const SizedBox(height: 16),
        Text(
          'We\\'ll send you a verification code',
          style: Theme.of(context).textTheme.bodySmall,
        ),
      ],
    ),
  );
}
}
''';
}